如何将Python数组中的条目转换为数组?

huangapple go评论64阅读模式
英文:

How do i convert the entries within an array into arrays in python?

问题

[1,1,1,1] -> [[1],[1],[1],[1]]

英文:

I need to convert a numpy array as follows:

[1,1,1,1] -> [[1],[1],[1],[1]]

答案1

得分: 5

使用 np.reshape

import numpy as np

values = np.random.randint(1, 10, 10)
print(values)
values = values.reshape((-1, 1))
print(values)

<!-- -->

重新整形之前

[5 9 8 1 2 6 2 2 9 8]

重新整形之后

[[5]
[9]
[8]
[1]
[2]
[6]
[2]
[2]
[9]
[8]]

使用 -1 告诉 NumPy 新的形状应该与原始形状兼容(即它应该根据数组中的元素数量来确定该维度的值)。

英文:

Use np.reshape:

import numpy as np

values = np.random.randint(1, 10, 10)
print(values)
values = values.reshape((-1, 1))
print(values)

<!-- -->

# Before reshaping
[5 9 8 1 2 6 2 2 9 8]

# After reshaping
[[5]
 [9]
 [8]
 [1]
 [2]
 [6]
 [2]
 [2]
 [9]
 [8]]

Using -1 tells NumPy that the new shape should be compatible with the original shape (i.e. it should figure out the value for that dimension based on the number of elements in the array).

答案2

得分: 2

以下是翻译好的部分:

另一种回答已经给出的.reshape(-1,1)的方法是添加一个新的轴

x=np.arange(4)
x[:,None]
#array([[0],
#       [1],
#       [2],
#       [3]])

请注意,无论是(reshape和[:,None])都只是创建了原始数组的视图。
所以

x=np.arange(4)
x[:,None][0,0] = 12
x.reshape(-1,1)[1,0]=13
x
# [12, 13, 2, 3]

如果你想要一个新的数组,可能需要.copy()结果。

在这种情况下,我不会偏好.reshape(-1,1)[:,None]之间的任何一个。但是当你有多于1个轴时,后者可能更灵活。例如,假设你有一个2维数组(我为了这个示例而使用reshape创建的)

x=np.arange(12).reshape(-1, 4) # 这里我使用reshape,因为我想要指定形状:我想要4行。
# array([[ 0,  1,  2,  3],
#       [ 4,  5,  6,  7],
#       [ 8,  9, 10, 11]])

现在你想将它转换为

array([[ [0],  [1], [2],  [3]],
       [ [4],  [5], [6],  [7]],
       [ [8],  [9], [10], [11]]])

在这种情况下,使用reshape更难。你需要知道你的x的形状(这并不是机密信息,当然,但它变得不那么自然)。

你不能x.reshape(-1,-1,1)。你要么x.reshape(3,4,1),要么只有一个,不再多,-1,就像x.reshape(3,-1,1)x.reshape(-1,4,1)。或者,更通用地说x.reshape(x.shape+(1,))

而对于newaxis,因为我们只是添加了一个轴,而不是指定轴的大小,所以在1D中仍然与之前相同。

x[:,:,None]
# 也可以写成
x[:,:,np.newaxis]

所以,当你想要重塑事物(改变大小)时,我会让reshape来处理,当你只想添加一个新轴时,我会让newaxis来处理(像这样写,似乎很明显)。

英文:

An alternative answer to the already given .reshape(-1,1) is to add a new axis

x=np.arange(4)
x[:,None]
#array([[0],
#       [1],
#       [2],
#       [3]])

Note that both, (reshape and [:,None]) create just a view of the original array.
So

x=np.arange(4)
x[:,None][0,0] = 12
x.reshape(-1,1)[1,0]=13
x
# [12, 13, 2, 3]

If you wanted a new array, you may need to .copy() the result.

In such case, I wouldn't have any preference between .reshape(-1,1) and [:,None]. But the latter may be more flexible when you have more than 1 axis. For example, let say that you have a 2d array (that I create for this example using a reshape, incidently)

x=np.arange(12).reshape(-1, 4) # here I use reshape, because it is a shape that I want to specify : I want rows of 4.
# array([[ 0,  1,  2,  3],
#       [ 4,  5,  6,  7],
#       [ 8,  9, 10, 11]])

Now you want to convert this to

array([[ [0],  [1], [2],  [3]],
       [ [4],  [5], [6],  [7]],
       [ [8],  [9], [10], [11]]])

reshape is harder to use in such case. You need to know the shape of your x (which isn't a top secret information, sure, but it becomes less natural).

You cannot x.reshape(-1,-1,1). You have to either x.reshape(3,4,1) or having one, no more, -1, like x.reshape(3,-1,1) or x.reshape(-1,4,1). Or, more generically x.reshape(x.shape+(1,))

Where as with newaxis, since we are just adding an axis, not specifying the size of the axes, it is still the same as with 1D.

x[:,:,None]
# aka
x[:,:,np.newaxis]

So, I would let reshape the job, when you want to reshape things (change the sizes), and let newaxis the job when you just want to add a new axis (written like that, it seems obvious).

答案3

得分: 1

尽管不是NumPy,你也可以在普通的Python中使用列表推导来轻松实现这个:

lst = [1, 1, 1, 1]
reshaped = [[el] for el in lst]
print(reshaped)
[[1], [1], [1], [1]]
英文:

Although not numpy, you could also do this really easily with list comprehension in plain old Python:

&gt;&gt;&gt; lst = [1,1,1,1]
&gt;&gt;&gt; reshaped = [[el] for el in lst]
&gt;&gt;&gt; print(reshaped)
[[1], [1], [1], [1]]

huangapple
  • 本文由 发表于 2023年6月25日 21:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定