python机器学习分析影响房价的主要因素

博文python使用线性模型lasso对波士顿房价进行预测介绍了如何使用线性模型lasso对波士顿房价进行预测,在这个数据集中给出了13个影响房价的因素,显然,这些因素对房价的影响重要性是不同的,那么怎么来看那些因素比较重要呢?我们可以使用gbr_reg这个模型来进行回归,并且调用其中的feature_importance_参数来做量化判断,最终,我们可以对该重要性指标进行排序并且可视化输出。具体的代码如下:

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV
from sklearn import ensemble
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
boston = datasets.load_boston()
X = boston.data
y = boston.target
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=13)
gbr_reg = ensemble.GradientBoostingRegressor()
gbr_reg.fit(X_train,y_train)
feature_importance_df = pd.DataFrame({
		'name':boston.feature_names,
		'importance':gbr_reg.feature_importances_
	})
feature_importance_df.sort_values(by = 'importance',ascending=False,inplace = True)
x_axis = list(feature_importance_df['name'])
y_axis = list(feature_importance_df['importance'])
plt.title('Importance of Factors')
plt.ylabel('Importance')
plt.bar(x_axis,y_axis)
plt.show()

运行程序之后,我们可以得到如下图像:
在这里插入图片描述
我们可以看到,LSTA、RM和DIS分列重要性排行的前三位。我们可以查得他们代表的意义分别是:人口中地位较低人群的百分数、平均每居民房数、与五个波士顿就业中心的加权距离。

发布了179 篇原创文章 · 获赞 9 · 访问量 5554

猜你喜欢

转载自blog.csdn.net/weixin_41855010/article/details/104507755
今日推荐