对“data.csv”数据中的二手房房价数据集,通过LinearRegression进行实现房价预测。
要求:预测建筑面积为180平米的房子的总价是多少。
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
from sklearn.metrics import *
data=pd.read_csv("data.csv")
del data['Unnamed:0']
data.dropna(axis=0,how='any',inplace=True)
data[总价']=data['总价'].map(lambda e:e.replace('万',' ')
data[’总价']=data[’总价'].astype(float)
data['建筑面积']=data['建筑面积'].map(1ambda p:p.replace('平米',''))
data['建筑面积']=data['建筑面积'].astype(float)
new_data=data[['总价’,'建筑面积']]
data_train=new_data.loc[e:2001]
data_test=new_data.loc[2001:]
data_train=data_train-data_train.mean()/data_train.std()
x_train=pd.DataFrame(data_train['建筑面积'])
y_train=pd.DataFrame(data_train['总价'])
data_test=data_test-data_test.mean()/data_test.std()
x_test=pd.DataFrame(data_test['建筑面积'])
y_test=pd.DataFrame(data_test['总价'])
clf=linear model.Ridge()
clf.fit(x_train,y_train)
w=clf.coef_
b=clf.intercept_
y5=180*w+b
print(y5)