python数据建模

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 05:18   1075   0

from sklearn.svm import SVC
import pandas as pd
import numpy as np
import seaborn as sns

———————-导入数据集————————————–

iris = sns.load_dataset(“iris”)
iris.species.replace([‘setosa’,’versicolor’, ‘virginica’],[0,1,2],inplace = True)
iris = iris[iris.species != 2]
X = iris.iloc[:,0:2]
y = iris.iloc[:,4]

———————划分测试集、训练集———————————

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=1,stratify = y)

———————做模型——————————————

from sklearn.svm import SVC

for i in [0.1,1,10,100]:

if name == “main“:
svc = SVC(C=10,kernel=”rbf”,random_state=1,
cache_size=500,decision_function_shape=”ovr”,probability=True)
svc.fit(X_train,y_train)
y_het = svc.predict(X_train)
y_het2 = svc.predict(X_test)

——————–评估———————————————

from sklearn.metrics import confusion_matrix,classification_report #会输出precison,recall,fi_score,support
classificationReport = classification_report(y_train,y_het)
print("C=",10)
print("\ttrian = \n",classificationReport)

混淆矩阵

CM = confusion_matrix(y_train,y_het,labels = [2,1,0])
print("\ttrain = \n",CM)

测试级

classificationReport2 = classification_report(y_test,y_het2)
print("\ttest = \n",classificationReport2)
CM2 = confusion_matrix(y_test,y_het2)
print("\ttest = \n",CM2)

——————画ROC曲线——————————————

from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
y_proba = svc.predict_proba(X_test)
fpr,tpr,thresholds = roc_curve(y_test,y_proba[:,1])
print(fpr)
print(tpr)
print(thresholds)
plt.plot(fpr,tpr,”r-“)
plt.plot([0,1],[0,1],’b–’,label = “aoc”)
plt.legend()
plt.show()
plt.plot(tpr,1-thresholds,”r–”,label = “tpr”)
plt.plot(fpr,1-thresholds,”b-“,label= “fpr”)
plt.legend()
plt.show()

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP