67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.datasets import load_breast_cancer
|
|
from sklearn.svm import SVC
|
|
from sklearn.linear_model import LogisticRegression
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
|
|
# Load the data
|
|
cancer = load_breast_cancer()
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
|
print(X_train.shape)
|
|
print(X_test.shape)
|
|
# Logistic Regression
|
|
logreg = LogisticRegression(solver='lbfgs')
|
|
logreg.fit(X_train, y_train)
|
|
print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
|
# Support vector machine
|
|
svm = SVC(gamma='auto', C=100)
|
|
svm.fit(X_train, y_train)
|
|
print("Test set accuracy with SVM: {:.2f}".format(svm.score(X_test,y_test)))
|
|
# Decision Trees
|
|
deep_tree_clf = DecisionTreeClassifier(max_depth=None)
|
|
deep_tree_clf.fit(X_train, y_train)
|
|
print("Test set accuracy with Decision Trees: {:.2f}".format(deep_tree_clf.score(X_test,y_test)))
|
|
#now scale the data
|
|
from sklearn.preprocessing import StandardScaler
|
|
scaler = StandardScaler()
|
|
scaler.fit(X_train)
|
|
X_train_scaled = scaler.transform(X_train)
|
|
X_test_scaled = scaler.transform(X_test)
|
|
# Logistic Regression
|
|
logreg.fit(X_train_scaled, y_train)
|
|
print("Test set accuracy Logistic Regression with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
|
# Support Vector Machine
|
|
svm.fit(X_train_scaled, y_train)
|
|
print("Test set accuracy SVM with scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
|
# Decision Trees
|
|
deep_tree_clf.fit(X_train_scaled, y_train)
|
|
print("Test set accuracy with Decision Trees and scaled data: {:.2f}".format(deep_tree_clf.score(X_test_scaled,y_test)))
|
|
|
|
|
|
from sklearn.ensemble import RandomForestClassifier
|
|
from sklearn.preprocessing import LabelEncoder
|
|
from sklearn.model_selection import cross_validate
|
|
# Data set not specificied
|
|
#Instantiate the model with 100 trees and entropy as splitting criteria
|
|
Random_Forest_model = RandomForestClassifier(n_estimators=500,criterion="entropy")
|
|
Random_Forest_model.fit(X_train_scaled, y_train)
|
|
#Cross validation
|
|
accuracy = cross_validate(Random_Forest_model,X_test_scaled,y_test,cv=10)['test_score']
|
|
print(accuracy)
|
|
print("Test set accuracy with Random Forests and scaled data: {:.2f}".format(Random_Forest_model.score(X_test_scaled,y_test)))
|
|
|
|
|
|
import scikitplot as skplt
|
|
y_pred = Random_Forest_model.predict(X_test_scaled)
|
|
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
|
|
#<matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
|
|
plt.show()
|
|
y_probas = Random_Forest_model.predict_proba(X_test_scaled)
|
|
skplt.metrics.plot_roc(y_test, y_probas)
|
|
plt.show()
|
|
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
|
|
plt.show()
|