Python 機(jī)器學(xué)習(xí):十個(gè)入門機(jī)器學(xué)習(xí)的必備庫
大家好!今天我們要聊的是 Python 機(jī)器學(xué)習(xí)中不可或缺的 10 個(gè)庫。無論你是剛剛接觸機(jī)器學(xué)習(xí)的新手,還是已經(jīng)有一定經(jīng)驗(yàn)的老手,這些庫都能幫助你更好地理解和應(yīng)用機(jī)器學(xué)習(xí)技術(shù)。讓我們一步步來,從最基礎(chǔ)的庫開始,逐漸深入到更高級(jí)的工具。
1. NumPy
簡介:NumPy 是 Python 中用于科學(xué)計(jì)算的基礎(chǔ)庫,提供了多維數(shù)組對(duì)象、各種派生對(duì)象(如掩碼數(shù)組和矩陣)以及用于數(shù)組快速操作的各種函數(shù)。
示例:
import numpy as np
# 創(chuàng)建一個(gè)一維數(shù)組
a = np.array([1, 2, 3, 4, 5])
print(a) # 輸出: [1 2 3 4 5]
# 創(chuàng)建一個(gè)二維數(shù)組
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# 輸出:
# [[1 2 3]
# [4 5 6]]
# 數(shù)組的基本操作
c = a + 2
print(c) # 輸出: [3 4 5 6 7]
d = b * 2
print(d)
# 輸出:
# [[ 2 4 6]
# [ 8 10 12]]
2. Pandas
簡介:Pandas 是一個(gè)強(qiáng)大的數(shù)據(jù)處理和分析庫,提供了 DataFrame 和 Series 數(shù)據(jù)結(jié)構(gòu),方便進(jìn)行數(shù)據(jù)清洗、轉(zhuǎn)換和分析。
示例:
import pandas as pd
# 創(chuàng)建一個(gè) DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
print(df)
# 輸出:
# Name Age City
# 0 Alice 25 New York
# 1 Bob 30 Los Angeles
# 2 Charlie 35 Chicago
# 數(shù)據(jù)篩選
young_people = df[df['Age'] < 30]
print(young_people)
# 輸出:
# Name Age City
# 0 Alice 25 New York
3. Matplotlib
簡介:Matplotlib 是一個(gè)用于繪制圖表的庫,可以生成各種靜態(tài)、動(dòng)態(tài)和交互式圖表。
示例:
import matplotlib.pyplot as plt
# 繪制簡單的折線圖
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
plt.show()
4. Scikit-learn
簡介:Scikit-learn 是一個(gè)用于機(jī)器學(xué)習(xí)的庫,提供了大量的監(jiān)督和非監(jiān)督學(xué)習(xí)算法,以及模型評(píng)估和選擇的工具。
示例:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加載 Iris 數(shù)據(jù)集
iris = load_iris()
X = iris.data
y = iris.target
# 劃分訓(xùn)練集和測(cè)試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 訓(xùn)練 KNN 模型
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
# 預(yù)測(cè)
y_pred = knn.predict(X_test)
# 評(píng)估模型
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
# 輸出: Accuracy: 0.97
5. TensorFlow
簡介:TensorFlow 是由 Google 開發(fā)的深度學(xué)習(xí)框架,支持多種平臺(tái)和語言,廣泛應(yīng)用于圖像識(shí)別、自然語言處理等領(lǐng)域。
示例:
import tensorflow as tf
from tensorflow.keras import layers, models
# 構(gòu)建一個(gè)簡單的神經(jīng)網(wǎng)絡(luò)
model = models.Sequential([
layers.Dense(64, activation='relu', input_shape=(32,)),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# 編譯模型
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 生成一些隨機(jī)數(shù)據(jù)
import numpy as np
x_train = np.random.random((1000, 32))
y_train = np.random.randint(10, size=(1000,))
# 訓(xùn)練模型
model.fit(x_train, y_train, epochs=10, batch_size=32)
6. PyTorch
簡介:PyTorch 是由 Facebook 開發(fā)的深度學(xué)習(xí)框架,以靈活性和動(dòng)態(tài)計(jì)算圖著稱,廣泛應(yīng)用于研究和生產(chǎn)環(huán)境。
示例:
import torch
import torch.nn as nn
import torch.optim as optim
# 定義一個(gè)簡單的神經(jīng)網(wǎng)絡(luò)
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.fc1 = nn.Linear(32, 64)
self.fc2 = nn.Linear(64, 64)
self.fc3 = nn.Linear(64, 10)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = torch.softmax(self.fc3(x), dim=1)
return x
# 實(shí)例化模型
model = SimpleNet()
# 生成一些隨機(jī)數(shù)據(jù)
x_train = torch.randn(1000, 32)
y_train = torch.randint(10, (1000,))
# 定義損失函數(shù)和優(yōu)化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 訓(xùn)練模型
for epoch in range(10):
optimizer.zero_grad()
outputs = model(x_train)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
print(f'Epoch {epoch+1}, Loss: {loss.item()}')
7. Keras
簡介:Keras 是一個(gè)高級(jí)神經(jīng)網(wǎng)絡(luò) API,可以運(yùn)行在 TensorFlow、Theano 或 CNTK 后端,提供了簡潔易用的接口。
示例:
from keras.models import Sequential
from keras.layers import Dense
# 構(gòu)建一個(gè)簡單的神經(jīng)網(wǎng)絡(luò)
model = Sequential([
Dense(64, activation='relu', input_shape=(32,)),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# 編譯模型
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 生成一些隨機(jī)數(shù)據(jù)
import numpy as np
x_train = np.random.random((1000, 32))
y_train = np.random.randint(10, size=(1000,))
# 訓(xùn)練模型
model.fit(x_train, y_train, epochs=10, batch_size=32)
8. LightGBM
簡介:LightGBM 是一個(gè)基于梯度提升決策樹(GBDT)的高效機(jī)器學(xué)習(xí)框架,特別適用于大規(guī)模數(shù)據(jù)集。
示例:
import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加載乳腺癌數(shù)據(jù)集
data = load_breast_cancer()
X = data.data
y = data.target
# 劃分訓(xùn)練集和測(cè)試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 轉(zhuǎn)換為 LightGBM 的數(shù)據(jù)格式
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test, reference=train_data)
# 設(shè)置參數(shù)
params = {
'objective': 'binary',
'metric': 'binary_logloss',
'boosting_type': 'gbdt',
'num_leaves': 31,
'learning_rate': 0.05
}
# 訓(xùn)練模型
model = lgb.train(params, train_data, num_boost_round=100, valid_sets=[test_data], early_stopping_rounds=10)
# 預(yù)測(cè)
y_pred = model.predict(X_test)
y_pred = [1 if pred > 0.5 else 0 for pred in y_pred]
# 評(píng)估模型
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
# 輸出: Accuracy: 0.96
9. XGBoost
簡介:XGBoost 是另一個(gè)基于梯度提升決策樹的高效機(jī)器學(xué)習(xí)框架,以其高性能和準(zhǔn)確性而聞名。
示例:
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加載乳腺癌數(shù)據(jù)集
data = load_breast_cancer()
X = data.data
y = data.target
# 劃分訓(xùn)練集和測(cè)試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 轉(zhuǎn)換為 DMatrix 格式
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
# 設(shè)置參數(shù)
params = {
'objective': 'binary:logistic',
'eval_metric': 'logloss',
'eta': 0.1,
'max_depth': 3
}
# 訓(xùn)練模型
model = xgb.train(params, dtrain, num_boost_round=100, evals=[(dtest, 'test')], early_stopping_rounds=10)
# 預(yù)測(cè)
y_pred = model.predict(dtest)
y_pred = [1 if pred > 0.5 else 0 for pred in y_pred]
# 評(píng)估模型
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
# 輸出: Accuracy: 0.96
10. CatBoost
簡介:CatBoost 是一個(gè)開源的梯度提升框架,特別適合處理分類特征,無需進(jìn)行預(yù)處理。
示例:
from catboost import CatBoostClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加載乳腺癌數(shù)據(jù)集
data = load_breast_cancer()
X = data.data
y = data.target
# 劃分訓(xùn)練集和測(cè)試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 訓(xùn)練模型
model = CatBoostClassifier(iterations=100, learning_rate=0.1, depth=3)
model.fit(X_train, y_train, verbose=False)
# 預(yù)測(cè)
y_pred = model.predict(X_test)
# 評(píng)估模型
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
# 輸出: Accuracy: 0.96
實(shí)戰(zhàn)案例:使用 Scikit-learn 進(jìn)行房價(jià)預(yù)測(cè)
假設(shè)我們有一個(gè)包含房屋特征的數(shù)據(jù)集,目標(biāo)是預(yù)測(cè)房屋的價(jià)格。我們將使用 Scikit-learn 來構(gòu)建一個(gè)線性回歸模型。
數(shù)據(jù)集:
- house_prices.csv 包含以下列:
- bedrooms:臥室數(shù)量
- bathrooms:浴室數(shù)量
- sqft_living:居住面積(平方英尺)
- price:房屋價(jià)格
步驟:
- 加載數(shù)據(jù)。
- 數(shù)據(jù)預(yù)處理。
- 劃分訓(xùn)練集和測(cè)試集。
- 訓(xùn)練線性回歸模型。
- 評(píng)估模型性能。
代碼實(shí)現(xiàn):
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# 1. 加載數(shù)據(jù)
data = pd.read_csv('house_prices.csv')
# 2. 數(shù)據(jù)預(yù)處理
X = data[['bedrooms', 'bathrooms', 'sqft_living']]
y = data['price']
# 3. 劃分訓(xùn)練集和測(cè)試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 4. 訓(xùn)練線性回歸模型
model = LinearRegression()
model.fit(X_train, y_train)
# 5. 評(píng)估模型性能
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'Mean Squared Error: {mse:.2f}')
print(f'R^2 Score: {r2:.2f}')
總結(jié)
本文介紹了 10 個(gè) Python 機(jī)器學(xué)習(xí)的必備庫,包括 NumPy、Pandas、Matplotlib、Scikit-learn、TensorFlow、PyTorch、Keras、LightGBM、XGBoost 和 CatBoost。每個(gè)庫都有其獨(dú)特的特點(diǎn)和應(yīng)用場(chǎng)景,通過實(shí)際的代碼示例,我們展示了如何使用這些庫進(jìn)行數(shù)據(jù)處理、可視化和模型訓(xùn)練。最后,我們通過一個(gè)實(shí)戰(zhàn)案例,展示了如何使用 Scikit-learn 進(jìn)行房價(jià)預(yù)測(cè)。