我正在尝试将一个数组传递给一个函数,该函数将根据传递给它的 x 值数组预测 y 值。该模型根据风速预测发电量。我正在使用一系列风速传递给函数,这将有望创建一系列发电。 我相信我的问题是将数组作为输入参数传递,但是我的函数也可能是错误的,所以如果这是问题,我将不胜感激,如果您能提供帮助! 我将在下面包含一些相关的代码。
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 2)
wspoly = poly_reg.fit_transform(ws)
wspoly
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=3)
wspoly = poly_reg.fit_transform(ws)
lin_reg2 = LinearRegression()
lin_reg2.fit(wspoly,elec)
plt.figure(figsize = (25,15))
x_grid = np.arange(min(ws), max(ws), 0.1)
x_grid = x_grid.reshape(len(x_grid), 1)
plt.scatter(ws, elec, color = 'red')
plt.plot(x_grid, lin_reg2.predict(poly_reg.fit_transform(x_grid)), color = 'blue')
plt.title("Polynomial Regression of Wind Speed & Electricty Generated")
plt.xlabel('Wind Speed (m/s)')
plt.ylabel('Electricity Generated (kWh)')
plt.show()
prediction = lin_reg2.predict(poly_reg.fit_transform([[10]]))
print("Using Polynomial Regression, the prediction is: ", prediction)
上面使用模型来预测 x 值为 10 时的 y 值。理想情况下,我希望这个 '10' 是一个包含许多 x 值的数组。
forecast = pd.DataFrame(df, columns = ['Avg WS'])
forecast2000 = forecast.head(1976)
forecast2000.head()
array = np.array(forecast2000['Avg WS'])
array
这是我想用来代替上面的“10”的数组。此点以下的所有内容都展示了我尝试使用函数来使其工作的尝试。我在这种方法中可能完全错误,因此请随意丢弃此方法并从您喜欢的任何方面提供帮助。
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 2)
arraypoly1 = array[:,None]
arraypoly = poly_reg.fit_transform(arraypoly1)
arraypoly
def myfunc(*xval):
for x in xval:
lin_reg2.predict(poly_reg.fit_transform([[x]]))
return (yval)
myfunc(arraypoly)
上述尝试提供了一个错误,即它具有“找到带有暗淡 4 的数组。估计器预期 <= 2。”
请帮忙!!