1. 程式人生 > >多分類問題決策樹資料分析-大資料ML樣本集案例實戰

多分類問題決策樹資料分析-大資料ML樣本集案例實戰

版權宣告:本套技術專欄是作者(秦凱新)平時工作的總結和昇華,通過從真實商業環境抽取案例進行總結和分享,並給出商業應用的調優建議和叢集環境容量規劃等內容,請持續關注本套部落格。QQ郵箱地址:[email protected],如有任何學術交流,可隨時聯絡。

1 資料預處理

  • DF加上表頭

      5.1,3.5,1.4,0.2,Iris-setosa
      4.9,3.0,1.4,0.2,Iris-setosa
      4.7,3.2,1.3,0.2,Iris-setosa
      4.6,3.1,1.5,0.2,Iris-setosa
      5.0,3.6,1.4,0.2,Iris-setosa
      5.4,3.9,1.7,0.4,Iris-setosa
      4.6,3.4,1.4,0.3,Iris-setosa
    
      import pandas as pd
      import matplotlib.pyplot as plt
      import numpy as np
      iris_data = pd.read_csv('C:\\ML\\MLData\\iris.data')
      iris_data.columns = ['sepal_length_cm', 'sepal_width_cm', 'petal_length_cm', 'petal_width_cm', 'class']
      iris_data.head()
    複製程式碼

  • 讀取圖片

      from PIL import Image
      img=Image.open('test.jpg')
      plt.imshow(img)
      plt.show()
    複製程式碼

  • 數值描述(數值區間)

      iris_data.describe()
    複製程式碼

  • 高階視覺化庫pairplot

      %matplotlib inline
      
      import matplotlib.pyplot as plt
      import seaborn as sb
      sb.pairplot(iris_data.dropna(), hue='class')
    複製程式碼

  • 高階視覺化庫 violinplot分佈範圍(花瓣相對可以區分出不同特徵)

      plt.figure(figsize=(10, 10))
      for column_index, column in enumerate(iris_data.columns):
          if column == 'class':
              continue
          plt.subplot(2, 2, column_index + 1)
          sb.violinplot(x='class', y=column, data=iris_data)
    複製程式碼

  • 版權宣告:本套技術專欄是作者(秦凱新)平時工作的總結和昇華,通過從真實商業環境抽取案例進行總結和分享,並給出商業應用的調優建議和叢集環境容量規劃等內容,請持續關注本套部落格。QQ郵箱地址:[email protected],如有任何學術交流,可隨時聯絡。

2 構造分類器(sklearn.cross_validation過期)

  • 測試集與訓練集

      from sklearn.model_selection import KFold
      from sklearn.model_selection import train_test_split
      
      all_inputs = iris_data[['sepal_length_cm', 'sepal_width_cm',
                                   'petal_length_cm', 'petal_width_cm']].values
      
      all_classes = iris_data['class'].values
      
       (training_inputs,
       testing_inputs,
       training_classes,
       testing_classes) = train_test_split(all_inputs, all_classes, train_size=0.75, random_state=1)
    複製程式碼
  • 引數設定詳解

      from sklearn.tree import DecisionTreeClassifier
      
      #  1.criterion  gini  or  entropy(基於gini係數和熵值來指定)
      
      #  2.splitter  best or random 前者是在所有特徵中找最好的切分點 後者是在部分特徵中(資料量大的時候)
      
      #  3.max_features  None(所有) 特徵小於50的時候一般使用所有的 ,log2,sqrt,N  
      
      #  4.max_depth  資料少或者特徵少的時候可以不管這個值,如果模型樣本量多,特徵也多的情況下,可以嘗試限制下
      
      #  5.min_samples_split  如果某節點的樣本數少於min_samples_split,則不會繼續再嘗試選擇最優特徵來進行劃分
      #                       如果樣本量不大,不需要管這個值。如果樣本量數量級非常大,則推薦增大這個值。
      
      #  6.min_samples_leaf  這個值限制了葉子節點最少的樣本數,如果某葉子節點數目小於樣本數,則會和兄弟節點一起被
      #                      剪枝,如果樣本量不大,不需要管這個值,大些如10W可是嘗試下5
      
      #  7.min_weight_fraction_leaf 這個值限制了葉子節點所有樣本權重和的最小值,如果小於這個值,則會和兄弟節點一起
      #                          被剪枝預設是0,就是不考慮權重問題。一般來說,如果我們有較多樣本有缺失值,
      #                          或者分類樹樣本的分佈類別偏差很大,就會引入樣本權重,這時我們就要注意這個值了。
      
      #  8.max_leaf_nodes 通過限制最大葉子節點數,可以防止過擬合,預設是"None”,即不限制最大的葉子節點數。
      #                   如果加了限制,演算法會建立在最大葉子節點數內最優的決策樹。
      #                   如果特徵不多,可以不考慮這個值,但是如果特徵分成多的話,可以加以限制
      #                   具體的值可以通過交叉驗證得到。
      
      #  9.class_weight 指定樣本各類別的的權重,主要是為了防止訓練集某些類別的樣本過多
      #                 導致訓練的決策樹過於偏向這些類別。這裡可以自己指定各個樣本的權重
      #                 如果使用“balanced”,則演算法會自己計算權重,樣本量少的類別所對應的樣本權重會高。
      
      #  10.min_impurity_split 這個值限制了決策樹的增長,如果某節點的不純度
      #                       (基尼係數,資訊增益,均方差,絕對差)小於這個閾值
      #                       則該節點不再生成子節點。即為葉子節點 。
      
      decision_tree_classifier = DecisionTreeClassifier()
      
      # Train the classifier on the training set
      decision_tree_classifier.fit(training_inputs, training_classes)
      
      # Validate the classifier on the testing set using classification accuracy
      decision_tree_classifier.score(testing_inputs, testing_classes)
      
      0.9736842105263158
    複製程式碼
  • 版權宣告:本套技術專欄是作者(秦凱新)平時工作的總結和昇華,通過從真實商業環境抽取案例進行總結和分享,並給出商業應用的調優建議和叢集環境容量規劃等內容,請持續關注本套部落格。QQ郵箱地址:[email protected],如有任何學術交流,可隨時聯絡。

3 交叉驗證

from sklearn.model_selection import KFold

# 但目前train_test_split已被cross_validation被廢棄了
# 廢棄 from sklearn.cross_validation import cross_val_score

from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
import numpy as np

decision_tree_classifier = DecisionTreeClassifier()
# cross_val_score returns a list of the scores, which we can visualize
# to get a reasonable estimate of our classifier's performance
# 10倍交叉驗證
cv_scores = cross_val_score(decision_tree_classifier, all_inputs, all_classes, cv=10)
print (cv_scores)
#kde=False
sb.distplot(cv_scores)
plt.title('Average score: {}'.format(np.mean(cv_scores)))

[1.         0.93333333 1.         0.93333333 0.93333333 0.86666667
 0.93333333 0.93333333 1.         1.        ]
複製程式碼

decision_tree_classifier = DecisionTreeClassifier(max_depth=1)

cv_scores = cross_val_score(decision_tree_classifier, all_inputs, all_classes, cv=10)
print (cv_scores)
sb.distplot(cv_scores, kde=False)
plt.title('Average score: {}'.format(np.mean(cv_scores)))
複製程式碼

  • 4 引數網格

    from sklearn.model_selection import GridSearchCV
    from sklearn.model_selection import StratifiedKFold
    
    decision_tree_classifier = DecisionTreeClassifier()
    
    parameter_grid = {'max_depth': [1, 2, 3, 4, 5],
                      'max_features': [1, 2, 3, 4]}
    cross_validation = StratifiedKFold(10)
    
    grid_search = GridSearchCV(decision_tree_classifier,
                               param_grid=parameter_grid,
                               cv=cross_validation)
    
    grid_search.fit(all_inputs, all_classes)
    print('Best score: {}'.format(grid_search.best_score_))
    print('Best parameters: {}'.format(grid_search.best_params_))
    複製程式碼
  • 5 heatmap堆疊熱力圖使用

      grid_visualization = []
      
      for grid_pair in grid_search.cv_results_['mean_test_score']:
          grid_visualization.append(grid_pair)
          
      grid_visualization = np.array(grid_visualization)
      grid_visualization.shape = (5, 4)
      sb.heatmap(grid_visualization, cmap='Blues')
      plt.xticks(np.arange(4) + 0.5, grid_search.param_grid['max_features'])
      plt.yticks(np.arange(5) + 0.5, grid_search.param_grid['max_depth'][::-1])
      plt.xlabel('max_features')
      plt.ylabel('max_depth')
    複製程式碼

  • 6 生成決策樹iris_dtc.dot檔案

      import sklearn.tree as tree
      from sklearn.externals.six import StringIO
      
      with open('C:\\ML\\MLData\\iris_dtc.dot', 'w') as out_file:
          out_file = tree.export_graphviz(decision_tree_classifier, out_file=out_file)
    複製程式碼
  • 7 下載解析器

     http://www.graphviz.org/
      
     Graphviz is open source graph visualization software. Graph visualization is a way of representing
     structural information as diagrams of abstract graphs and networks. It has important applications in
     networking, bioinformatics,  software engineering, database and web design, machine learning, and in
     visual interfaces for other technical domains.  
    複製程式碼

dot -Tpdf iris_dtc.dot -o iris.pdf
複製程式碼

  • 8 多引數網格以及交叉驗證(最新版)

      from sklearn.ensemble import RandomForestClassifier
      from sklearn.model_selection import GridSearchCV
      from sklearn.model_selection import StratifiedKFold
      from sklearn.model_selection import KFold
      random_forest_classifier = RandomForestClassifier()
      
      parameter_grid = {'n_estimators': [5, 10, 25, 50],
                        'criterion': ['gini', 'entropy'],
                        'max_features': [1, 2, 3, 4],
                        'warm_start': [True, False]}
      
      cross_validation = StratifiedKFold(10)
      
      grid_search = GridSearchCV(random_forest_classifier,
                                 param_grid=parameter_grid,
                                 cv=cross_validation)
      
      grid_search.fit(all_inputs, all_classes)
      print('Best score: {}'.format(grid_search.best_score_))
      print('Best parameters: {}'.format(grid_search.best_params_))
      
      Best score: 0.9664429530201343
      Best parameters: {'criterion': 'gini', 'max_features': 2, 'n_estimators': 5, 'warm_start': False}
      
      grid_search.best_estimator_
      
      RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
          max_depth=None, max_features=2, max_leaf_nodes=None,
          min_impurity_decrease=0.0, min_impurity_split=None,
          min_samples_leaf=1, min_samples_split=2,
          min_weight_fraction_leaf=0.0, n_estimators=5, n_jobs=None,
          oob_score=False, random_state=None, verbose=0,
          warm_start=False)
    複製程式碼

4 總結

sklearn多引數網格和交叉驗證的使用,版本很重要,不然都執行不了。

版權宣告:本套技術專欄是作者(秦凱新)平時工作的總結和昇華,通過從真實商業環境抽取案例進行總結和分享,並給出商業應用的調優建議和叢集環境容量規劃等內容,請持續關注本套部落格。QQ郵箱地址:[email protected],如有任何學術交流,可隨時聯絡。

秦凱新 於深圳 201812082235