import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression #导入数据集 data = pd.read_csv('data.csv') #分割训练集和测试集 X = data.iloc[:, :-1] y = data.iloc[:, -1] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) #模型训练 model = LinearRegression() model.fit(X_train, y_train) #预测结果 y_pred = model.predict(X_test) #评估模型性能 score = model.score(X_test, y_test) print(score)