读取D盘的"D:\housing.csv"文件,编写程序,分别用岭回归、神经网络、支持向量机方法建立房价预测模型,涵盖完整的数据建模步骤,从预测误差(MSE和MAE)和决定系数(R2)对比不同模型的预测性能,并绘制不同模型预测结果和真实结果的对比曲线图
时间: 2024-04-15 20:26:33 浏览: 105
好的!为了读取 D 盘上的 "D:\housing.csv" 文件,我们可以使用 `pandas` 库中的 `read_csv` 函数。以下是一个完整的示例程序,包括了读取数据、建立模型、计算性能指标和绘制结果的步骤:
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import matplotlib.pyplot as plt
# 读取数据集
data = pd.read_csv("D:/housing.csv")
# 划分特征和标签
X = data.drop('target', axis=1)
y = data['target']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 岭回归模型
ridge = Ridge()
ridge.fit(X_train, y_train)
ridge_pred = ridge.predict(X_test)
# 神经网络模型
mlp = MLPRegressor()
mlp.fit(X_train, y_train)
mlp_pred = mlp.predict(X_test)
# 支持向量机模型
svm = SVR()
svm.fit(X_train, y_train)
svm_pred = svm.predict(X_test)
# 计算性能指标
ridge_mse = mean_squared_error(y_test, ridge_pred)
ridge_mae = mean_absolute_error(y_test, ridge_pred)
ridge_r2 = r2_score(y_test, ridge_pred)
mlp_mse = mean_squared_error(y_test, mlp_pred)
mlp_mae = mean_absolute_error(y_test, mlp_pred)
mlp_r2 = r2_score(y_test, mlp_pred)
svm_mse = mean_squared_error(y_test, svm_pred)
svm_mae = mean_absolute_error(y_test, svm_pred)
svm_r2 = r2_score(y_test, svm_pred)
# 打印性能指标
print("Ridge Regression:")
print("MSE:", ridge_mse)
print("MAE:", ridge_mae)
print("R2 Score:", ridge_r2)
print("\nNeural Network:")
print("MSE:", mlp_mse)
print("MAE:", mlp_mae)
print("R2 Score:", mlp_r2)
print("\nSupport Vector Machine:")
print("MSE:", svm_mse)
print("MAE:", svm_mae)
print("R2 Score:", svm_r2)
# 绘制预测结果和真实结果对比曲线图
plt.figure(figsize=(10, 6))
plt.plot(y_test.values, label='True')
plt.plot(ridge_pred, label='Ridge')
plt.plot(mlp_pred, label='Neural Network')
plt.plot(svm_pred, label='Support Vector Machine')
plt.xlabel('Samples')
plt.ylabel('House Price')
plt.legend()
plt.show()
```
这段代码首先使用 `read_csv` 函数从指定路径读取 "D:\housing.csv" 文件,并将数据划分为特征和标签。然后,我们按照之前的步骤进行模型的建立、性能指标的计算以及绘制预测结果和真实结果的对比曲线图。
请注意,这里的代码使用了默认的模型参数。如果您想进一步优化模型性能,可以尝试调整模型的超参数或使用交叉验证等技术。希望这能帮到您!
阅读全文