python日常操作-经过mysql导入抉择树

2 篇文章 0 订阅
1 篇文章 0 订阅

要将决策树模型导入MySQL中,需要完成以下几个步骤:

  1. 将训练好的决策树模型保存为pkl文件。
import pickle

# 假设训练好的决策树模型为dt_model
with open('dt_model.pkl', 'wb') as f:
    pickle.dump(dt_model, f)

  1. 创建MySQL表来存储决策树模型。
CREATE TABLE decision_tree_model (
  id INT PRIMARY KEY,
  model BLOB
);

  1. 将pkl文件中的模型序列化为二进制格式,并将其插入到MySQL表中。
import mysql.connector

# 假设MySQL连接参数为host、user、password、database
cnx = mysql.connector.connect(host='localhost',
                              user='user',
                              password='password',
                              database='database')
cursor = cnx.cursor()

# 读取pkl文件中的模型
with open('dt_model.pkl', 'rb') as f:
    model_binary = f.read()

# 将模型插入到MySQL表中
query = "INSERT INTO decision_tree_model (id, model) VALUES (1, %s)"
cursor.execute(query, (model_binary,))
cnx.commit()

cursor.close()
cnx.close()

  1. 从MySQL中读取决策树模型,并进行预测。
import mysql.connector
import pickle

# 假设MySQL连接参数为host、user、password、database
cnx = mysql.connector.connect(host='localhost',
                              user='user',
                              password='password',
                              database='database')
cursor = cnx.cursor()

# 从MySQL表中读取模型
query = "SELECT model FROM decision_tree_model WHERE id = 1"
cursor.execute(query)
model_binary = cursor.fetchone()[0]

# 反序列化模型
dt_model = pickle.loads(model_binary)

# 进行预测
X_test = [[...], [...], ...]  # 测试数据集
y_predict = dt_model.predict(X_test)

cursor.close()
cnx.close()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值