columns = np.shape(lines)[0] # Gets x-axis dimension of array lines (to get numbers of columns)
lengths = np.zeros(shape=(2,1)) # Create a 2D array
# lengths = [[ 0.]
# [ 0.]]
lengths = np.arange(columns).reshape((columns)) # Makes array have the same number of columns as columns and fills it with elements going up from zero <--- This line seems to be turning it into a 1D array
输出:1D numpy的阵列没有得到再成形为一个二维阵列式打印长度后阵
print(lengths)
[0 1 2]
预期输出示例:
print(lengths)
[[0 1 2]] # Notice the double square bracket
这导致我不能够进入数据转换为数组的2D部分,因为它现在不再存在:
np.append(lengths, 65, axis=1)
AxisError: axis 1 is out of bounds for array of dimension 1
我希望数组是2D的,所以我可以在第一行存储“ID”,在第二行(在程序的稍后一点)存储值。我也知道我可以添加另一行到数组,而不是在初始化时进行。但是我宁愿不这样做,因为我听说这样效率低下,而且这个计划的成功高度依赖于性能。
谢谢。
你需要'np.arange(列).reshape(-1,列)'。 –