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
  • 通用範例/範例一: Plotting Cross-Validated Predictions
  • (一)引入函式庫及內建測試資料庫
  • (二)引入內建測試資料庫(boston房產資料)
  • (三)cross_val_predict的使用
  • (四)繪出預測結果與實際目標差異圖
  • (五)完整程式碼
  1. 通用範例 General Examples

Ex 1: Plotting Cross-Validated Predictions

Previous通用範例 General ExamplesNextEx 2: Concatenating multiple feature extraction methods

Last updated 6 years ago

通用範例/範例一: Plotting Cross-Validated Predictions

  1. 資料集:波士頓房產

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

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

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

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

  6. 關鍵函式: sklearn.cross_validation.cross_val_predict

(一)引入函式庫及內建測試資料庫

引入之函式庫如下

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

  2. sklearn.datasets: 用來繪入內建測試資料庫

  3. sklearn.cross_validation import cross_val_predict:利用交叉驗證的方式來預測

  4. sklearn.linear_model:使用線性迴歸

(二)引入內建測試資料庫(boston房產資料)

使用 datasets.load_boston() 將資料存入, boston 為一個dict型別資料,我們看一下資料的內容。

lr = linear_model.LinearRegression()
#lr = LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
boston = datasets.load_boston()
y = boston.target

顯示

說明

('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)

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

X軸為回歸目標,Y軸為預測結果。

並劃出一條斜率=1的理想曲線(用虛線標示)

fig, ax = plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()

(五)完整程式碼

Python source code: plot_cv_predict.py

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)

fig, ax = plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()

http://scikit-learn.org/stable/auto_examples/plot_cv_predict.html
http://scikit-learn.org/stable/auto_examples/plot_cv_predict.html