数学建模多元线性回归python代码
时间: 2024-11-28 13:14:35 浏览: 6
在Python中,我们通常使用scikit-learn库来进行多元线性回归的数学建模。以下是一个简单的例子,展示如何使用Pandas加载数据,然后进行模型训练:
```python
# 导入所需的库
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
# 加载数据(假设数据在csv文件中)
data = pd.read_csv('your_data.csv') # 替换为实际文件路径
X = data.drop('target_column', axis=1) # 假设目标变量是'target_column'
y = data['target_column']
# 划分数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建并训练模型
model = LinearRegression()
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
# 评估模型性能
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('R-squared Score:', metrics.r2_score(y_test, y_pred))
#
阅读全文