在python中使用数组预测值

问题描述

我正在寻找解决以下问题的方法。我希望使用回归模型来预测一些值。我目前可以使用它来预测单个值,即模型将预测 x 值等于 10 的 y 值。我想做的是使用数组作为输入值。我希望当第二个数组用作输入值时,我可以让模型创建一个新的预测值数组。这可能吗?我附上了一些代码,希望这会有所帮助。

 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()

Output from the above code showing the polynomial regression model

   prediction = lin_reg2.predict(poly_reg.fit_transform([[10]]))

解决方法

首先创建一个函数来返回预测。该函数将如下所示。

def my_func(x_value):
    # your code goes here
    return y_value

然后你可以像这样创建一个预测数组。

y_values = [my_func(x) for x in list_of_x_values]