Scikit-learn 是 Python 中最風行的呆板進修庫之一,它供給了大年夜量的呆板進修演算法跟東西,可能幫助數據科學家跟開辟者疾速實現呆板進修項目。本文將深刻剖析 Scikit-learn 的核心功能,並經由過程實戰代碼示例展示怎樣利用該庫停止數據預處理、模型練習跟評價。
1. Scikit-learn 簡介
Scikit-learn 最初由 David Cournapeau 在 2007 年創建,它基於 Python 言語,並依附於 NumPy、SciPy 跟 matplotlib 等庫。Scikit-learn 供給了以下功能:
- 數據預處理:包含特徵提取、特徵抉擇、數據標準化跟歸一化等。
- 模型抉擇:供給了多種呆板進修演算法,如分類、回歸、聚類跟降維等。
- 模型評價:包含穿插驗證、混淆矩陣跟機能指標等。
- 模型調優:可能利用網格查抄跟隨機查抄等方法來優化模型參數。
2. 安裝 Scikit-learn
在開端利用 Scikit-learn 之前,須要確保曾經安裝了 Python 跟以下依附庫:
pip install numpy scipy matplotlib
pip install -U scikit-learn
3. 數據預處理
數據預處理是呆板進修項目中非常重要的一步,它包含數據清洗、特徵提取跟特徵抉擇等。
3.1 數據清洗
數據清洗平日包含處理缺掉值、處理異常值跟填充數據等。
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
# 載入數據集
iris = load_iris()
X, y = iris.data, iris.target
# 處理缺掉值
imputer = SimpleImputer(strategy='mean')
X = imputer.fit_transform(X)
# 分別練習集跟測試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
3.2 特徵提取
特徵提取是將原始數據轉換為更合適模型處理的情勢。
from sklearn.feature_extraction.text import TfidfVectorizer
# 假設有一個文本數據集
corpus = ['this is the first document.', 'this document is the second document.', 'and this is the third one.', 'is this the first document?']
# 利用 TF-IDF 停止特徵提取
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
3.3 特徵抉擇
特徵抉擇是從原始特徵中挑選出最有效的特徵。
from sklearn.feature_selection import SelectKBest, chi2
# 利用卡方測驗停止特徵抉擇
selector = SelectKBest(score_func=chi2, k=2)
X = selector.fit_transform(X, y)
4. 模型抉擇
Scikit-learn 供給了多種呆板進修演算法,以下是一些常用的分類跟回歸模型:
4.1 邏輯回歸
邏輯回歸用於二分類成績。
from sklearn.linear_model import LogisticRegression
# 創建邏輯回歸模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 評價模型
accuracy = model.score(X_test, y_test)
print(f"Accuracy: {accuracy}")
4.2 決定樹
決定樹用於分類跟回歸成績。
from sklearn.tree import DecisionTreeClassifier
# 創建決定樹範型
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
# 評價模型
accuracy = model.score(X_test, y_test)
print(f"Accuracy: {accuracy}")
4.3 隨機叢林
隨機叢林是一種集成進修方法,可能進步模型的泛化才能。
from sklearn.ensemble import RandomForestClassifier
# 創建隨機叢林模型
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
# 評價模型
accuracy = model.score(X_test, y_test)
print(f"Accuracy: {accuracy}")
5. 模型評價
模型評價是衡量模型機能的重要步調,Scikit-learn 供給了多種評價指標。
from sklearn.metrics import classification_report, confusion_matrix
# 評價邏輯回歸模型
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))
print(confusion_matrix(y_test, predictions))
6. 數據集深度剖析
Scikit-learn 供給了多種數據集,以下是一些常用的數據集及其利用處景:
6.1 鳶尾花數據集 (Iris)
鳶尾花數據集是最常用的呆板進修數據集之一,包含 150 個樣本,每個樣本有 4 個特徵。
# 載入鳶尾花數據集
iris = load_iris()
X, y = iris.data, iris.target
# 利用鳶尾花數據集停止分類任務
6.2 波士頓房價數據集 (Boston Housing)
波士頓房價數據集包含 506 個樣本,每個樣本有 13 個特徵,用於猜測房價。
from sklearn.datasets import load_boston
# 載入波士頓房價數據集
boston = load_boston()
X, y = boston.data, boston.target
# 利用波士頓房價數據集停止回歸任務
6.3 麥克爾-羅斯數據集 (Michael-ross)
邁克爾-羅斯數據集包含 140 個樣本,每個樣本有 11 個特徵,用於猜測信用評分。
from sklearn.datasets import make_classification
# 生成邁克爾-羅斯數據集
X, y = make_classification(n_samples=140, n_features=11, n_informative=8, n_redundant=3, random_state=42)
# 利用邁克爾-羅斯數據集停止分類任務
7. 總結
Scikit-learn 是 Python 中最風行的呆板進修庫之一,它供給了豐富的東西跟演算法,可能幫助數據科學家跟開辟者疾速實現呆板進修項目。經由過程本文的實戰代碼示例跟數據集深度剖析,讀者可能更好地懂得 Scikit-learn 的功能跟用法。盼望本文能對妳在呆板進修範疇的摸索有所幫助。