如何从numpy数组构造一个ndarray?蟒蛇

时间:2021-06-24 23:19:51

I can't seem to convert it into an ndarray in numpy, i've read http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html but it didn't show me how i can convert my input data as shown below into an ndarray.

我似乎无法将其转换为numpy中的ndarray,我已阅读http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html但它没有告诉我我是怎么做的可以将我的输入数据转换为ndarray,如下所示。

How to construct a ndarray from a numpy array or a list of integer lists? *What's the difference between ndarray and array?* I could just use an array type right?

如何从numpy数组或整数列表列表构造一个ndarray? * ndarray和array之间有什么区别?*我可以使用数组类型吗?

I have a list of integer counts like this

我有一个像这样的整数计数列表

[[1, 2, 4, 1, 5],
 [6, 0, 0, 0, 2],
 [0, 0, 0, 1, 0]]

And i manage to use this code to create a np.array as shown in http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array

我设法使用此代码创建一个np.array,如http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array中所示。

import numpy as np
x = [[1, 2, 4, 1, 5],
 [6, 0, 0, 0, 2],
 [0, 0, 0, 1, 0]]
print np.array(x)

[out]:

[OUT]:

[[1 2 4 1 5]
 [6 0 0 0 2]
 [0 0 0 1 0]]

But I can't change it into a np.ndarray with this code:

但我不能用这段代码将它改成np.ndarray:

import numpy as np
x = [[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
print np.ndarray(x)

I got an error:

我收到一个错误:

Traceback (most recent call last):
  File "/home/alvas/workspace/sklearntut/test.py", line 7, in <module>
    print np.ndarray(x)
TypeError: an integer is required

How do I create a np.ndarray with the list of integer counts i've got? What integer is the TypeError talking about?

如何使用我得到的整数计数列表创建一个np.ndarray? TypeError谈的是什么整数?

2 个解决方案

#1


26  

An ndarray is a NumPy array.

ndarray是一个NumPy数组。

>>> x = np.array([1, 2, 3])
>>> type(x)
<type 'numpy.ndarray'>

The difference between np.ndarray and np.array is that the former is the actual type, while the latter is a flexible shorthand function for constructing arrays from data in other formats. The TypeError comes your use of np.array arguments to np.ndarray, which takes completely different arguments (see docstrings).

np.ndarray和np.array之间的区别在于前者是实际类型,而后者是一种灵活的简写函数,用于从其他格式的数据构造数组。 TypeError使用了对np.ndarray的np.array参数,它使用完全不同的参数(参见docstrings)。

#2


5  

Though the accepted response is correct, that didn't help me actually create a 1-dimensional array of arrays.

虽然接受的响应是正确的,但这并没有帮助我实际创建一个数组的数组。

As this thread is the first answer at Google, I post my work around, even if it isn't elegant solution (please don't hesitate to point one out to me):

由于这个帖子是谷歌的第一个答案,我发布我的工作,即使它不是优雅的解决方案(请不要犹豫,向我指出一个):

import numpy as np

# Create example array
initial_array = np.ones(shape = (2,2))

# Create array of arrays
array_of_arrays = np.ndarray(shape = (1,), dtype = "object")
array_of_arrays[0] = initial_array

Be aware that array_of_arrays is in this case mutable, i.e. changing initial_array automatically changes array_of_arrays .

请注意,array_of_arrays在这种情况下是可变的,即更改initial_array会自动更改array_of_arrays。

#1


26  

An ndarray is a NumPy array.

ndarray是一个NumPy数组。

>>> x = np.array([1, 2, 3])
>>> type(x)
<type 'numpy.ndarray'>

The difference between np.ndarray and np.array is that the former is the actual type, while the latter is a flexible shorthand function for constructing arrays from data in other formats. The TypeError comes your use of np.array arguments to np.ndarray, which takes completely different arguments (see docstrings).

np.ndarray和np.array之间的区别在于前者是实际类型,而后者是一种灵活的简写函数,用于从其他格式的数据构造数组。 TypeError使用了对np.ndarray的np.array参数,它使用完全不同的参数(参见docstrings)。

#2


5  

Though the accepted response is correct, that didn't help me actually create a 1-dimensional array of arrays.

虽然接受的响应是正确的,但这并没有帮助我实际创建一个数组的数组。

As this thread is the first answer at Google, I post my work around, even if it isn't elegant solution (please don't hesitate to point one out to me):

由于这个帖子是谷歌的第一个答案,我发布我的工作,即使它不是优雅的解决方案(请不要犹豫,向我指出一个):

import numpy as np

# Create example array
initial_array = np.ones(shape = (2,2))

# Create array of arrays
array_of_arrays = np.ndarray(shape = (1,), dtype = "object")
array_of_arrays[0] = initial_array

Be aware that array_of_arrays is in this case mutable, i.e. changing initial_array automatically changes array_of_arrays .

请注意,array_of_arrays在这种情况下是可变的,即更改initial_array会自动更改array_of_arrays。