自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 機(jī)器學(xué)習(xí):十個(gè)入門機(jī)器學(xué)習(xí)的必備庫

開發(fā) 機(jī)器學(xué)習(xí)
本文介紹了 10 個(gè) Python 機(jī)器學(xué)習(xí)的必備庫,每個(gè)庫都有其獨(dú)特的特點(diǎn)和應(yīng)用場(chǎng)景,通過實(shí)際的代碼示例,我們展示了如何使用這些庫進(jìn)行數(shù)據(jù)處理、可視化和模型訓(xùn)練。

大家好!今天我們要聊的是 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è)。

責(zé)任編輯:趙寧寧 來源: 小白PythonAI編程
相關(guān)推薦

2023-12-25 10:53:54

機(jī)器學(xué)習(xí)模型性能

2023-03-02 00:04:59

機(jī)器學(xué)習(xí)系統(tǒng)架構(gòu)

2022-10-28 15:19:28

機(jī)器學(xué)習(xí)距離度量數(shù)據(jù)集

2018-07-11 08:40:29

AWSWeb機(jī)器學(xué)習(xí)

2022-04-19 08:29:12

Python機(jī)器學(xué)習(xí)

2019-07-31 09:00:00

Python編程語言Python庫

2022-02-07 00:05:49

機(jī)器學(xué)習(xí)GitHub工具

2024-10-30 16:59:57

Python機(jī)器學(xué)習(xí)

2016-11-03 09:19:04

Python機(jī)器學(xué)習(xí)庫

2023-02-10 16:36:30

機(jī)器學(xué)習(xí)評(píng)估指標(biāo)

2024-07-29 15:07:16

2020-05-19 09:00:26

機(jī)器學(xué)習(xí)人工智能TensorFlow

2017-06-27 09:43:43

Python機(jī)器學(xué)習(xí)

2023-04-19 19:05:08

機(jī)器學(xué)習(xí)零售業(yè)

2024-04-10 12:39:08

機(jī)器學(xué)習(xí)python

2019-07-17 20:27:04

機(jī)器學(xué)習(xí)人工智能計(jì)算機(jī)

2022-09-11 15:02:22

機(jī)器學(xué)習(xí)算法感知器

2022-11-07 14:29:46

機(jī)器學(xué)習(xí)零售業(yè)

2022-08-10 15:56:40

機(jī)器學(xué)習(xí)算法深度學(xué)習(xí)

2020-08-19 09:20:00

機(jī)器學(xué)習(xí)人工智能Python
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)