1

EDIT: I have changed np.array to np.arange to better explain what my problem is.

Being new to Python, I still struggle a lot with data structures. This seems like it should be a very obvious (and obviously duplicate) question, yet I can't understand enough of other explanations to make it work.

I need to create a list of lists (and / or arrays, I'll explain this in a minute), all with different lengths and ranges. I set up my initial arrays in the following way, but I still don't understand how to create the 'partotal' array. I know the syntax is wrong but still don't understand why.

import numpy as np

par1 = np.arange([10,80,1])
par2 = np.arange([50,120,1])
par3 = np.arange([0,40,1])
par4 = np.arange([0,30,1])

partotal = np.array([par1][par2][par3][par4])

The second problem, as you may have guessed, is that I have no idea whether I should be using numpy arrays, list of lists, pandas, or something else entirely. Since all my arrays are of different lengths, I find it hard to understand how to put things together or remove them again.

EDIT: The purpose of partotal is to create a set of starting positions for another function (see below)

inputnumber = 200

def positions(partotal, inputnumber):
    return np.random.uniform(partotal, size=(inputnumber, len(partotal)))

I know this must sound very basic but as a beginner I find it confusing and difficult. Most answers focus on syntax and don't help develop true insight. If someone can take some time to explain the very obvious I would appreciate it.

FINAL EDIT: The answer was very simple when I understood my own problem. I won't delete the rest of the post, for the sake of others who might need to follow my thought process.

par = 3
par1 = np.random.uniform(10,80,size=par)
par2 = np.random.uniform(5,120,size=par)
par3 = np.random.uniform(0,40,size=par)

allpar = np.array([par1,par2,par3])
7
  • Without knowing what you want to use partotal for, it's very hard to suggest how to build it. Are you looking for a 4x3 array? An array of 4 lists? Or an array of arrays? Something different? You can build a 4x3 array out of either np.array([par1, par2, par3, par4]) or by just stacking the arrays together, but I don't know if that's what you're looking for. Commented Apr 30, 2018 at 0:44
  • Also, you say in your description that have a bunch of lists of different lengths, but then your example is four arrays of the same size. An example that actually matches what you're trying to do—and with the output you actually want—would really help. Read minimal reproducible example; it's not always easy to figure out what to put in a question without guidance, and that's even more true for someone new to SO, but the help is pretty decent. Commented Apr 30, 2018 at 0:46
  • You say the lengths should be different, but your par1 through par4 all have the same length, which is 3. Were you expecting np.array([10,80,1]) to work like range or arange, and produce a 70-element result? Commented Apr 30, 2018 at 1:07
  • @user2357112, arrrgh... yes... I see at least part of where I'm going wrong now. So if I wanted to use arange or linspace (which is best?) that would give me a bunch of lists, right? So then how do I create a list of lists for them? Commented Apr 30, 2018 at 1:14
  • @abarnert I realised that one of my mistakes was using np.array in the same way as np.arange, giving me a result that I wasn't expecting. I have edited my original post and hopefully the problem makes more sense now. Commented Apr 30, 2018 at 1:41

3 Answers 3

1

If you're trying to create a 2d array you can just pass a 2d list to np.array.

np.array([[10, 80, 1], [50, 120, 1], [0, 40, 1], [0, 30, 1]])

Not sure if this is what you're going for, but in your specific case you'd do:

partotal = np.array([par1, par2, par3, par4])
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. Part of the problem is that I was using np.array wrong. I have edited my original question, I hope it now makes more sense.
0

A common use of uniform is to specify scalar start and stop values, and a shape:

In [101]: np.random.uniform(0,1,size=(3,4))
Out[101]: 
array([[0.87953793, 0.83726369, 0.53280941, 0.69611469],
       [0.78369061, 0.99258945, 0.65533223, 0.8342177 ],
       [0.69943211, 0.53965698, 0.06419811, 0.36591087]])

This is 12 values drawn from [0,1), arranged in a (3,4) array.

It's docs says that start and stop can be arrays, but isn't very clear about how they are used. The best clue is in:

np.broadcast(low, high).size samples are drawn.

So trying (3,1) and (1,4) inputs, we again get a (3,4) array.

In [102]: np.random.uniform(np.zeros((3,1)), np.ones((1,4)))
Out[102]: 
array([[0.35865707, 0.39068231, 0.9117642 , 0.49346499],
       [0.1439195 , 0.1217748 , 0.21780452, 0.83235673],
       [0.24894503, 0.36413268, 0.51516651, 0.8480244 ]])

To generate 3 numbers from (0,1), (10,11), and (20,21) respectively:

In [105]: np.random.uniform(np.arange(0,30,10), np.arange(1,31,10))
Out[105]: array([ 0.54715093, 10.75390957, 20.98101312])

I don't know what you are trying to do with those 4 arrays.

In [107]: par1 = np.arange(10,80)     # corrected usage
     ...: par2 = np.arange(50,120)
     ...: par3 = np.arange(0,40)
     ...: par4 = np.arange(0,30)

I could concatenate those 4 arrays into one:

In [108]: par = np.concatenate([par1,par2,par3,par4])
In [109]: par.shape
Out[109]: (210,)

With different lengths, this hstack is the only option.

Notice that I constructed a list of those arrays, and used that as an input to concatenate (as it expected).

In [110]: alist = [par1,par2,par3,par4]

A list of lists or list of arrays is a easy Python construct. It certainly should be used before trying to make an array of arrays (of differing sizes). But I don't see how that applies to uniform (as I just illustrated).

Another (3,4) array of random values:

In [111]: np.random.uniform(np.arange(3)[:,None], np.arange(4))
Out[111]: 
array([[0.        , 0.74651856, 0.60318064, 0.75254649],
       [0.49864947, 1.        , 1.60558937, 2.06444058],
       [1.03298196, 1.80321816, 2.        , 2.3358475 ]])

Each element is taken from a different range

 [0,0) [0,1) [0,2)
 [1,0) [1,1) [1,2)
 [2,0) ...

Comments

0

Thanks everyone for your help on what I realise was an extremely badly worded question. The answer turned out to be quite simple:

val = 4 # or any required number of start points
par1 = np.random.uniform(10,60,size=val)
par2 = np.random.uniform(0,20,size=val)
par3 = np.random.uniform(0,30,size=val)

allpar = np.array([par1,par2,par3])

This gives me an array of the correct values, with the right number of randomly generated points within specified boundaries.

Thanks everyone who contributed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.