问题:在numpy中将一维数组转换为二维数组
我想通过指定2D数组中的列数将一维数组转换为二维数组。可能会像这样工作:
> import numpy as np
> A = np.array([1,2,3,4,5,6])
> B = vec2matrix(A,ncol=2)
> B
array([[1, 2],
[3, 4],
[5, 6]])
numpy是否具有类似于我的组合函数“ vec2matrix”的功能?(我知道您可以像2D数组一样索引1D数组,但这不是我拥有的代码中的选项-我需要进行此转换。)
I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this:
> import numpy as np
> A = np.array([1,2,3,4,5,6])
> B = vec2matrix(A,ncol=2)
> B
array([[1, 2],
[3, 4],
[5, 6]])
Does numpy have a function that works like my made-up function “vec2matrix”? (I understand that you can index a 1D array like a 2D array, but that isn’t an option in the code I have – I need to make this conversion.)
回答 0
您要reshape
阵列。
B = np.reshape(A, (-1, 2))
其中-1
,从输入数组的大小推断出新维度的大小。
You want to reshape
the array.
B = np.reshape(A, (-1, 2))
where -1
infers the size of the new dimension from the size of the input array.
回答 1
您有两种选择:
如果您不再想要原始形状,最简单的方法就是为数组分配一个新形状
a.shape = (a.size//ncols, ncols)
您可以切换a.size//ncols
通过-1
自动计算合适的形状。确保a.shape[0]*a.shape[1]=a.size
,否则会遇到一些问题。
您可以使用np.reshape
函数获得一个新的数组,该函数的工作原理与上述版本相似
new = np.reshape(a, (-1, ncols))
如果可能,new
将仅是初始array的视图a
,这意味着数据是共享的。但是,在某些情况下,new
数组将被复制。请注意,np.reshape
还接受一个可选关键字order
,该关键字使您可以从行优先C顺序切换到列优先Fortran顺序。np.reshape
是该a.reshape
方法的函数版本。
如果您不能满足要求a.shape[0]*a.shape[1]=a.size
,则必须创建一个新数组。您可以使用该np.resize
函数并将其与混合使用np.reshape
,例如
>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)
You have two options:
If you no longer want the original shape, the easiest is just to assign a new shape to the array
a.shape = (a.size//ncols, ncols)
You can switch the a.size//ncols
by -1
to compute the proper shape automatically. Make sure that a.shape[0]*a.shape[1]=a.size
, else you’ll run into some problem.
You can get a new array with the np.reshape
function, that works mostly like the version presented above
new = np.reshape(a, (-1, ncols))
When it’s possible, new
will be just a view of the initial array a
, meaning that the data are shared. In some cases, though, new
array will be acopy instead. Note that np.reshape
also accepts an optional keyword order
that lets you switch from row-major C order to column-major Fortran order. np.reshape
is the function version of the a.reshape
method.
If you can’t respect the requirement a.shape[0]*a.shape[1]=a.size
, you’re stuck with having to create a new array. You can use the np.resize
function and mixing it with np.reshape
, such as
>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)
回答 2
尝试类似的方法:
B = np.reshape(A,(-1,ncols))
您需要确保可以将数组中的元素数除以ncols
。您也可以B
使用order
关键字按照将数字拉入的顺序进行游戏。
Try something like:
B = np.reshape(A,(-1,ncols))
You’ll need to make sure that you can divide the number of elements in your array by ncols
though. You can also play with the order in which the numbers are pulled into B
using the order
keyword.
回答 3
如果您的唯一目的是将1d数组X转换为2d数组,请执行以下操作:
X = np.reshape(X,(1, X.size))
If your sole purpose is to convert a 1d array X to a 2d array just do:
X = np.reshape(X,(1, X.size))
回答 4
import numpy as np
array = np.arange(8)
print("Original array : \n", array)
array = np.arange(8).reshape(2, 4)
print("New array : \n", array)
import numpy as np
array = np.arange(8)
print("Original array : \n", array)
array = np.arange(8).reshape(2, 4)
print("New array : \n", array)
回答 5
some_array.shape = (1,)+some_array.shape
或换一个新的
another_array = numpy.reshape(some_array, (1,)+some_array.shape)
这将使尺寸+1,等于在最外层添加一个括号
some_array.shape = (1,)+some_array.shape
or get a new one
another_array = numpy.reshape(some_array, (1,)+some_array.shape)
This will make dimensions +1, equals to adding a bracket on the outermost
回答 6
您可以flatten()
从numpy包中使用。
import numpy as np
a = np.array([[1, 2],
[3, 4],
[5, 6]])
a_flat = a.flatten()
print(f"original array: {a} \nflattened array = {a_flat}")
输出:
original array: [[1 2]
[3 4]
[5 6]]
flattened array = [1 2 3 4 5 6]
You can useflatten()
from the numpy package.
import numpy as np
a = np.array([[1, 2],
[3, 4],
[5, 6]])
a_flat = a.flatten()
print(f"original array: {a} \nflattened array = {a_flat}")
Output:
original array: [[1 2]
[3 4]
[5 6]]
flattened array = [1 2 3 4 5 6]
回答 7
不使用Numpy将一维数组更改为二维数组。
l = [i for i in range(1,21)]
part = 3
new = []
start, end = 0, part
while end <= len(l):
temp = []
for i in range(start, end):
temp.append(l[i])
new.append(temp)
start += part
end += part
print("new values: ", new)
# for uneven cases
temp = []
while start < len(l):
temp.append(l[start])
start += 1
new.append(temp)
print("new values for uneven cases: ", new)
Change 1D array into 2D array without using Numpy.
l = [i for i in range(1,21)]
part = 3
new = []
start, end = 0, part
while end <= len(l):
temp = []
for i in range(start, end):
temp.append(l[i])
new.append(temp)
start += part
end += part
print("new values: ", new)
# for uneven cases
temp = []
while start < len(l):
temp.append(l[start])
start += 1
new.append(temp)
print("new values for uneven cases: ", new)