python sklearn 验证曲线

python sklearn 验证曲线

下载文件地址:
链接: https://pan.baidu.com/s/1VdjCSw9MfKZ1WAcaiDyi8w 提取码: wtck

验证参数max_depth

"""
    验证曲线 对超参数 进行逐一验证  以验证取得最佳 超参数
"""
import numpy as np
import sklearn.preprocessing as sp
import sklearn.ensemble as se
import sklearn.model_selection as ms
import matplotlib.pyplot as mp

# 读取数据
lines = np.loadtxt(r".\car.txt", delimiter=',', dtype='str')
print(lines.shape)

# 整理样本空间 并编码 (对列执行标签编码)
train_x, train_y = [], []
encoders = []  # 标签编码数组
for index, row in enumerate(lines.T):
    encoder = sp.LabelEncoder()
    if index < (len(lines.T) - 1):  # 训练样本 X
        train_x.append(encoder.fit_transform(row))
    else:
        train_y = encoder.fit_transform(row)  # 训练样本结果 Y
    encoders.append(encoder)

train_x = np.array(train_x).T  # 转置
train_y = np.array(train_y)

print(train_x.shape, train_y.shape)
print(train_x[0], train_y[0])

# 随机森林分类模型
model = se.RandomForestClassifier(
    max_depth=9, n_estimators=140, random_state=7)

# 验证曲线  参数max_depth 分别使用 1-10 验证结果
# 最好效果为 9
train_scores, test_scores = ms.validation_curve(
    model, train_x, train_y, "max_depth", np.arange(1, 11, 1), cv=5)
print(test_scores.mean(axis=1))

# 绘制验证曲线
mp.grid(linestyle=":")
mp.plot(np.arange(1, 11, 1),
        test_scores.mean(axis=1),
        "o-", color="dodgerblue", label="Validation Curve")
mp.legend()
mp.show()

在这里插入图片描述

验证参数 n_estimators

"""
    验证曲线 对超参数 进行逐一验证  以验证取得最佳 超参数
"""
import numpy as np
import sklearn.preprocessing as sp
import sklearn.ensemble as se
import sklearn.model_selection as ms
import matplotlib.pyplot as mp

# 读取数据
lines = np.loadtxt(r".\car.txt", delimiter=',', dtype='str')
print(lines.shape)

# 整理样本空间 并编码 (对列执行标签编码)
train_x, train_y = [], []
encoders = []  # 标签编码数组
for index, row in enumerate(lines.T):
    encoder = sp.LabelEncoder()
    if index < (len(lines.T) - 1):  # 训练样本 X
        train_x.append(encoder.fit_transform(row))
    else:
        train_y = encoder.fit_transform(row)  # 训练样本结果 Y
    encoders.append(encoder)

train_x = np.array(train_x).T  # 转置
train_y = np.array(train_y)

print(train_x.shape, train_y.shape)
print(train_x[0], train_y[0])

# 随机森林分类模型
model = se.RandomForestClassifier(
    max_depth=9, n_estimators=140, random_state=7)

# 验证曲线 n_estimators 分别使用 100-200 验证结果
train_scores, test_scores = ms.validation_curve(model, train_x, train_y, "n_estimators",
                                                np.arange(100, 200, 10), cv=5)
print(test_scores.mean(axis=1))

# 绘制验证曲线
mp.grid(linestyle=":")
mp.plot(np.arange(100, 200, 10),
        test_scores.mean(axis=1),
        "o-", color="dodgerblue", label="Validation Curve")
mp.legend()
mp.show()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值