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
  • (一)引入函式庫及內建波士頓房地產資料庫
  • (二)SVR的使用
  • (三)使用joblib.dump匯出預測器
  • (四)訓練以及分類
  • (五)使用score計算準確率
  • (六)繪出預測結果與實際目標差異圖
  • (六)完整程式碼
  1. 應用範例 Application

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

支持向量機回歸分析: Property value prediction

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

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

引入之函式庫如下

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

  2. sklearn.SVR: 支持向量機回歸分析之演算法

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

from sklearn import datasets
from sklearn.svm import SVR
import matplotlib.pyplot as plt

boston = datasets.load_boston()
X=boston.data
y = boston.target

使用 datasets.load_boston() 將資料存入至boston。 使用datasets.data將士頓房地產資料的數據資料(data)匯入到X。 使用datasets.target將士頓房地產資料的預測數值匯入到y。 為一個dict型別資料,我們看一下資料的內容。

(二)SVR的使用

sklearn.svm.SVR(kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=0.001, C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False, max_iter=-1)

clf = SVR(kernel='rbf', C=1e3, gamma=0.1)
clf.fit(X, y)

使用clf = SVR(kernel='rbf', C=1e3, gamma=0.1),將SVR演算法引入到clf,並設定SVR演算法的參數。 使用clf.fit(X, y),用波士頓房地產數據(boston.data)以及預測目標(y)來訓練預測機clf

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

from sklearn.externals import joblib
joblib.dump(clf,"./machine_SVR.pkl")

使用joblib.dump將SVR預測器匯出為pkl檔。

(四)訓練以及分類

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

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

(五)使用score計算準確率

先用predict=clf.predict(X)將所有波士頓房地產數據丟入clf預測機預測,並將所預測出的結果存入predict。接著使用clf.score(X, y)來計算準確率,score=1為最理想情況,本範例中score=0.99988275378631286

predict=clf.predict(X)
clf.score(X, y)

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

X軸為預測結果,Y軸為回歸目標。 並劃出一條斜率=1的理想曲線(用虛線標示)。 紅點為房地產第三項數據的預測結果

因為使用clf的準確率很高,所以預測結果與回歸目標幾乎一樣,scatter的點會幾乎都在理想曲線上。

plt.scatter(predict,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.svm import SVR
import matplotlib.pyplot as plt

boston = datasets.load_boston()
X=boston.data
y = boston.target
clf = SVR(kernel='rbf', C=1e3, gamma=0.1)
clf.fit(X, y)
from sklearn.externals import joblib
joblib.dump(clf,"./machine_SVR.pkl")
clf=joblib.load("./machine_SVR.pkl")
clf.fit(boston.data, y)
predict_y=clf.predict(boston.data[2])
predict=clf.predict(X)
clf.score(X, y)
plt.scatter(predict,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類神經網路 Neural_Networks

Last updated 6 years ago