24.11.14 朴素贝叶斯分类 决策树-分类

朴素贝叶斯分类

python 复制代码
import joblib
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB

# 实例化贝叶斯分类器
model = MultinomialNB()
# 记载鸢尾花数据
X, y = load_iris(return_X_y=True)
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=666)
# 训练模型
model.fit(X_train, y_train)
# 评估
score = model.score(X_test, y_test)
print(score)
# 保存模型
joblib.dump(model, "./model/bayes.bin")
python 复制代码
import joblib

# 加载模型
model = joblib.load("./model/bayes.bin")
# 传入参数进行预测
poin = model.predict([[1, 2, 3, 4]])
print(poin)
python 复制代码
# 泰坦尼克号生还测试
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB

# 实例化贝叶斯分类器
model = MultinomialNB()
# 实例化字典列表特征提取


data = pd.read_csv("./src/titanic/titanic.csv")
x = data[["age", "sex", "pclass"]]
x["age"].fillna(x["age"].value_counts().index[0], inplace=True)
print(x)
x["sex"] = [0 if i == "male" else 1 for i in x["sex"]]
x["pclass"] = [int(i[0]) for i in x["pclass"]]
print(x)
# y = data["survived"]

# 数据处理
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(x, y, train_size=0.8, random_state=666)
# 训练模型
model.fit(X_train, y_train)
# 评估
score = model.score(X_test, y_test)
print(score)
# 保存模型
joblib.dump(model, "./model/ttbayes.bin")
python 复制代码
import joblib

# 加载模型
model = joblib.load("./model/ttbayes.bin")
# 传入参数进行预测
poin = model.predict([[3,1,3]])
print(poin)

决策树-分类

python 复制代码
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier, export_graphviz

# 决策树
model = DecisionTreeClassifier(criterion="entropy")
# 加载数据
x, y = load_iris(return_X_y=True)
# 
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

# 加载标准化估计器
scaler = StandardScaler()

scaler.fit(x_train)
x_train = scaler.transform(x_train)

# 训练模型
model.fit(x_train, y_train)

# 标准化需要用来测试的数据
x_test = scaler.transform(x_test)
# 评分
rank = model.score(x_test, y_test)
print(rank)

# 预估数据
y_pred = model.predict([[1, 1, 1, 1], [2, 2, 2, 2]])
print(y_pred)

# 决策过程可视化
export_graphviz(model, out_file="./model/tree.dot", feature_names=["萼片长", "萼片宽", "花瓣长", "花瓣宽"])
相关推荐
LearnYard1 分钟前
技术博主实测:2026年大语言模型辅助学习工具横向对比
人工智能·学习·语言模型
blues92578 分钟前
赋能企业AI高效落地|Vantage万极FDE四天实战营杭州首班开启报名
大数据·人工智能·fde
深海鱼在掘金9 分钟前
深入浅出RAG——第10章:检索后处理与重排序
人工智能
阿里云大数据AI技术26 分钟前
使用 PAI,一键拉起云端 AI “投资智囊团”
人工智能·agent
四六的六30 分钟前
让 AI 自己去点后台页面,它把我们的库存点没了
前端·人工智能·agent·个人开发·ai编程·ai产品·ai前端
月华路34 分钟前
《模型不玄学》第30章 完整跑一遍 Cheat Sheet
人工智能·深度学习·机器学习
workflower37 分钟前
智能体-企业行政问答助手
人工智能·机器学习·机器人·云计算·无人机
武子康41 分钟前
Agent 的工具没变,SGLang 缓存为什么没命中?
人工智能·llm·agent
hyunbar77744 分钟前
LangChain 实战:Agent 4类结构化输出方式
人工智能
咖啡星人k1 小时前
2026 可观测性实战:把观测契约写进SPEC,MonkeyCode 云端跑通
人工智能·机器学习