連淡水阿嬤都聽得懂的
機器學習入門
scikit-learn
Cicilia Lee 李佳穎
PyCon TW 2016/06/04
aacs0130@gmail.com
1
給門外漢的機器學習入門
描述 程度
Level 1 不知道什麼是機器學習 門外漢 (O)
Level 2 知道機器學習是AI的子學門
Level 3 會使用機器學習套件解問題 初學者
Level 4 會選擇適合的機器學習演算法與調參數
Level 5 知道機器學習演算法的數學原理 專家
Level 6 會設計新的機器學習演算法
2
Cicilia Lee @ PyCon TW 2016
大綱
1. 什麼是機器學習?
2. 機器學習的分類
3. 機器學習流程
4. Scikit-learn 範例
5. Scikit Learn 數字辨識步驟
6. 前處理
7. 選擇機器學習演算法 3
Cicilia Lee @ PyCon TW 2016
什麼是機器學習?
 我們有大量的樣本資料(sample data),讓機
器自動從中學習出規則,用來預測其他未知
的資料。
 機器學習是基於機率、統計、逼近論等數學
理論的研究。
 機器學習可應用於電腦視覺、自然語言處理、
語音和手寫識別和機器人等領域。
Cicilia Lee @ PyCon TW 2016
4
機器學習的分類
 Supervised Learning 監督式學習
 訓練集的目標是人為標註的。
 Classification 分類 : 預測類別
 Regression 回歸 : 預測變量
 Unsupervised Learning 非監督式學習
 訓練集沒有人為標註的目標。
 Clustering 分群
5
Cicilia Lee @ PyCon TW 2016
機器學習流程
Cicilia Lee @ PyCon TW 2016
6
Model 模型
Testing set
Scikit Learn 數字辨識範例
 這個範例用來展示scikit-learn 如何用SVM演
算法來達成手寫的數字辨識
 http://scikit-
learn.org/stable/auto_examples/classification/plot_digits_class
ification.html
Cicilia Lee @ PyCon TW 2016
7
Scikit Learn 數字辨識步驟
1. Load data
2. Set a classifier
3. Learn a model
4. Predict the result
5. Evaluate
Cicilia Lee @ PyCon TW 2016
8
Scikit Learn 數字辨識 (1/3)
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
### 1. Load data
# The digits dataset
digits = datasets.load_digits()
# To apply a classifier on this data, we need to flatten the image,
to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1)) 9
Cicilia Lee @ PyCon TW 2016
Scikit Learn 數字辨識 (2/3)
### 2. Set a classifier
# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)
### 3. Learn a model
# We learn the digits on the first half of the digits
classifier.fit(data[:n_samples / 2],
digits.target[:n_samples / 2])
10
Cicilia Lee @ PyCon TW 2016
Scikit Learn 數字辨識 (3/3)
### 4. Predict the result
# Now predict the value of the digit on the second half:
expected = digits.target[n_samples / 2:]
predicted = classifier.predict(data[n_samples / 2:])
### 5. Evaluate
print("Classification report for classifier %s:n%sn"
% (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:n%s"
% metrics.confusion_matrix(expected, predicted))
11
Cicilia Lee @ PyCon TW 2016
Script output (1/2)
Classification report for classifier SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma=0.001, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False):
precision recall f1-score support
0 1.00 0.99 0.99 88
1 0.99 0.97 0.98 91
2 0.99 0.99 0.99 86
3 0.98 0.87 0.92 91
4 0.99 0.96 0.97 92
5 0.95 0.97 0.96 91
6 0.99 0.99 0.99 91
7 0.96 0.99 0.97 89
8 0.94 1.00 0.97 88
9 0.93 0.98 0.95 92
avg / total 0.97 0.97 0.97 899
12
Cicilia Lee @ PyCon TW 2016
Script output (2/2)
Confusion matrix:
[[87 0 0 0 1 0 0 0 0 0]
[ 0 88 1 0 0 0 0 0 1 1]
[ 0 0 85 1 0 0 0 0 0 0]
[ 0 0 0 79 0 3 0 4 5 0]
[ 0 0 0 0 88 0 0 0 0 4]
[ 0 0 0 0 0 88 1 0 0 2]
[ 0 1 0 0 0 0 90 0 0 0]
[ 0 0 0 0 0 1 0 88 0 0]
[ 0 0 0 0 0 0 0 0 88 0]
[ 0 0 0 1 0 1 0 0 0 90]] 13
Cicilia Lee @ PyCon TW 2016
前處理
1. Clean data
2. Feature extraction
3. Convert category and string to number
4. Sparse data
5. Feature selection
14
Cicilia Lee @ PyCon TW 2016
選擇機器學習演算法
15
Cicilia Lee @ PyCon TW 2016
複習
1. 什麼是機器學習?
2. 機器學習的分類
3. 機器學習流程
4. Scikit-learn 範例
5. Scikit Learn 數字辨識步驟
6. 前處理
7. 選擇機器學習演算法 16
Cicilia Lee @ PyCon TW 2016
Thank you
 Reference
 Scikit-learn 官網:
http://scikit-learn.org/stable/index.html
 Scikit-learn數字範例
http://scikit-
learn.org/stable/auto_examples/classification/plot_digits_c
lassification.html
 選擇機器學習演算法
http://scikit-
learn.org/stable/tutorial/machine_learning_map/index.ht
ml
 林軒田教授的機器學習教學影片
https://www.youtube.com/playlist?list=PLXVfgk9fNX2I7tB
6oIINGBmW50rrmFTqf
Cicilia Lee @ PyCon TW 2016
17

連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn

  • 1.
  • 2.
    給門外漢的機器學習入門 描述 程度 Level 1不知道什麼是機器學習 門外漢 (O) Level 2 知道機器學習是AI的子學門 Level 3 會使用機器學習套件解問題 初學者 Level 4 會選擇適合的機器學習演算法與調參數 Level 5 知道機器學習演算法的數學原理 專家 Level 6 會設計新的機器學習演算法 2 Cicilia Lee @ PyCon TW 2016
  • 3.
    大綱 1. 什麼是機器學習? 2. 機器學習的分類 3.機器學習流程 4. Scikit-learn 範例 5. Scikit Learn 數字辨識步驟 6. 前處理 7. 選擇機器學習演算法 3 Cicilia Lee @ PyCon TW 2016
  • 4.
    什麼是機器學習?  我們有大量的樣本資料(sample data),讓機 器自動從中學習出規則,用來預測其他未知 的資料。 機器學習是基於機率、統計、逼近論等數學 理論的研究。  機器學習可應用於電腦視覺、自然語言處理、 語音和手寫識別和機器人等領域。 Cicilia Lee @ PyCon TW 2016 4
  • 5.
    機器學習的分類  Supervised Learning監督式學習  訓練集的目標是人為標註的。  Classification 分類 : 預測類別  Regression 回歸 : 預測變量  Unsupervised Learning 非監督式學習  訓練集沒有人為標註的目標。  Clustering 分群 5 Cicilia Lee @ PyCon TW 2016
  • 6.
    機器學習流程 Cicilia Lee @PyCon TW 2016 6 Model 模型 Testing set
  • 7.
    Scikit Learn 數字辨識範例 這個範例用來展示scikit-learn 如何用SVM演 算法來達成手寫的數字辨識  http://scikit- learn.org/stable/auto_examples/classification/plot_digits_class ification.html Cicilia Lee @ PyCon TW 2016 7
  • 8.
    Scikit Learn 數字辨識步驟 1.Load data 2. Set a classifier 3. Learn a model 4. Predict the result 5. Evaluate Cicilia Lee @ PyCon TW 2016 8
  • 9.
    Scikit Learn 數字辨識(1/3) # Import datasets, classifiers and performance metrics from sklearn import datasets, svm, metrics ### 1. Load data # The digits dataset digits = datasets.load_digits() # To apply a classifier on this data, we need to flatten the image, to # turn the data in a (samples, feature) matrix: n_samples = len(digits.images) data = digits.images.reshape((n_samples, -1)) 9 Cicilia Lee @ PyCon TW 2016
  • 10.
    Scikit Learn 數字辨識(2/3) ### 2. Set a classifier # Create a classifier: a support vector classifier classifier = svm.SVC(gamma=0.001) ### 3. Learn a model # We learn the digits on the first half of the digits classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2]) 10 Cicilia Lee @ PyCon TW 2016
  • 11.
    Scikit Learn 數字辨識(3/3) ### 4. Predict the result # Now predict the value of the digit on the second half: expected = digits.target[n_samples / 2:] predicted = classifier.predict(data[n_samples / 2:]) ### 5. Evaluate print("Classification report for classifier %s:n%sn" % (classifier, metrics.classification_report(expected, predicted))) print("Confusion matrix:n%s" % metrics.confusion_matrix(expected, predicted)) 11 Cicilia Lee @ PyCon TW 2016
  • 12.
    Script output (1/2) Classificationreport for classifier SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=None, degree=3, gamma=0.001, kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False): precision recall f1-score support 0 1.00 0.99 0.99 88 1 0.99 0.97 0.98 91 2 0.99 0.99 0.99 86 3 0.98 0.87 0.92 91 4 0.99 0.96 0.97 92 5 0.95 0.97 0.96 91 6 0.99 0.99 0.99 91 7 0.96 0.99 0.97 89 8 0.94 1.00 0.97 88 9 0.93 0.98 0.95 92 avg / total 0.97 0.97 0.97 899 12 Cicilia Lee @ PyCon TW 2016
  • 13.
    Script output (2/2) Confusionmatrix: [[87 0 0 0 1 0 0 0 0 0] [ 0 88 1 0 0 0 0 0 1 1] [ 0 0 85 1 0 0 0 0 0 0] [ 0 0 0 79 0 3 0 4 5 0] [ 0 0 0 0 88 0 0 0 0 4] [ 0 0 0 0 0 88 1 0 0 2] [ 0 1 0 0 0 0 90 0 0 0] [ 0 0 0 0 0 1 0 88 0 0] [ 0 0 0 0 0 0 0 0 88 0] [ 0 0 0 1 0 1 0 0 0 90]] 13 Cicilia Lee @ PyCon TW 2016
  • 14.
    前處理 1. Clean data 2.Feature extraction 3. Convert category and string to number 4. Sparse data 5. Feature selection 14 Cicilia Lee @ PyCon TW 2016
  • 15.
  • 16.
    複習 1. 什麼是機器學習? 2. 機器學習的分類 3.機器學習流程 4. Scikit-learn 範例 5. Scikit Learn 數字辨識步驟 6. 前處理 7. 選擇機器學習演算法 16 Cicilia Lee @ PyCon TW 2016
  • 17.
    Thank you  Reference Scikit-learn 官網: http://scikit-learn.org/stable/index.html  Scikit-learn數字範例 http://scikit- learn.org/stable/auto_examples/classification/plot_digits_c lassification.html  選擇機器學習演算法 http://scikit- learn.org/stable/tutorial/machine_learning_map/index.ht ml  林軒田教授的機器學習教學影片 https://www.youtube.com/playlist?list=PLXVfgk9fNX2I7tB 6oIINGBmW50rrmFTqf Cicilia Lee @ PyCon TW 2016 17