Skip to content
Snippets Groups Projects
Commit 694ce504 authored by ZHANG Zuoyu's avatar ZHANG Zuoyu
Browse files

Merge branch 'XiranZ' into 'main'

Fonctions_version1.py

See merge request !2
parents 8fa54afd e6cbeb63
No related branches found
No related tags found
1 merge request!2Fonctions_version1.py
......@@ -227,3 +227,62 @@ def test_decision_tree(data, max_depth):
plt.ylabel('True Positive Rate')
plt.title('The ROC curve of the decision tree model')
plt.show()
class Gaussian_NB:
def __init__(self):
self.mean0,self.mean1 = 0,0
self.var0,self.var1 = 1,1
self.p0,self.p1 = [0],[0]
def fit(self,trainMatrix,trainCategory):
numTrainData=len(trainMatrix)
numFeatures=len(trainMatrix[0])
p_c1=sum(trainCategory)/float(numTrainData)
self.mean0 = np.mean(trainMatrix[trainCategory==0],axis = 0)
self.mean1 = np.mean(trainMatrix[trainCategory==1],axis = 0)
self.var0 = np.var(trainMatrix[trainCategory==0],axis = 0)
self.var1 = np.var(trainMatrix[trainCategory==1],axis = 0)
return self
def _get_proba(self,testMatrix):
p0Vect = ((2*np.pi*self.var0)**0.5)*np.exp(-(testMatrix-self.mean0)**2/(2*self.var0**2))
p1Vect = ((2*np.pi*self.var1)**0.5)*np.exp(-(testMatrix-self.mean1)**2/(2*self.var1**2))
p_condition0 = reduce(operator.mul, p0Vect.T)
p_condition1 = reduce(operator.mul, p1Vect.T)
self.p0 = p_condition0*(1-p_c1)
self.p1 = p_condition1*p_c1
return
def predict(self,testMatrix):
self._get_proba(testMatrix)
label = np.zeros(len(self.p1))
for i in range(len(label)):
label[i] = 0 if self.p0[i]>self.p1[i] else 1
return label.reshape([-1,1])
def predict_proba(self,testMatrix):
self._get_proba(testMatrix)
return np.array([self.p0,self.p1]).T
def score(self,y_true, y_pred):
acc = np.mean([1 if y_true[i] == y_pred[i] else 0 for i in range(len(y_true))])
return acc
def test_Gaussian_NB(data):
X_train, X_test, y_train, y_test = train_test_split(data[:, 0:-1], data[:,-1], test_size=0.25, random_state=42)
gnb = Gaussian_NB()
y_pred = gnb.fit(X_train, y_train).predict(X_test)
acc_gnb = gnb.score(y_test, y_pred)
P_score = precision_score(y_test,y_pred)
R_score = recall_score(y_test,y_pred)
y_score = (gnb.predict_proba(X_test))[:,1]
fpr, tpr, thresholds = roc_curve(y_test,y_score)
Area_Under_Curve = auc(fpr, tpr)
print("The accuracy of the logistic regression model is: ", acc_gnb, "\n",
"The precision of the logistic regression model is: ", P_score, "\n",
"The recall of the logistic regression model is: ", R_score, "\n",
"The AUC of the logistic regression model is: ", Area_Under_Curve, "\n")
plt.plot(fpr, tpr, 'b',label='AUC = %0.2f'% Area_Under_Curve)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.1])
plt.ylim([-0.1,1.1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('The ROC curve of the Gaussian Naive Bayes classifier')
plt.show()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment