text_plain_output_1.png
from sklearn.feature_selection import RFE

from sklearn.feature_selection import RFECV

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import confusion_matrix

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import seaborn as sns

import seaborn as sns

from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(C=10)
logreg.fit(X_train, Y_train)
Y_predict1 = logreg.predict(X_test)

from sklearn.metrics import confusion_matrix
import seaborn as sns

logreg_cm = confusion_matrix(Y_test, Y_predict1)
f, ax = plt.subplots(figsize=(5,5))
sns.heatmap(logreg_cm, annot=True, linewidth=0.7, linecolor='red', fmt='g', ax=ax, cmap="BuPu")
plt.title('Logistic Regression Classification Confusion Matrix')
plt.xlabel('Y predict')
plt.ylabel('Y test')
plt.show()

score_logreg = logreg.score(X_test, Y_test)

from sklearn.feature_selection import RFE
logreg_2 = LogisticRegression()
rfe = RFE(estimator=logreg_2, n_features_to_select=5, step=1)
rfe = rfe.fit(X_train, Y_train)
X_train_3 = rfe.transform(X_train)
X_test_3 = rfe.transform(X_test)
logreg_2 = LogisticRegression()
logreg_2 = logreg_2.fit(X_train_3, Y_train)
Y_predict2 = logreg.predict(X_test_3)
score_logreg = logreg_2.score(X_test_3, Y_test)

from sklearn.feature_selection import RFECV
logreg_3 = LogisticRegression()
rfecv = RFECV(estimator=logreg_3, step=1, cv=5, scoring='accuracy')
rfecv = rfecv.fit(X_train, Y_train)

import matplotlib.pyplot as plt
plt.figure()
plt.xlabel('Number of features selected')
plt.ylabel('Cross validation score of number of selected features')
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()
max(rfecv.grid_scores_)
rfecv.grid_scores_