假设您想创建一个2D NumPy数组,并希望使用连续的整数填充它。有哪些可选项?
示例:
import numpy
a=numpy.array([[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]])
在这种情况下,我当然可以使用
range(15)
来确定上限,但如何告诉numpy
在5
个值后创建新行?我正在寻找一般解决方案,因为我的真正问题涉及一个有466列和365行的2D数组。假设您想创建一个2D NumPy数组,并希望使用连续的整数填充它。有哪些可选项?
示例:
import numpy
a=numpy.array([[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]])
range(15)
来确定上限,但如何告诉numpy
在5
个值后创建新行?我正在寻找一般解决方案,因为我的真正问题涉及一个有466列和365行的2D数组。只需在正常的np.arange
调用上使用.reshape
,这将仅重新整形数组而不更改其数据。
In[101]: np.arange(15).reshape(3, 5)
Out[101]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In[102]: np.arange(466*365).reshape(466, 365)
Out[102]:
array([[ 0, 1, 2, ..., 362, 363, 364],
[ 365, 366, 367, ..., 727, 728, 729],
[ 730, 731, 732, ..., 1092, 1093, 1094],
...,
[168995, 168996, 168997, ..., 169357, 169358, 169359],
[169360, 169361, 169362, ..., 169722, 169723, 169724],
[169725, 169726, 169727, ..., 170087, 170088, 170089]])
a = np.arange(15).reshape(3,5)
,对于一般情况,a = np.arange(466*365).reshape(466,365)
应该可以解决问题。 - EdChum