【发布时间】:2019-04-03 16:53:06
【问题描述】:
我正在尝试增加初始数组的维度:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
x = 10*rng.rand(50)
y = np.sin(x) + 0.1*rng.rand(50)
poly = PolynomialFeatures(7, include_bias=False)
poly.fit_transform(x[:,np.newaxis])
首先,我知道 np.newaxis 正在创建附加列。为什么需要这样做?
现在我将使用线性回归训练更新后的 x 数据(多边形)
test_x = np.linspace(0,10,1000)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
# train with increased dimension(x=poly) with its target
model.fit(poly,y)
# testing
test_y = model.predict(x_test)
当我运行它时,它给了我:ValueError: Expected 2D array, got scalar array instead: on model.fit(poly,y) line。我已经为多边形添加了一个维度,发生了什么?
x[:,np.newaxis] 与 x[:,np.newaxis] 之间还有什么区别。 x[:,无]?
【问题讨论】:
标签: python numpy machine-learning