24.11.14 朴素贝叶斯分类 决策树-分类
朴素贝叶斯分类
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")
import joblib
# 加载模型
model = joblib.load("./model/bayes.bin")
# 传入参数进行预测
poin = model.predict([[1, 2, 3, 4]])
print(poin)
# 泰坦尼克号生还测试
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")
import joblib
# 加载模型
model = joblib.load("./model/ttbayes.bin")
# 传入参数进行预测
poin = model.predict([[3,1,3]])
print(poin)
决策树-分类
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=["萼片长", "萼片宽", "花瓣长", "花瓣宽"])