波士頓房價線性回歸

2021-10-09 04:56:15 字數 1528 閱讀 1683

from matplotlib import pyplot as plt

from sklearn import linear_model

from sklearn.model_selection import train_test_split

import numpy as np

import pandas as pd

plt.rcparams['font.sans-serif'] = ['kaiti']

data_path1 = 'housing_true.xls'

hd1 = pd.read_excel(data_path1)

data_ = hd1.drop(['medv'],axis=1)

y1 = hd1['medv']

y = np.reshape(y1,len(y1),1)

#將資料集7:3比例分割

x_train, x_test, y_train, y_test =train_test_split(data_,y,test_size=0.3,random_state=1)

#訓練lr = linear_model.linearregression()

lr.fit(x_train,y_train)

score_test = lr.score(x_test,y_test)

print(score_test)

#print(lr.coef_)#斜率

#plt.plot(lr.coef_)#斜率視覺化

test_pre = lr.predict(x_test)

#print(test_pre)

#print(y_test)

dev = y_test-test_pre #偏差

#print(dev)

rmse = np.sum(np.sqrt(dev*dev))/152#共152資料

print(rmse)#均方根誤差

x_data = range(0,len(x_test))

y_data1 = y_test

#print(len(y_data1))

y_data2 = test_pre

plt.figure(figsize=(20,8),dpi=80)

plt.plot(x_data,y_data1,label='實際值')

plt.plot(x_data,y_data2,label='**值')

plt.grid(alpha = 0.3)#網格,alpha為透明度

plt.ylabel("房價")

plt.legend()

plt.show()

##儲存**的值

#result =

#result_file = pd.dataframe(result)

#result_file.head()

#result_file.to_csv('test_pre_housing_true.csv')#儲存**的值

使用的sklearn的linearregression

波士頓房價資料集視覺化

將所有屬性與房價之間的關係視覺化 import matplotlib.pyplot as plt import numpy as np import tensorflow as tf plt.rcparams font.sans serif microsoft yahei 指定預設字型 plt.rc...

Python 波士頓房價資料主成分PCA分析降維

import pandas as pd import matplotlib.pyplot as plt import numpy as np from sklearn import datasets,metrics from sklearn.linear model import linearreg...

使用sklearn載入波士頓房價資料集

使用sklearn載入波士頓房價資料集 從sklearn匯入資料集 from sklearn.datasets import load boston載入資料 boston load boston x為輸入,y為輸出 x boston.data y boston.target檢視資料有哪些字段 載入的...