import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('./data/6_credit.csv')
X = df.iloc[:,1:6]#左闭右开
y = df['credit']
X = np.array(X.values)
y = np.array(y.values)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=1)
#进行朴素贝叶斯估计
from sklearn.naive_bayes import GaussianNB
GNB = GaussianNB()
GNB.fit(X_train,y_train)
pred = GNB.predict(X_test)
print(pred)
#对预测结果进行评价
from sklearn import model_selection
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
accuracy_score(y_test,pred)
print(confusion_matrix(y_true=y_test,y_pred=pred))
[[21 0 0]
[ 0 22 0]
[ 0 0 12]]
#方法2.逻辑回归
from sklearn.linear_model.logistic import LogisticRegression
clf = LogisticRegression()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
accuracy_score(y_test,y_pred)
print(confusion_matrix(y_true=y_test,y_pred=y_pred))
[[19 2 0]
[ 0 22 0]
[ 0 1 11]]
在这里,混淆矩阵的含义是矩阵的行代表是真是类别,列代表的是实际类别。第一行代表了真实类别为1的样本数据被预测为类别1、2、3的分布情况。从列的角度看,第1列就代表了预测类别为1的样本数据其真实值的分布情况。