首页 > 解决方案 > NumPy, .reshape()

问题描述

这段代码有什么作用?

climate_results = np.concatenate((climate_data, yields.reshape(1000, 1)), axis=1)

注意:我有一个名为气候数据和产量的变量

标签: pythonnumpy

解决方案


numpy.concatenate(), 连接两个数组。

(在您的代码中,气候数据有 1000 行,在此示例中,气候数据有 2 行,我们应该重塑为可以连接两个数组的 1000 或 2 行) 请参阅此示例:

climate_data = np.array([[1,2],[3,4]])
# array([[1, 2],
#        [3, 4]])

yields = np.array([0,1])
# array([0, 1])

yields = np.array([0,1]).reshape(2,1)
# array([[0],
#        [1]])

climate_results = np.concatenate((climate_data, yields.reshape(2, 1)), axis=1)
climate_results

输出:

array([[1, 2, 0],
       [3, 4, 1]])

您可以axis=0像下面这样连接:

climate_data = np.array([[1,2],[3,4]])
yields = np.array([[0,1]])
# array([[0, 1]])
climate_results = np.concatenate((climate_data, yields), axis=0)

输出:

array([[1, 2],
       [3, 4],
       [0, 1]])

推荐阅读