我对 Python 相当陌生,正在尝试编写线性/多项式回归。
我正在尝试手动设计三阶多项式的特征,而不是使用例如 scikit-learn。
我已从 CSV 文件导入数据并将其转换为行向量。现在我正在尝试创建一个具有三列的矩阵(原始特征、特征平方、特征立方)。
我希望为此使用 numpy.c_ ,但是我没有创建一个具有三列的矩阵,而是得到一个(非常)长的行向量。
屏幕截图说明了我正在尝试做什么以及我期望它如何表现(带有 x 和 X 的简短部分)以及它正在做什么(带有 francis_guide_vane_pos 和 francis_[...]_engineered 的部分) 我正在使用 Python v.3.11.4
# what I expected to happen
x = np.arange(0,20,1)
X = np.c_[x, x**2, x**3]
print(x, x.shape)
print(X, X.shape)
# what is actually happning
francis_guide_vane_pos_engineered = np.c_[francis_guide_vane_pos, francis_guide_vane_pos**2, francis_guide_vane_pos**3]
print(francis_guide_vane_pos, francis_guide_vane_pos.shape)
print(francis_guide_vane_pos_engineered, francis_guide_vane_pos_engineered.shape)
结果:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] (20,)
[[ 0 0 0]
[ 1 1 1]
[ 2 4 8]
[ 3 9 27]
[ 4 16 64]
[ 5 25 125]
[ 6 36 216]
[ 7 49 343]
[ 8 64 512]
[ 9 81 729]
[ 10 100 1000]
[ 11 121 1331]
[ 12 144 1728]
[ 13 169 2197]
[ 14 196 2744]
[ 15 225 3375]
[ 16 256 4096]
[ 17 289 4913]
[ 18 324 5832]
[ 19 361 6859]] (20, 3)
[[89.8 89.8 89.7 ... 14.8 14.8 14.8]] (1, 286996)
[[ 89.8 89.8 89.7 ... 3241.792 3241.792 3241.792]] (1, 860988)
编辑以包含书面代码而不仅仅是屏幕截图
请注意,在第一个示例中,
x
是形状为(20,)
的一维数组。您可以使用
np.c_
将它们堆叠起来以获得 (20, 3)
数组。
在第二种情况下,
francis_guide_vane_pos
是形状为(1, 286996)
的二维数组。要将其堆叠为列,请先转置数组,或将其压缩为一维数组:
>>> arr = np.c_[francis_guide_vane_pos.T, francis_guide_vane_pos.T**2, francis_guide_vane_pos.T**3]
>>> print(arr.shape) # (286996, 3)
或
>>> francis_guide_vane_pos = francis_guide_vane_pos.squeeze()
>>> arr = np.c_[francis_guide_vane_pos, francis_guide_vane_pos**2, francis_guide_vane_pos**3]
>>> print(arr.shape) # (286996, 3)