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=["萼片长", "萼片宽", "花瓣长", "花瓣宽"])
相关推荐
字节跳动_离青6 分钟前
智能的路径
人工智能
王上上17 分钟前
【论文阅读28】-CNN-BiLSTM-Attention-(2024)
论文阅读·人工智能·cnn
Channing Lewis28 分钟前
如果科技足够发达,是否还需要维持自然系统(例如生物多样性)中那种‘冗余’和‘多样性’,还是可以只保留最优解?
大数据·人工智能·科技
禺垣29 分钟前
区块链技术概述
大数据·人工智能·分布式·物联网·去中心化·区块链
IT科技那点事儿37 分钟前
引领AI安全新时代 Accelerate 2025北亚巡展·北京站成功举办
人工智能·安全
新智元1 小时前
美 IT 业裁员狂飙 35%,「硅谷梦」彻底崩塌!打工人怒喷 PIP
人工智能·openai
新智元1 小时前
乔布斯挚友去世!胰腺癌再夺硅谷天才,曾写下苹果「创世代码」
人工智能·openai
春末的南方城市1 小时前
中山大学&美团&港科大提出首个音频驱动多人对话视频生成MultiTalk,输入一个音频和提示,即可生成对应唇部、音频交互视频。
人工智能·python·深度学习·计算机视觉·transformer
春末的南方城市1 小时前
Ctrl-Crash 助力交通安全:可控生成逼真车祸视频,防患于未然
人工智能·计算机视觉·自然语言处理·aigc·音视频
程序边界1 小时前
全球人工智能技术大会(GAITC 2025):技术前沿与产业融合的深度交响
人工智能