numpy数组vs列表列表?

时间:2021-12-30 21:45:01

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

编辑:我已将np.array更改为np.arange以更好地解释我的问题。

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.

作为Python的新手,我仍然在为数据结构付出很多努力。这似乎应该是一个非常明显(并且明显重复)的问题,但我无法理解其他解释以使其有效。

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.

我需要创建一个列表(和/或数组,我将在一分钟内解释),所有列表都有不同的长度和范围。我以下面的方式设置我的初始数组,但我仍然不明白如何创建'partotal'数组。我知道语法错误但仍然不明白为什么。

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.

你可能已经猜到的第二个问题是我不知道我是应该使用numpy数组,列表列表,pandas还是其他完全不同的东西。由于我的所有数组都有不同的长度,我发现很难理解如何将它们放在一起或再次删除它们。

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

编辑:partotal的目的是为另一个函数创建一组起始位置(见下文)

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])

3 个解决方案

#1


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.

这是从[0,1)绘制的12个值,排列在(3,4)数组中。

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.

np.broadcast(低,高).size样本被绘制。

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

所以尝试(3,1)和(1,4)输入,我们再次获得(3,4)数组。

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:

从(0,1),(10,11)和(20,21)分别生成3个数字:

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.

我不知道你要对这4个阵列做什么。

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:

我可以将这4个数组连接成一个:

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.

不同长度,这个hstack是唯一的选择。

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).

列表或数组列表是一个简单的Python构造。它当然应该在尝试制作阵列数组(不同大小)之前使用。但我不明白这是如何适用于制服(正如我刚才所说)。

Another (3,4) array of random values:

另一个(3,4)随机值数组:

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) ...

#2


0  

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

如果您正在尝试创建二维数组,则可以将二维列表传递给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])

#3


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.

谢谢所有贡献的人。

#1


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.

这是从[0,1)绘制的12个值,排列在(3,4)数组中。

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.

np.broadcast(低,高).size样本被绘制。

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

所以尝试(3,1)和(1,4)输入,我们再次获得(3,4)数组。

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:

从(0,1),(10,11)和(20,21)分别生成3个数字:

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.

我不知道你要对这4个阵列做什么。

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:

我可以将这4个数组连接成一个:

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.

不同长度,这个hstack是唯一的选择。

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).

列表或数组列表是一个简单的Python构造。它当然应该在尝试制作阵列数组(不同大小)之前使用。但我不明白这是如何适用于制服(正如我刚才所说)。

Another (3,4) array of random values:

另一个(3,4)随机值数组:

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) ...

#2


0  

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

如果您正在尝试创建二维数组,则可以将二维列表传递给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])

#3


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.

谢谢所有贡献的人。