MNIST & CatBoost保存模型并预测

安装

pip install catboost

数据集

分类MNIST(60000条数据784个特征),已上传CSDN

代码

import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
train = pd.read_csv('./input/mnist/train.csv')
train.head()

在这里插入图片描述

X = train.iloc[:, 1:]  # 训练数据
y = train['label']  #标签
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 划分训练、测试集
def plot_digits(instances, images_per_row=10):
    '''绘制数据集
    
    :param instances: 部分数据集
    :type instances: numpy.ndarray
    :param images_per_row: 每一行显示图片数
    '''
    size = 28
    images_per_row = min(len(instances), images_per_row)
    images = [instance.reshape(size, size) for instance in instances]
    n_rows = (len(instances) - 1) // images_per_row + 1
    row_images = []
    n_empty = n_rows * images_per_row - len(instances)
    images.append(np.zeros((size, size * n_empty)))
    for row in range(n_rows):
        rimages = images[row * images_per_row: (row + 1) * images_per_row]
        row_images.append(np.concatenate(rimages, axis=1))
    image = np.concatenate(row_images, axis=0)
    plt.imshow(image, cmap='gray_r')
    plt.axis("off")
    
plt.figure()
plot_digits(X_train[:100].values, images_per_row=10)
plt.show()

在这里插入图片描述

# 定义模型
clf = CatBoostClassifier()
# 训练
model = clf.fit(X_train, y_train)
0:	learn: 2.2139620	total: 975ms	remaining: 16m 13s
1:	learn: 2.1344069	total: 1.95s	remaining: 16m 15s
2:	learn: 2.0559619	total: 2.92s	remaining: 16m 10s
3:	learn: 1.9850790	total: 3.89s	remaining: 16m 7s
......
996:	learn: 0.1231917	total: 16m 35s	remaining: 3s
997:	learn: 0.1231500	total: 16m 36s	remaining: 2s
998:	learn: 0.1231068	total: 16m 37s	remaining: 999ms
999:	learn: 0.1230654	total: 16m 38s	remaining: 0us
# 评估
print('accuracy:', model.score(X_test, y_test))
# 保存
model.save_model('mnist.model')
# 加载
ccc = CatBoostClassifier()
ccc.load_model('mnist.model')
# 预测
index = random.randint(0, len(X_test))  # 随机挑一个
_X = X_test.values[index]
_y = y_test.values[index]  # 真值
predict = ccc.predict(_X)[0]  # 预测值

_X = _X.reshape(28, 28)
plt.imshow(_X, cmap='gray_r')
plt.title('original {}'.format(_y))
plt.show()

print('index:', index)
print('original:', _y)
print('predicted:', predict)

在这里插入图片描述

index: 7534
original: 6
predicted: 6

在这里插入图片描述

index: 6510
original: 4
predicted: 4

在这里插入图片描述

index: 7311
original: 6
predicted: 6

ipynb

下载地址

参考文献

  1. Battle of the Boosting Algos: LGB, XGB, Catboost
  2. CatBoost - open-source gradient boosting library
  3. Quick start - CatBoost. Documentation
  4. CatBoost tutorials
  5. 机器学习算法之Catboost
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XerCis

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

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

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

打赏作者

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

抵扣说明:

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

余额充值