保存算法模型
1、加载数据集
data = load_iris()
2、数据集划分
train_x,test_x,train_y,test_y = train_test_split(data['data'],data['target'])
3、特征工程(标准化)
std = StandardScaler()
train_x = std.fit_transform(train_x)
test_x = std.transform(test_x)
4、模型选择
可以选择不同的算法
逻辑回归
lg = LogisticRegression()
lg.fit(train_x,train_y)
KNN算法
lg = KNeighborsClassifier(n_neighbors=3)
lg.fit(train_x,train_y)
朴素贝叶斯
lg = MultinomialNB()
lg.fit(train_x,train_y)
支持向量机
lg = SVC()
lg.fit(train_x,train_y)
决策树
lg = DecisionTreeClassifier()
lg.fit(train_x,train_y)
随机森林
lg = RandomForestClassifier()
lg.fit(train_x,train_y)
保存模型
joblib.dump(std,'lg_std.pkl')
joblib.dump(lg,'lg.pkl')
代码:
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import precision_score,recall_score,f1_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB,MultinomialNB,BernoulliNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
import joblib
#1、加载数据集
data = load_iris()
#2、数据集划分
train_x,test_x,train_y,test_y = train_test_split(data['data'],data['target'])
#3、特征工程(标准化)
std = StandardScaler()
train_x = std.fit_transform(train_x)
test_x = std.transform(test_x)
#4、模型选择
# lg = LogisticRegression()
# lg.fit(train_x,train_y)
# lg = KNeighborsClassifier(n_neighbors=3)
# lg.fit(train_x,train_y)
# lg = MultinomialNB()
# lg.fit(train_x,train_y)
# lg = SVC()
# lg.fit(train_x,train_y)
# lg = DecisionTreeClassifier()
# lg.fit(train_x,train_y)
lg = RandomForestClassifier()
lg.fit(train_x,train_y)
joblib.dump(std,'lg_std.pkl')
joblib.dump(lg,'lg.pkl')
使用算法模型
import joblib
import numpy as np
x1 = input("请输入鸢尾花花萼的长度")
x2 = input("请输入鸢尾花花萼的宽度")
x3 = input("请输入鸢尾花花瓣的长度")
x4 = input("请输入鸢尾花花瓣的宽度")
x = np.array([x1,x2,x3,x4]).reshape(1,4)
std = joblib.load('lg_std.pkl')
x = std.transform(x)
lg = joblib.load('lg.pkl')
y = lg.predict(x)
print(y)