中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

Scikit-learn中怎么繪制學習曲線

小億
89
2024-05-10 17:22:56
欄目: 編程語言

要繪制學習曲線,可以使用learning_curve函數來實現。下面是一個示例代碼:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

# 加載數據集
iris = load_iris()
X, y = iris.data, iris.target

# 初始化Logistic回歸模型
model = LogisticRegression()

# 繪制學習曲線
train_sizes, train_scores, test_scores = learning_curve(model, X, y, train_sizes=np.linspace(0.1, 1.0, 10), cv=5)

train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)

plt.figure()
plt.title("Learning Curve")
plt.xlabel("Training examples")
plt.ylabel("Score")
plt.grid()

plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                 train_scores_mean + train_scores_std, alpha=0.1,
                 color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                 test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score")

plt.legend(loc="best")
plt.show()

這段代碼將繪制Logistic回歸模型在不同訓練數據量下的學習曲線,可以直觀地觀察模型的訓練和驗證表現。

0
定兴县| 新安县| 包头市| 牡丹江市| 北海市| 抚顺县| 东兰县| 珲春市| 荆州市| 化州市| 江津市| 江安县| 仙桃市| 安达市| 深泽县| 宜兰县| 楚雄市| 明溪县| 梅河口市| 格尔木市| 泽库县| 寿光市| 沧州市| 尼玛县| 绥滨县| 神木县| 黄浦区| 上高县| 凤冈县| 且末县| 长岭县| 镇平县| 礼泉县| 商洛市| 康乐县| 灵寿县| 阿克陶县| 招远市| 闸北区| 额尔古纳市| 泸州市|