使用skflow內建的LR,DNN,Scikit-learn中的整合迴歸模型對“美國波士頓樓價”進行預測

來源:互聯網
上載者:User

話不多說,直接上代碼

1 代碼實現及結果截圖、

#coding:utf-8
#使用skflow內建的LR,DNN,Scikit-learn中的整合迴歸模型對“美國波士頓樓價”進行預測
from sklearn import datasets,metrics,preprocessing,cross_validation
#讀取資料
boston=datasets.load_boston()
#擷取樓價資料特徵及對應樓價
x,y=boston.data,boston.target
#資料分割,25%做測試
x_train,x_test,y_train,y_test=cross_validation.train_test_split(x,y,test_size=0.25,random_state=33)
#對資料特徵進行標準化處理
scaler=preprocessing.StandardScaler()
x_train=scaler.fit_transform(x_train)
x_test=scaler.transform(x_test)

import skflow
tf_lr=skflow.TensorFlowLinearRegressor(steps=10000,learning_rate=0.01,batch_size=50)
tf_lr.fit(x_train, y_train)
tf_lr_y_predict=tf_lr.predict(x_test)
#輸出效能
print 'LR'
print 'absoluate:',metrics.mean_absolute_error(tf_lr_y_predict, y_test)
print 'squared:',metrics.mean_squared_error(tf_lr_y_predict, y_test)
print 'R-squared:',metrics.r2_score(tf_lr_y_predict, y_test)


tf_dnn_regressor=skflow.TensorFlowDNNRegressor(hidden_units=[100,40],steps=10000,learning_rate=0.01,batch_size=50)
tf_dnn_regressor.fit(x_train, y_train)
tf_dnn_regressor_y_predict=tf_dnn_regressor.predict(x_test)
print 'DNN'
print 'absoluate:',metrics.mean_absolute_error(tf_dnn_regressor_y_predict, y_test)
print 'squared:',metrics.mean_squared_error(tf_dnn_regressor_y_predict, y_test)

print 'R-squared:',metrics.r2_score(tf_dnn_regressor_y_predict, y_test)




from sklearn.ensemble import RandomForestRegressor
rfr=RandomForestRegressor()
rfr.fit(x_train,y_train)
rfr_y_predict=rfr.predict(x_test)
print 'Scikit-learn'
print 'absoluate:',metrics.mean_absolute_error(rfr_y_predict, y_test)
print 'squared:',metrics.mean_squared_error(rfr_y_predict, y_test)

print 'R-squared:',metrics.r2_score(rfr_y_predict, y_test)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.