特征矩阵中的每个观察值都由 2 个值组成(对于 2 个特征)。您试图一次传递 6 个值,而不是将这 6 个值分成 3 个数组,每个数组包含 2 个值(表示数据中的观察值)。
import numpy as np
from sklearn.linear_model import LinearRegression
x = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]
x, y = np.array(x), np.array(y)
model = LinearRegression().fit(x, y)
test_x = np.array([[5, 20], [14, 32], [22, 38]])
model.predict(test_x)
我可以为您推荐两种方法:
- 您可以对在预测输出时不希望使用的列使用零值。
- 您可以根据所需功能训练模型。
"""create dummy data"""
import pandas as pd
import numpy as np
# construct a few features
features = np.array([[2, 2],
[4, 6],
[9, 1],
[6, 2]])
# construct a target
target = np.array([15, 20, 50, 18])
# construct a dataframe
dataframe = pd.DataFrame()
dataframe['Price'] = features[:, 0]
dataframe['HorsePower'] = features[:, 1]
dataframe['Cost'] = target
# p.s. I've used the long method to construct my dataframe, you may pass data using the 'data' parameter.
print(dataframe)
print(' ')
# separate features matrix and target vector
features = dataframe.iloc[:, 0:2]
target = dataframe.iloc[:, -1]
# import package
from sklearn.linear_model import LinearRegression
# create instance of LR
algorithm = LinearRegression()
# train the model on both features
model = algorithm.fit(features, target)
# view parameters and hyperparameters
print(model)
# create observation passing values for both features
observation = [[9, 1]]
# obtain predictions
predictions = model.predict(observation)
# print prediction
print(predictions)
plt.scatter(dataframe.index, target, color='crimson', marker='v', edgecolors='black', label='Target_Value')
plt.scatter(dataframe.index, model.predict(features), color='silver', marker='d', edgecolors='black', label='Predicted_Value')
plt.title('Scatter Plot Showing Predicted Target Values Vs Actual Target Values', color='blue')
plt.xlabel('Observation Number', color='blue')
plt.ylabel('Value', color='blue')
plt.legend(numpoints=1, loc='best')
plt.show()
# train model, this time on desired feature (s)
model = algorithm.fit(np.array(features.iloc[:, 0]).reshape(-1, 1),
target)
# obtain prediction
prediction = model.predict([[2]])
# print predictions
print(prediction)