学习数据分析的第一次练手项目。从网上爬取关于房价的相关数据属性来分析房价,并且基于一些属性来预测房价,使用的是网格搜索算法。相关的数据文件和完整代码可以从文末获取。
plt.style.use("fivethirtyeight")
sns.set_style({'font.sans_serif':['simhei','Arial']}) #转换字体
#提取所需数据保存到CSV中
def get_data():
with open('house_price_data.csv','rt') as f1:
f = open('./house_price.csv', 'wt', encoding='utf-8',newline='')
df = pd.read_csv('./house_price_data.csv', usecols=['price', 'bedrooms', 'sqft_living', 'grade'])
df.to_csv('./house_price.csv')
f.close()
f1.close()
def data_analyse():
df=pd.read_csv('./house_price.csv')
#df.info() #检查数据缺失值情况
#房价与室内面积关系的可视化
f,[ax1,ax2]=plt.subplots(1,2,figsize=(20,5))
sns.distplot(df['sqft_living'],bins=20,ax=ax1,color='r')
sns.kdeplot(df['sqft_living'],shade=True,ax=ax1)
sns.regplot(x='sqft_living',y='price',data=df,ax=ax2)
plt.show()
#房价与卧室个数关系的可视化
f,[ax1,ax2]=plt.subplots(1,2,figsize=(20,8))
sns.barplot(x='bedrooms',y='price',data=df,ax=ax1)
#房价与grade关系的可视化图
sns.barplot(x='grade',y='price',data=df,ax=ax1)
plt.show()
#绘制多变量之间的对比关系图
sns.pairplot(df,vars=('price','sqft_living','grade','bedrooms'))
plt.show()
绘制出来的图表:
data=pd.read_csv('./house_price.csv')
columns=['bedrooms','sqft_living','grade','price']
data=pd.DataFrame(data,columns=columns) #重新摆放位置
prices=data['price']
features=data.drop('price',axis=1)
features=np.array(features)
prices=np.array(prices)
features_train,features_test,prices_train,prices_test=train_test_split(features,prices,test_size=0.3,random_state=0)
#建立模型
def fit_model(X,y):
cross_validator=KFold(n_splits=10,shuffle=True)
regressor=DecisionTreeRegressor()
params={'max_depth':range(1,11)}
scoring_fnc=make_scorer(performance_metric)
grid=GridSearchCV(estimator=regressor,param_grid=params,scoring=scoring_fnc,cv=cross_validator)
grid=grid.fit(X,y)
return grid.best_estimator_
#计算R2分数
def performance_metric(y_true,y_predict):
score=r2_score(y_true,y_predict)
return score
#获得最优模型
optimal_reg=fit_model(features_train,prices_train)
print("最理想模型的参数‘max_depth’是{}".format(optimal_reg.get_params()['max_depth']))
predicted_value=optimal_reg.predict(features_test)
r2=performance_metric(prices_test,predicted_value)
print("最优模型在测试数据上R^2分数{:,.2f}".format(r2))
#房价预测
client_data=[[3,1200,8],[4,720,8],[5,2310,7]]
predicted_price=optimal_reg.predict(client_data)
for i,price in enumerate(predicted_price):
print("第{}位客户,根据您输入的信息预测到的房价为:¥{:,.2f}".format(i+1,price))
最后显示的结果为:
所需CSV数据及完整代码地址:https://github.com/songtown/data_Analysis