text_plain_output_1.png
from imblearn.over_sampling import SMOTE

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import precision_score,recall_score,f1_score,roc_auc_score,roc_curve

from sklearn.model_selection import train_test_split,GridSearchCV

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import sklearn.metrics as metrics

file = pd.read_csv('../input/pulsar_stars.csv')

y = file.target_class
X = file[file.columns[:8]]
X.shape

y.value_counts()

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X.astype(np.float64))

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=0)

sm = SMOTE(random_state=2)
X_train_res, y_train_res = sm.fit_sample(X_train, y_train.ravel())

rnd_clf = RandomForestClassifier(random_state=100)
param_grid = {'n_estimators': [100, 150], 'max_features': ['auto', 'sqrt', 'log2'], 'max_depth': [5, 6, 7, 8], 'criterion': ['gini', 'entropy']}
CV_rfc = GridSearchCV(estimator=rnd_clf, param_grid=param_grid, cv=5)
rnd_cv_fit = CV_rfc.fit(X_train_res, y_train_res)

rnd = RandomForestClassifier(random_state=100, n_estimators=150, criterion='gini', max_depth=8, max_features='log2')
rnd_fit = rnd_clf.fit(X_train_res, y_train_res)
y_test_fit = rnd_fit.predict(X_test)

roc_curve(y_test, y_test_fit)
fpr, tpr, threshold = roc_curve(y_test, y_test_fit)
roc_auc = metrics.auc(fpr, tpr)
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()