回歸分析就是用于預(yù)測輸入變量(自變量)和輸出變量(因變量)之間的關(guān)系,特別當(dāng)輸入的值發(fā)生變化時,輸出變量值也發(fā)生改變!回歸簡單來說就是對數(shù)據(jù)進行擬合。線性回歸就是通過線性的函數(shù)對數(shù)據(jù)進行擬合。機器學(xué)習(xí)并不能實現(xiàn)預(yù)言,只能實現(xiàn)簡單的預(yù)測。我們這次對房價關(guān)于其他因素的關(guān)系。
定日網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
wget.download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data',out= 'housing.data')
wget.download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.names',out='housing.names')
wget.download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/housing/Index',out='Index')
feature_names = ['CRIM','ZN','INDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT','MEDV']
feature_num = len(feature_names)
print(feature_num)
# 把7084 變?yōu)?06*14
housing_data = housing_data.reshape(housing_data.shape[0]//feature_num,feature_num)
print(housing_data.shape[0])
# 打印第一行數(shù)據(jù)
print(housing_data[:1])
## 歸一化
feature_max = housing_data.max(axis=0)
feature_min = housing_data.min(axis=0)
feature_avg = housing_data.sum(axis=0)/housing_data.shape[0]
## 實例化模型
def Model():
model = linear_model.LinearRegression()
return model
# 擬合模型
def train(model,x,y):
model.fit(x,y)
def draw_infer_result(groud_truths,infer_results):
title = 'Boston'
plt.title(title,fontsize=24)
x = np.arange(1,40)
y = x
plt.plot(x,y)
plt.xlabel('groud_truth')
plt.ylabel('infer_results')
plt.scatter(groud_truths,infer_results,edgecolors='green',label='training cost')
plt.grid()
plt.show()
## 基于線性回歸實現(xiàn)房價預(yù)測
## 擬合函數(shù)模型
## 梯度下降方法
## 開源房價策略數(shù)據(jù)集
import wget
import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import linear_model
## 下載之后注釋掉
'''
wget.download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data',out= 'housing.data')
wget.download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.names',out='housing.names')
wget.download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/housing/Index',out='Index')
'''
'''
1. CRIM per capita crime rate by town
2. ZN proportion of residential land zoned for lots over
25,000 sq.ft.
3. INDUS proportion of non-retail business acres per town
4. CHAS Charles River dummy variable (= 1 if tract bounds
river; 0 otherwise)
5. NOX nitric oxides concentration (parts per 10 million)
6. RM average number of rooms per dwelling
7. AGE proportion of owner-occupied units built prior to 1940
8. DIS weighted distances to five Boston employment centres
9. RAD index of accessibility to radial highways
10. TAX full-value property-tax rate per $10,000
11. PTRATIO pupil-teacher ratio by town
12. B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks
by town
13. LSTAT % lower status of the population
14. MEDV Median value of owner-occupied homes in $1000's
'''
## 數(shù)據(jù)加載
datafile = './housing.data'
housing_data = np.fromfile(datafile,sep=' ')
print(housing_data.shape)
feature_names = ['CRIM','ZN','INDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT','MEDV']
feature_num = len(feature_names)
print(feature_num)
# 把7084 變?yōu)?06*14
housing_data = housing_data.reshape(housing_data.shape[0]//feature_num,feature_num)
print(housing_data.shape[0])
# 打印第一行數(shù)據(jù)
print(housing_data[:1])
## 歸一化
feature_max = housing_data.max(axis=0)
feature_min = housing_data.min(axis=0)
feature_avg = housing_data.sum(axis=0)/housing_data.shape[0]
def feature_norm(input):
f_size = input.shape
output_features = np.zeros(f_size,np.float32)
for batch_id in range(f_size[0]):
for index in range(13):
output_features[batch_id][index] = (input[batch_id][index]-feature_avg[index])/(feature_max[index]-feature_min[index])
return output_features
housing_features = feature_norm(housing_data[:,:13])
housing_data = np.c_[housing_features,housing_data[:,-1]].astype(np.float32)
## 劃分?jǐn)?shù)據(jù)集 8:2
ratio =0.8
offset = int(housing_data.shape[0]*ratio)
train_data = housing_data[:offset]
test_data = housing_data[offset:]
print(train_data[:2])
## 模型配置
## 線性回歸
## 實例化模型
def Model():
model = linear_model.LinearRegression()
return model
# 擬合模型
def train(model,x,y):
model.fit(x,y)
## 模型訓(xùn)練
X, y = train_data[:,:13], train_data[:,-1:]
model = Model()
train(model,X,y)
x_test, y_test = test_data[:,:13], test_data[:,-1:]
prefict = model.predict(x_test)
## 模型評估
infer_results = []
groud_truths = []
def draw_infer_result(groud_truths,infer_results):
title = 'Boston'
plt.title(title,fontsize=24)
x = np.arange(1,40)
y = x
plt.plot(x,y)
plt.xlabel('groud_truth')
plt.ylabel('infer_results')
plt.scatter(groud_truths,infer_results,edgecolors='green',label='training cost')
plt.grid()
plt.show()
draw_infer_result(y_test,prefict)
線性回歸預(yù)測還是比較簡單的,可以簡單理解為函數(shù)擬合,數(shù)據(jù)集是使用的開源的波士頓房價的數(shù)據(jù)集,算法也是打包好的包,方便我們引用。