--- a +++ b/Models/decisiontrees.py @@ -0,0 +1,58 @@ +# Importing the libraries +import numpy as np +import matplotlib.pyplot as plt +import pandas as pd +from sklearn.model_selection import GridSearchCV +from sklearn.tree import DecisionTreeClassifier +from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve + +# Importing the dataset +dataset = pd.read_csv('../Dataset/diabetes.csv') +X = dataset.iloc[:, :-1].values +y = dataset.iloc[:, 8].values + +# Splitting the dataset into the Training set and Test set +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 = 42) + +# Feature Scaling +from sklearn.preprocessing import StandardScaler +sc = StandardScaler() +X_train = sc.fit_transform(X_train) +X_test = sc.transform(X_test) + +# Parameter evaluation +treeclf = DecisionTreeClassifier(random_state=42) +parameters = {'max_depth': [6, 7, 8, 9], + 'min_samples_split': [2, 3, 4, 5], + 'max_features': [1, 2, 3, 4] +} +gridsearch=GridSearchCV(treeclf, parameters, cv=100, scoring='roc_auc') +gridsearch.fit(X,y) +print(gridsearch.best_params_) +print(gridsearch.best_score_) + +# Adjusting development threshold +tree = DecisionTreeClassifier(max_depth = 6, max_features = 4, + min_samples_split = 5, + random_state=42) +X_train,X_test,y_train,y_test = train_test_split(X, y, random_state=42) +tree.fit(X_train, y_train) +print("Accuracy on training set: {:.3f}".format(tree.score(X_train, y_train))) +print("Accuracy on test set: {:.3f}".format(tree.score(X_test, y_test))) + +# Predicting the Test set results +y_pred = tree.predict(X_test) + +# Making the Confusion Matrix +from sklearn.metrics import classification_report, confusion_matrix +cm = confusion_matrix(y_test, y_pred) + +print('TP - True Negative {}'.format(cm[0,0])) +print('FP - False Positive {}'.format(cm[0,1])) +print('FN - False Negative {}'.format(cm[1,0])) +print('TP - True Positive {}'.format(cm[1,1])) +print('Accuracy Rate: {}'.format(np.divide(np.sum([cm[0,0],cm[1,1]]),np.sum(cm)))) +print('Misclassification Rate: {}'.format(np.divide(np.sum([cm[0,1],cm[1,0]]),np.sum(cm)))) + +round(roc_auc_score(y_test,y_pred),5) \ No newline at end of file