machine-learning
  • 機器學習:使用Python
    • 簡介Scikit-learn 機器學習
  • 分類法 Classification
    • Ex 1: Recognizing hand-written digits
    • EX 2: Normal and Shrinkage Linear Discriminant Analysis for classification
    • EX 3: Plot classification probability
    • EX 4: Classifier Comparison
    • EX 5: Linear and Quadratic Discriminant Analysis with confidence ellipsoid
  • 特徵選擇 Feature Selection
    • Ex 1: Pipeline Anova SVM
    • Ex 2: Recursive Feature Elimination
    • Ex 3: Recursive Feature Elimination with Cross-Validation
    • Ex 4: Feature Selection using SelectFromModel
    • Ex 5: Test with permutations the significance of a classification score
    • Ex 6: Univariate Feature Selection
    • Ex 7: Comparison of F-test and mutual information
  • 互分解 Cross Decomposition
  • 通用範例 General Examples
    • Ex 1: Plotting Cross-Validated Predictions
    • Ex 2: Concatenating multiple feature extraction methods
    • Ex 3: Isotonic Regression
    • Ex 4: Imputing missing values before building an estimator
    • Ex 5: ROC Curve with Visualization API
    • Ex 7: Face completion with a multi-output estimators
  • 群聚法 Clustering
    • EX 1: Feature_agglomeration.md
    • EX 2: Mean-shift 群聚法.md
    • EX 6: 以群聚法切割錢幣影像.md
    • EX 10:_K-means群聚法
    • EX 12: Spectral clustering for image segmentation
    • Plot Hierarchical Clustering Dendrogram
  • 支持向量機
    • EX 1:Non_linear_SVM.md
    • [EX 4: SVM_with _custom _kernel.md](SVM/EX4_SVM_with _custom _kernel.md)
  • 機器學習資料集 Datasets
    • Ex 1: The digits 手寫數字辨識
    • Ex 2: Plot randomly generated classification dataset 分類數據集
    • Ex 3: The iris 鳶尾花資料集
    • Ex 4: Plot randomly generated multilabel dataset 多標籤數據集
  • 應用範例 Application
    • 用特徵臉及SVM進行人臉辨識實例
    • 維基百科主要的特徵向量
    • 波士頓房地產雲端評估(一)
    • 波士頓房地產雲端評估(二)
  • 類神經網路 Neural_Networks
    • Ex 1: Visualization of MLP weights on MNIST
    • Ex 2: Restricted Boltzmann Machine features for digit classification
    • Ex 3: Compare Stochastic learning strategies for MLPClassifier
    • Ex 4: Varying regularization in Multi-layer Perceptron
  • 決策樹 Decision_trees
    • Ex 1: Decision Tree Regression
    • Ex 2: Multi-output Decision Tree Regression
    • Ex 3: Plot the decision surface of a decision tree on the iris dataset
    • Ex 4: Understanding the decision tree structure
  • 機器學習:使用 NVIDIA JetsonTX2
    • 從零開始
    • 讓 TX2 動起來
    • 安裝OpenCV
    • 安裝TensorFlow
  • 廣義線性模型 Generalized Linear Models
    • Ex 3: SGD: Maximum margin separating hyperplane
  • 模型選擇 Model Selection
    • Ex 3: Plotting Validation Curves
    • Ex 4: Underfitting vs. Overfitting
  • 半監督式分類法 Semi-Supervised Classification
    • Ex 3: Label Propagation digits: Demonstrating performance
    • Ex 4: Label Propagation digits active learning
    • Decision boundary of label propagation versus SVM on the Iris dataset
  • Ensemble_methods
    • IsolationForest example
  • Miscellaneous_examples
    • Multilabel classification
  • Nearest_Neighbors
    • Nearest Neighbors Classification
Powered by GitBook
On this page
  • 線性回歸分析: Property value prediction
  • (一)引入函式庫及內建波士頓房地產資料庫
  • (二)cross_val_predict的使用
  • (三)使用joblib.dump匯出預測器
  • (四)訓練以及分類
  • (五)繪出預測結果與實際目標差異圖
  • (六)完整程式碼
  1. 應用範例 Application

波士頓房地產雲端評估(一)

線性回歸分析: Property value prediction

此檔案使用scikit-learn 機器學習套件裡的linear regression演算法,來達成波士頓房地產價錢預測

  1. 資料集:波士頓房產

  2. 特徵:房地產客觀數據,如年份、平面大小

  3. 預測目標:房地產價格

  4. 機器學習方法:線性迴歸

  5. 探討重點:10 等分的交叉驗証(10-fold Cross-Validation)來實際測試資料以及預測值的關係

  6. 關鍵函式: sklearn.cross_validation.cross_val_predict;joblib.dump;joblib.load

(一)引入函式庫及內建波士頓房地產資料庫

引入之函式庫如下

  1. sklearn.datasets: 用來匯入內建之波士頓房地產資料庫

  2. sklearn.cross_val_predict: 使用交叉驗證用來評估辨識準確度

  3. sklearn.linear_model: 線性分析之模組

  4. matplotlib.pyplot: 用來繪製影像

from sklearn import datasets
from sklearn.cross_validation import cross_val_predict
from sklearn import linear_model
import matplotlib.pyplot as plt

lr = linear_model.LinearRegression()
# The boston dataset
boston = datasets.load_boston()
y = boston.target

使用linear_model.LinearRegression()將線性迴歸分析演算法引入到lr。 使用datasets.target將士頓房地產資料的預測數值匯入到y。 使用 datasets.load_boston() 將資料存入, boston 為一個dict型別資料,我們看一下資料的內容。

顯示

說明

('data', (506, 13))

房地產的資料集,共506筆房產13個特徵

('feature_names', (13,))

房地產的特徵名

('target', (506,))

回歸目標

DESCR

資料之描述

(二)cross_val_predict的使用

sklearn.cross_validation.cross_val_predict(estimator, X, y=None, cv=None, n_jobs=1, verbose=0, fit_params=None, pre_dispatch='2*n_jobs')

X為機器學習數據, y為回歸目標, cv為交叉驗証時資料切分的依據,範例為10則將資料切分為10等分,以其中9等分為訓練集,另外一等分則為測試集。

predicted = cross_val_predict(lr, boston.data, y, cv=10)

(三)使用joblib.dump匯出預測器

from sklearn.externals import joblib

joblib.dump(lr,"./lr_machine.pkl")

使用joblib.dump將線性回歸預測器匯出為pkl檔。

(四)訓練以及分類

接著使用lr=joblib.load("./lr_machine.pkl")將pkl檔匯入為一個linear regression預測器lr。接著使用波士頓房地產數據(boston.data),以及預測目標(y)來訓練預測機lr lr.fit(boston.data, y)。最後,使用predict_y=lr.predict(boston.data[2])預測第三筆資料的價格,並將結果存入predicted_y變數。

lr=joblib.load("./lr_machine.pkl")
lr.fit(boston.data, y)
predict_y=lr.predict(boston.data[2])

(五)繪出預測結果與實際目標差異圖

X軸為預測結果,Y軸為回歸目標。 並劃出一條斜率=1的理想曲線(用虛線標示)。

紅點為房地產第三項數據的預測結果。

plt.scatter(predicted,y,s=2)
plt.plot(predict_y, predict_y, 'ro')
plt.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=2)
plt.xlabel('Predicted')
plt.ylabel('Measured')

(六)完整程式碼

%matplotlib inline
from sklearn import datasets
from sklearn.cross_validation import cross_val_predict
from sklearn import linear_model
import matplotlib.pyplot as plt

lr = linear_model.LinearRegression()
boston = datasets.load_boston()
y = boston.target
# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validated:
predicted = cross_val_predict(lr, boston.data, y, cv=10)
from sklearn.externals import joblib

joblib.dump(lr,"./lr_machine.pkl")
lr=joblib.load("./lr_machine.pkl")
lr.fit(boston.data, y)
predict_y=lr.predict(boston.data[2])
plt.scatter(predicted,y,s=2)
plt.plot(predict_y, predict_y, 'ro')
plt.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=2)
plt.xlabel('Predicted')
plt.ylabel('Measured')
Previous維基百科主要的特徵向量Next波士頓房地產雲端評估(二)

Last updated 6 years ago