決策樹(Decision tree)與隨機森林(Random Forest)_葡萄酒資料集sklearn測試

 

決策樹和隨機森林都是機器學習中常用的模型,用於解決分類和回歸問題。讓我們分別解釋它們的概念,並舉例說明它們的應用時機。

決策樹

決策樹是一種機器學習模型,用於進行分類和預測。它的工作方式類似於一個樹狀結構,其中每個節點表示一個特徵,每個分支代表一個特徵的值,而每個葉子節點表示一個類別或預測值。在決策樹中,我們通過不斷地問基於特徵的問題來進行預測,直到達到葉子節點並得到預測結果。

決策樹在以下情況下特別有用:

當我們需要解釋模型的決策過程時,決策樹是一個很好的選擇,因為它的結構易於理解。
當我們的數據集包含混合類型的特徵(數值、類別等)時,決策樹能夠處理這種多樣性。
當我們需要進行特徵選擇時,決策樹可以告訴我們哪些特徵對預測最重要
舉例: 假設我們要建立一個模型來預測一個網站上的用戶是否會購買產品。我們有許多用戶特徵,包括年齡、性別、瀏覽次數等。我們可以使用決策樹模型來根據這些特徵來預測每位用戶的購買行為。如果決策樹的分支顯示性別是最重要的特徵,那麼我們可以得出結論,該網站上的用戶購買行為與性別高度相關。











https://blog.csdn.net/FlatTiger/article/details/111040141




https://medium.com/@whchang022/%E9%9A%A8%E6%A9%9F%E6%A3%AE%E6%9E%97-random-forest-a939adfa96de

隨機森林

隨機森林是一種集成學習方法,它基於多個決策樹的預測結果來進行最終預測。
隨機森林通常由許多決策樹組成,每棵樹都使用不同的隨機子集數據進行訓練。
一個隨機森林本質上是決策樹的集合,每一棵樹都長得不同。
每棵樹可能做相對好的預測但也可能因部分資料而有過度擬合情況。

最後,隨機森林將每棵樹的預測結果進行平均或投票,以提供最終的預測。
若我們建立很多棵樹而這些樹都運作良好,就能以不同方式過度擬合。
可透過平均它們結果來減少過度擬合的數量,藉由保留樹的預測能力減少過度擬合。

應用時機: 隨機森林在以下情況下特別有用:

當我們需要構建一個具有高預測準確度的模型時,隨機森林通常比單個決策樹效果更好
當我們的數據集具有高度噪聲或包含大量特徵時,隨機森林通常能夠處理這種複雜性。
當我們希望評估特徵的重要性或進行特徵選擇時,隨機森林提供了有價值的信息。
舉例: 假設我們要預測一位患者是否患有某種疾病,我們有大量的醫學數據,包括病歷、生理數值、基因信息等。我們可以使用隨機森林模型,它可以考慮所有這些數據並提供高度準確的預測。此外,隨機森林可以告訴我們哪些特徵對於疾病預測最重要,有助於醫生更好地理解疾病的風險因素。



在sklearn中提供很多內建資料集可幫助我們來測試跟學習

用於載入葡萄酒資料集(Wine Dataset)。
此資料集包含不同種類葡萄酒的化學特徵,是用於分類問題的經典範例。

一些認識資料集起手式


查看資料集的特徵(屬性)
傳回一個清單名稱,包含了資料集中的特徵,每個特徵對應葡萄酒的不同化學成分,例如酒精濃度、蘋果酸濃度等。


查看資料集的標籤(目標):
傳回一個包含 NumPy 倉庫的樣本標籤,標籤表示每個樣本的葡萄酒種類。


查看資料集的描述
輸出一個包含了資料集描述的詳細文本,包括資料集的來源、特徵、統計資料等。


程式

1
2
3
4
5
6
from sklearn.datasets import load_wine

wine_data = load_wine()
print(wine_data.feature_names)
print(wine_data.target)
print(wine_data.DESCR)




資料集描述

.. _wine_dataset:

Wine recognition dataset
------------------------

**Data Set Characteristics:**

    :Number of Instances: 178
    :Number of Attributes: 13 numeric, predictive attributes and the class
    :Attribute Information:
 		- Alcohol
 		- Malic acid
 		- Ash
		- Alcalinity of ash  
 		- Magnesium
		- Total phenols
 		- Flavanoids
 		- Nonflavanoid phenols
 		- Proanthocyanins
		- Color intensity
 		- Hue
 		- OD280/OD315 of diluted wines
 		- Proline

    - class:
            - class_0
            - class_1
            - class_2
		
    :Summary Statistics:
    
    ============================= ==== ===== ======= =====
                                   Min   Max   Mean     SD
    ============================= ==== ===== ======= =====
    Alcohol:                      11.0  14.8    13.0   0.8
    Malic Acid:                   0.74  5.80    2.34  1.12
    Ash:                          1.36  3.23    2.36  0.27
    Alcalinity of Ash:            10.6  30.0    19.5   3.3
    Magnesium:                    70.0 162.0    99.7  14.3
    Total Phenols:                0.98  3.88    2.29  0.63
    Flavanoids:                   0.34  5.08    2.03  1.00
    Nonflavanoid Phenols:         0.13  0.66    0.36  0.12
    Proanthocyanins:              0.41  3.58    1.59  0.57
    Colour Intensity:              1.3  13.0     5.1   2.3
    Hue:                          0.48  1.71    0.96  0.23
    OD280/OD315 of diluted wines: 1.27  4.00    2.61  0.71
    Proline:                       278  1680     746   315
    ============================= ==== ===== ======= =====

    :Missing Attribute Values: None
    :Class Distribution: class_0 (59), class_1 (71), class_2 (48)
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

This is a copy of UCI ML Wine recognition datasets.
https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data

The data is the results of a chemical analysis of wines grown in the same
region in Italy by three different cultivators. There are thirteen different
measurements taken for different constituents found in the three types of
wine.

Original Owners: 

Forina, M. et al, PARVUS - 
An Extendible Package for Data Exploration, Classification and Correlation. 
Institute of Pharmaceutical and Food Analysis and Technologies,
Via Brigata Salerno, 16147 Genoa, Italy.

Citation:

Lichman, M. (2013). UCI Machine Learning Repository
[https://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
School of Information and Computer Science. 

.. topic:: References

  (1) S. Aeberhard, D. Coomans and O. de Vel, 
  Comparison of Classifiers in High Dimensional Settings, 
  Tech. Rep. no. 92-02, (1992), Dept. of Computer Science and Dept. of  
  Mathematics and Statistics, James Cook University of North Queensland. 
  (Also submitted to Technometrics). 

  The data was used with many others for comparing various 
  classifiers. The classes are separable, though only RDA 
  has achieved 100% correct classification. 
  (RDA : 100%, QDA 99.4%, LDA 98.9%, 1NN 96.1% (z-transformed data)) 
  (All results using the leave-one-out technique) 

  (2) S. Aeberhard, D. Coomans and O. de Vel, 
  "THE CLASSIFICATION PERFORMANCE OF RDA" 
  Tech. Rep. no. 92-01, (1992), Dept. of Computer Science and Dept. of 
  Mathematics and Statistics, James Cook University of North Queensland. 
  (Also submitted to Journal of Chemometrics).

- 實例數量:178
- 屬性數量:13個數值型的預測屬性以及一個類別屬性(目標)

屬性資訊:

  • 酒精濃度(酒精)
  • 蘋果酸濃度(蘋果酸)
  • 灰分(Ash)
  • 灰的
  • 鎂(鎂)
  • 總酚(總酚)
  • 類黃酮(Flavanoids)
  • 非類黃酮
  • 原花青素(原花青素)
  • 色彩強度(色彩強度)
  • 色調(Hue)
  • 稀釋酒的OD280/OD315值(稀釋酒的OD280/OD315)
  • 脯胺酸(脯胺酸)

類別:

  • 類別_0
  • 類別_1
  • 類別_2

匯總統計資訊:

  • 資料集包括上述13個屬性,並提供相關統計資料

產出值:

  • 資料集中沒有欠缺的屬性值。

類別分佈:

  • class_0:59個實例
  • class_1:71個實例
  • class_2:48個實例

隨機森林(Random Forest)_葡萄酒分類

from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score


wine_data = load_wine()
X_train, X_test, y_train, y_test = train_test_split(wine_data.data, wine_data.target, test_size=0.3)

model = RandomForestClassifier() 
model.fit(X_train, y_train) 
y_pred = model.predict(X_test) 
acc = accuracy_score(y_pred, y_test) 
print(y_pred)
print(acc)






留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題