欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 机器学习_线性回归_岭回归算法预测波士顿房价代码实现(机器学习全流程)(附带数据集housingData下载链接, 长期有效)

机器学习_线性回归_岭回归算法预测波士顿房价代码实现(机器学习全流程)(附带数据集housingData下载链接, 长期有效)

2024/10/23 6:35:55 来源:https://blog.csdn.net/JR521314/article/details/142916415  浏览:    关键词:机器学习_线性回归_岭回归算法预测波士顿房价代码实现(机器学习全流程)(附带数据集housingData下载链接, 长期有效)

# 1.导入外部数据集HousingData
import pandas as pd
boston_data=pd.read_csv(r"C:\Users\鹰\Desktop\ML_Set\HousingData.csv")# 数据基本描述
print(boston_data.head())
print(boston_data.describe())
print(boston_data.shape)# 2.数据基本处理-缺失值处理,特征值和目标值确定, 数据集分割
# 缺失值处理-删除存在缺失值的数据行
print("缺失值:")
print(boston_data.isna().sum())
# if (~boston_data).any():
#     print("删除...")
#     boston_data= boston_data.dropna()
#     print(boston_data.isna().sum())
print("删除...")
boston_data= boston_data.dropna()
print(boston_data.isna().sum())
# 确定特征值和目标值, iloc[]取值
x_all=boston_data.iloc[:,:-1]
y_all=boston_data.iloc[:,-1]
# 数据集分割
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_all,y_all,test_size=0.2)# 3.特征工程-特征预处理-标准化
# 特征预处理:统一单位, 进行标准化处理
from sklearn.preprocessing import StandardScaler
sacler=StandardScaler()
sacler.fit_transform(x_train)
sacler.fit_transform(x_test)
# 特征转换和特征降维暂时不需要# 4.模型训练-选择调用模型--1.Ridge回归, 2.RidgeCV交叉验证岭回归
from sklearn.linear_model import Ridge, RidgeCV
# estimator=Ridge(alpha=1)
estimator=RidgeCV(alphas=(0.001, 0.01, 0.1, 1, 10, 100, 1000))
estimator.fit(x_train, y_train)# 5.模型评估
# 预测值, 利用测试集的特征值进行训练
y_predict=estimator.predict(x_test)
print(f"predict_values(预测值) are \n {y_predict}")
# 准确率, 通过测试集的特征值和目标值进行训练
score=estimator.score(x_train,y_train)
print(f"accuracy(准确率) is {score}")
# 线性回归评价指标-MSE
from sklearn.metrics import mean_squared_error
mse=mean_squared_error(y_test, y_predict)
print(f"MSE(均方误差) is {mse}")

 

需要下载数据集的兄弟们, 看这里

链接:https://pan.baidu.com/s/1uQiYeN5QYwIBAWj9LG1EgQ 
提取码:oem8

作者的废话

作为辛辛苦苦下载不到数据集的小白, 我太理解这种感受了, 只有有机会, 我就会共享下载链接,

咱们可以花钱, 但不能花这种冤枉钱呀, 

其实, 生活中, 很多答案, 只要多留心一点, 都是可以找到的, 

简言之, 只要思想不滑坡, 方法总比困难多, 这碗鸡汤, 我干了, 你随意

借用一句话, think big,  strat small

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com