简体   繁体   English

如何使用 numpy ndarray 使用给定数据创建 n 维数组

[英]How to create an n-dimensional array using given data using numpy ndarray

I am trying to cast my data into an ndarray.我正在尝试将我的数据转换为 ndarray。 Although I get what I want but while testing I am not able to figure out why it gives me this output.虽然我得到了我想要的,但是在测试时我无法弄清楚为什么它会给我这个输出。

I get the output:我得到输出:

np.ndarray(shape = (2,1),buffer = np.array([1,2,3,4]))
array([[4.24399158e-314],
   [8.48798317e-314]])

However for this block I am able to cast my random numbers the way I want.但是对于这个块,我可以按照我想要的方式投射我的随机数。

def x_mat(n,m):
   np.random.seed(0)
   randoms = np.random.normal(size=n*m)
   print(randoms)
   rand_mat = np.ndarray(shape = (n,m),buffer = randoms)
   return(rand_mat)
x_mat(5,2)

to give:给:

[ 1.76405235  0.40015721  0.97873798  2.2408932   1.86755799 -0.97727788 
  0.95008842 -0.15135721 -0.10321885  0.4105985 ]


array([[ 1.76405235,  0.40015721],
       [ 0.97873798,  2.2408932 ],
       [ 1.86755799, -0.97727788],
       [ 0.95008842, -0.15135721],
       [-0.10321885,  0.4105985 ]])

If someone can help me understand this behavior.如果有人可以帮助我理解这种行为。 It does the correct thing in the second case but I find the first case as something that is not straight forward.它在第二种情况下做了正确的事情,但我发现第一种情况并不直接。 Why is the first case the way it is?为什么第一种情况是这样的?

From the documentation on ndarray with the signature.来自带有签名的ndarray 文档

ndarray(shape, dtype, buffer, offset, strides, order)

  1. If buffer is None, then only shape, dtype, and order are used.如果buffer为 None,则仅使用 shape、dtype 和 order。
  2. If buffer is an object exposing the buffer interface, then all keywords are interpreted.如果buffer是一个暴露缓冲区接口的对象,那么所有的关键字都会被解释。

Seems to be that for the first example, even though the buffer is an np.array of integers, ndarray interprets it as a different type.对于第一个示例,似乎是这样,即使buffer是整数的np.arrayndarray也将其解释为不同的类型。 You have to give it the optional argument dtype = int so that the ndarray explicitly know its elements are of type integer.您必须为其提供可选参数dtype = int以便 ndarray 明确知道其元素是整数类型。

np.ndarray(shape = (2,1), dtype = int, buffer = np.array([1,2,3,4]))
In [164]: x = np.array([1,2,3,4])
In [165]: x.__array_interface__
Out[165]: 
{'data': (40465184, False),
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (4,),
 'version': 3}

The array created with ndarray has the same data buffer:使用ndarray创建的数组具有相同的data缓冲区:

In [166]: y = np.ndarray(shape = (2,1),buffer = x)
In [167]: y
Out[167]: 
array([[5.e-324],
       [1.e-323]])
In [168]: y.__array_interface__
Out[168]: 
{'data': (40465184, False),        # same as for x
 'strides': None,
 'descr': [('', '<f8')],
 'typestr': '<f8',
 'shape': (2, 1),
 'version': 3}

and for an array using the int dtype:对于使用 int dtype 的数组:

In [169]: z = np.ndarray(shape = (2,1),buffer = x, dtype=int)
In [170]: z.__array_interface__
Out[170]: 
{'data': (40465184, False),
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (2, 1),
 'version': 3}

In fact we don't need to pass x as the buffer argument, just its x.data attribute:事实上,我们不需要将x作为缓冲区参数传递,只需传递其x.data属性即可:

In [171]: np.ndarray(shape = (2,1), buffer = x.data, dtype=int)
Out[171]: 
array([[1],
       [2]])

Because of the shared data buffer, modifications to z appear in x as well In [172]: z[:] *= 2 In [173]: z Out[173]: array([[2], [4]]) In [174]: x Out[174]: array([2, 4, 3, 4])由于共享数据缓冲区,对z的修改也出现在x中 In [172]: z[:] *= 2 In [173]: z Out[173]: array([[2], [4]])输入 [174]: x 输出[174]: 数组([2, 4, 3, 4])

and y :y

In [175]: y
Out[175]: 
array([[1.e-323],
       [2.e-323]])

With an offset we can make an array that selects a different part of the data buffer:使用offset ,我们可以创建一个选择数据缓冲区不同部分的数组:

In [178]: w=np.ndarray(shape = (2,1), buffer = x.data, dtype=int, offset=8)
In [179]: w
Out[179]: 
array([[4],
       [3]])

But normally we don't use ndarray to do this.但通常我们不使用ndarray来执行此操作。 Instead we use slicing to get a view , with reshape if needed:相反,我们使用切片来获取view ,如果需要,可以使用reshape

In [181]: x[1:3].reshape(2,1)
Out[181]: 
array([[4],
       [3]])

You can construct the equivalent of rand_mat with randoms.reshape(n,m) or np.random.normal(size=(n,m)) .您可以使用randoms.reshape(n,m)np.random.normal(size=(n,m))构造等价的rand_mat

ndarray is normally only needed when the buffer comes from so other source, something that isn't already an ndarray which can be reshaped or viewed . ndarray通常仅在buffer来自其他来源时才需要,这些来源还不是可以ndarrayviewed的 ndarray 。

Let's consider your first example:让我们考虑您的第一个示例:

n = np.ndarray(shape=(2,2), buffer= np.array([1,12,3,4,5,6]), dtype=int, order='F') 
#Note that order is 'F' here and the the order changes from above. 
print(n)

Output: 
[[ 1  3]
 [12  4]]

Observe since buffer is provided, now it looks further into shape, dtype, and order.观察由于提供了缓冲区,现在它进一步研究了形状、数据类型和顺序。 Let's change the order to 'c' and see what happens.让我们将顺序更改为“c”,看看会发生什么。


n = np.ndarray(shape=(2,2), buffer= np.array([1,12,3,4,5,6]), dtype=int, order='C') 
#Note that order is 'C' here.
print(n)

[[ 1 12]
 [ 3  4]]

Now, let's make the buffer to 'None'.现在,让我们将缓冲区设为“无”。 Then it simply takes 0's and 1's values and applies the shape and dtype to it.然后它只需要 0 和 1 的值并将形状和 dtype 应用到它。

n = np.ndarray(shape=(2,2), buffer= None, dtype=int, order='F') 
print(n)

Output:
[[1 1]
 [1 0]]

So to conclude, whenever there is buffer takes an array.总而言之,只要有缓冲区,就需要一个数组。 It has to apply dtype, order and shape to it.它必须对其应用 dtype、order 和 shape。 When it is None, buffer is simply not applicable.当它为无时,缓冲区根本不适用。 Hope this helps!:)希望这可以帮助!:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM