<jupyter_start><jupyter_text>**Hello, this code aims to define the dangerousness of the water of a dataset provided on kaggle under the following link: https://www.kaggle.com/datasets/mssmartypants/water-quality** Different Import required<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from plotnine import ggplot, aes, geom_line
from google.colab import drive
import statistics
import numpy as np; np.random.seed(1)
import seaborn.apionly as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import RepeatedStratifiedKFold<jupyter_output><empty_output><jupyter_text>Importing data<jupyter_code>drive.mount('/content/drive')
!ls /content/drive/
data = pd.read_csv('/content/drive/MyDrive/projet_ML/waterQuality1.csv',header=0)<jupyter_output>Mounted at /content/drive
MyDrive<jupyter_text>Modification of the data to make it usable:<jupyter_code>for i in data.columns.values.tolist():
  data = data[data[i] != '#NUM!']
  print(i)
print(data)
data['is_safe'] = data['is_safe'].astype(int)<jupyter_output>aluminium
ammonia
arsenic
barium
cadmium
chloramine
chromium
copper
flouride
bacteria
viruses
lead
nitrates
nitrites
mercury
perchlorate
radium
selenium
silver
uranium
is_safe
      aluminium ammonia  arsenic  barium  cadmium  chloramine  chromium  \
0          1.65    9.08     0.04    2.85    0.007        0.35      0.83   
1          2.32   21.16     0.01    3.31    0.002        5.28      0.68   
2          1.01   14.02     0.04    0.58    0.008        4.24      0.53   
3          1.36   11.33     0.04    2.96    0.001        7.23      0.03   
4          0.92   24.33     0.03    0.20    0.006        2.67      0.69   
...         ...     ...      ...     ...      ...         ...       ...   
7994       0.05    7.78     0.00    1.95    0.040        0.10      0.03   
7995       0.05   24.22     0.02    0.59    0.010        0.45      0.02   
7996       0.09    6.85     0.00    0.61    0.030        0.05      0.05   
7997       0.01      10     0.01    2.00    0.000        2.00      0.00   [...]<jupyter_text>Breakdown between my parameters of interest and the variable I would like to predict<jupyter_code>X= data.drop(['is_safe'], axis=1)
print(X)
y= data['is_safe']
print(y)<jupyter_output>aluminium ammonia  arsenic  barium  cadmium  chloramine  chromium  \
0          1.65    9.08     0.04    2.85    0.007        0.35      0.83   
1          2.32   21.16     0.01    3.31    0.002        5.28      0.68   
2          1.01   14.02     0.04    0.58    0.008        4.24      0.53   
3          1.36   11.33     0.04    2.96    0.001        7.23      0.03   
4          0.92   24.33     0.03    0.20    0.006        2.67      0.69   
...         ...     ...      ...     ...      ...         ...       ...   
7994       0.05    7.78     0.00    1.95    0.040        0.10      0.03   
7995       0.05   24.22     0.02    0.59    0.010        0.45      0.02   
7996       0.09    6.85     0.00    0.61    0.030        0.05      0.05   
7997       0.01      10     0.01    2.00    0.000        2.00      0.00   
7998       0.04    6.85     0.01    0.70    0.030        0.05      0.01   

      copper  flouride  bacteria  viruses   lead  nitrates  nitrites  mercury  \
0       0.17      [...]<jupyter_text>Data visualisation :<jupyter_code>for i in X:
  mean = np.mean(X)
  variance = np.var(X)
  max = np.max(X)
  min = np.min(X)
results = np.array(([[mean],[variance], [max], [min]]))
print(mean,variance,max,min)
print(results)
for i in data.columns.values.tolist():
    sns.histplot(data = data, x = i)
    plt.show()
sns.heatmap(data = data.corr(), linewidths = .5, annot = False, cmap = "RdYlGn_r")
heatmap = plt.show()
print(data.corr())
print(heatmap)<jupyter_output><empty_output><jupyter_text>Splitting my data in train, validation and teest<jupyter_code>train, validate, test = np.split(data.sample(frac=1), [int(.6*len(data)), int(.8*len(data))])
X_train= train.drop(['is_safe'], axis=1)
y_train= train['is_safe']
X_validate= validate.drop(['is_safe'], axis=1)
y_validate= validate['is_safe']
X_test= test.drop(['is_safe'], axis=1)
y_test= test['is_safe']
print(y)
print(len(X_train))
print(len(X_test))
print(len(X_validate))
print(len(y_train))
print(len(y_test))
print(len(y_validate))<jupyter_output>0       1
1       1
2       0
3       1
4       1
       ..
7994    1
7995    1
7996    1
7997    1
7998    1
Name: is_safe, Length: 7996, dtype: int64
4797
1600
1599
4797
1600
1599<jupyter_text>Searching for the best Hyperparameters in a logistic reggression<jupyter_code>#from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import GridSearchCV
#from sklearn.linear_model import LogisticRegression
# define models and parameters
model = LogisticRegression()
solvers = ['newton-cg', 'lbfgs', 'liblinear']
penalty = ['l2','l1','elasticnet']
c_values = [100, 10, 1.0, 0.1, 0.01]
# define grid search
grid = dict(solver=solvers,penalty=penalty,C=c_values)
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='accuracy',error_score=0)
grid_result = grid_search.fit(X_validate, y_validate)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))<jupyter_output>Best: 0.910155 using {'C': 100, 'penalty': 'l1', 'solver': 'liblinear'}
0.909113 (0.014488) with: {'C': 100, 'penalty': 'l2', 'solver': 'newton-cg'}
0.904321 (0.018243) with: {'C': 100, 'penalty': 'l2', 'solver': 'lbfgs'}
0.909113 (0.014488) with: {'C': 100, 'penalty': 'l2', 'solver': 'liblinear'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'l1', 'solver': 'newton-cg'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'l1', 'solver': 'lbfgs'}
0.910155 (0.015844) with: {'C': 100, 'penalty': 'l1', 'solver': 'liblinear'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'elasticnet', 'solver': 'newton-cg'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'elasticnet', 'solver': 'lbfgs'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'elasticnet', 'solver': 'liblinear'}
0.906823 (0.017753) with: {'C': 10, 'penalty': 'l2', 'solver': 'newton-cg'}
0.904323 (0.017802) with: {'C': 10, 'penalty': 'l2', 'solver': 'lbfgs'}
0.907239 (0.018241) with: {'C': 10, 'penalty': 'l2', 'solver': 'liblinear'}[...]<jupyter_text>Logistic Regression using the best parameters:<jupyter_code>logreg = LogisticRegression(C=100,penalty='l1',solver = 'liblinear')
a = logreg.fit(X_train, y_train)
print(a)
y_pred = logreg.predict(X_test)
print(y_pred)
len(y_pred)<jupyter_output>[0 1 0 ... 0 0 0]<jupyter_text>Differents Results of this Logistic regression<jupyter_code>print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))
print('Accuracy of logistic regression classifier on validate set: {:.2f}'.format(logreg.score(X_validate, y_validate)))
print('Accuracy of logistic regression classifier on train set: {:.2f}'.format(logreg.score(X_train, y_train)))
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test,y_pred)
print(confusion_matrix)
print(y_test)
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(logreg,X_test,y_test,cmap="RdYlGn")<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function plot_confusion_matrix is deprecated; Function `plot_confusion_matrix` is deprecated in 1.0 and will be removed in 1.2. Use one of the class methods: ConfusionMatrixDisplay.from_predictions or ConfusionMatrixDisplay.from_estimator.
  warnings.warn(msg, category=FutureWarning)<jupyter_text>A better visualisation of my confusion matrix<jupyter_code>from sklearn.metrics import confusion_matrix
import seaborn as sns

cm = confusion_matrix(y_test, y_pred)
# Normalise
cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fig, ax = plt.subplots(figsize=(3,3))
sns.heatmap(cmn, annot=True, fmt='.2f',cmap="RdYlGn")
plt.title('Unbalanced')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=True)<jupyter_output><empty_output><jupyter_text>Making a classification report to see better my results<jupyter_code>from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.92      0.98      0.95      1404
           1       0.73      0.35      0.47       196

    accuracy                           0.90      1600
   macro avg       0.82      0.67      0.71      1600
weighted avg       0.89      0.90      0.89      1600<jupyter_text>A diffenrent way to see the performance of my ligistic regression<jupyter_code>from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from sklearn.tree import DecisionTreeClassifier, plot_tree
clf = DecisionTreeClassifier(random_state = 0)

fitt = clf.fit(X_train, y_train)

fitt.predict_proba(X_test)
logit_roc_auc = roc_auc_score(y_test, y_pred)
fpr, tpr, thresholds = roc_curve(y_test, logreg.predict_proba(X_test)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Logistic Regression (area = %0.2f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'g-')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.grid(color='grey',linestyle='--', linewidth=0.5)
plt.figure(figsize = (100, 100))
plt.show()<jupyter_output><empty_output><jupyter_text>Oversampling: because my data have a Unbalanced problem<jupyter_code>from imblearn.over_sampling import RandomOverSampler
ros = RandomOverSampler(random_state=0)
X_resampled, y_resampled = ros.fit_resample(X, y)
X_resampled
y_resampled
sns.countplot(y)
sns.countplot(y_resampled).set(title='Count of oversampling data')
X_train_resampled, X_test_resampled, y_train_resampled, y_test_resampled = train_test_split(X_resampled, y_resampled, test_size=0.2, random_state=0)<jupyter_output><empty_output><jupyter_text>Making a new research of hyperparameter with the oversampled data:<jupyter_code># example of grid searching key hyperparametres for logistic regression
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
# define models and parameters
model = LogisticRegression()
solvers = ['newton-cg', 'lbfgs', 'liblinear']
penalty = ['l2','l1','elasticnet']
c_values = [100, 10, 1.0, 0.1, 0.01]
# define grid search
grid = dict(solver=solvers,penalty=penalty,C=c_values)
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='accuracy',error_score=0)
grid_result = grid_search.fit(X_resampled, y_resampled)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py:372: FitFailedWarning: 
750 fits failed out of a total of 1350.
The score on these train-test partitions for these parameters will be set to 0.
If these failures are not expected, you can try to debug them by setting error_score='raise'.

Below are more details about the failures:
--------------------------------------------------------------------------------
150 fits failed with the following error:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py", line 680, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "/usr/local/lib/python3.7/dist-packages/sklearn/linear_model/_logistic.py", line 1461, in fit
    solver = _check_solver(self.solver, self.penalty, self.dual)
  File "/usr/local/lib/python3.7/dist-packages/sklearn/linear_model/_logistic.py", line 449, in _check_solver
    % (solver, penalty)
ValueError: Solv[...]<jupyter_text>I see the bests hyperparameter are the same as before.<jupyter_code>logreg2 = LogisticRegression(C=100,penalty='l1',solver = 'liblinear')
b = logreg2.fit(X_train_resampled, y_train_resampled)
y_pred_resampled = logreg2.predict(X_test_resampled)<jupyter_output><empty_output><jupyter_text>Results on the oversampled data<jupyter_code>print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg2.score(X_test_resampled, y_test_resampled)))
print('Accuracy of logistic regression classifier on train set: {:.2f}'.format(logreg2.score(X_train_resampled, y_train_resampled)))
from sklearn.metrics import confusion_matrix
confusion_matrix_resampled = confusion_matrix(y_test_resampled,y_pred_resampled)
print(confusion_matrix_resampled)
cm = confusion_matrix(y_test_resampled, y_pred_resampled)
# Normalise
cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fig, ax = plt.subplots(figsize=(3,3))
sns.heatmap(cmn, annot=True, fmt='.2f',cmap="RdYlGn")
plt.title('Oversampling')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=True)
from sklearn.metrics import classification_report
print(classification_report(y_test_resampled, y_pred_resampled))
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
logit_roc_auc = roc_auc_score(y_test_resampled, y_pred_resampled)
fpr, tpr, thresholds = roc_curve(y_test_resampled, logreg2.predict_proba(X_test_resampled)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Logistic Regression (area = %0.2f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'g-')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.grid(color='grey',linestyle='--', linewidth=0.5)
plt.figure(figsize = (100, 100))
plt.show()<jupyter_output><empty_output><jupyter_text>Undersampling: verification another example<jupyter_code>from imblearn.under_sampling import RandomUnderSampler
rus = RandomUnderSampler(random_state=0)
X_under, y_under = rus.fit_resample(X, y)
X_under
y_under
sns.countplot(y_under).set(title='Count of undersampling data')
X_train_under, X_test_under, y_train_under, y_test_under = train_test_split(X_under, y_under, test_size=0.2, random_state=0)
# example of grid searching key hyperparametres for logistic regression
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
# define models and parameters
model = LogisticRegression()
solvers = ['newton-cg', 'lbfgs', 'liblinear']
penalty = ['l2','l1','elasticnet']
c_values = [100, 10, 1.0, 0.1, 0.01]
# define grid search
grid = dict(solver=solvers,penalty=penalty,C=c_values)
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='accuracy',error_score=0)
grid_result = grid_search.fit(X_under, y_under)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))<jupyter_output>Best: 0.778161 using {'C': 100, 'penalty': 'l2', 'solver': 'newton-cg'}
0.778161 (0.034141) with: {'C': 100, 'penalty': 'l2', 'solver': 'newton-cg'}
0.754216 (0.027168) with: {'C': 100, 'penalty': 'l2', 'solver': 'lbfgs'}
0.777796 (0.034052) with: {'C': 100, 'penalty': 'l2', 'solver': 'liblinear'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'l1', 'solver': 'newton-cg'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'l1', 'solver': 'lbfgs'}
0.776147 (0.034873) with: {'C': 100, 'penalty': 'l1', 'solver': 'liblinear'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'elasticnet', 'solver': 'newton-cg'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'elasticnet', 'solver': 'lbfgs'}
0.000000 (0.000000) with: {'C': 100, 'penalty': 'elasticnet', 'solver': 'liblinear'}
0.775405 (0.031593) with: {'C': 10, 'penalty': 'l2', 'solver': 'newton-cg'}
0.757324 (0.027437) with: {'C': 10, 'penalty': 'l2', 'solver': 'lbfgs'}
0.775040 (0.031622) with: {'C': 10, 'penalty': 'l2', 'solver': 'liblinear'}[...]<jupyter_text>We have a change in the hyperparameter to use:<jupyter_code>logreg3 = LogisticRegression(C=100,penalty='l2',solver = 'newton-cg')
c = logreg3.fit(X_train_under, y_train_under)
y_pred_under = logreg3.predict(X_test_under)
print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg3.score(X_test_under, y_test_under)))
print('Accuracy of logistic regression classifier on train set: {:.2f}'.format(logreg3.score(X_train_under, y_train_under)))
from sklearn.metrics import confusion_matrix
confusion_matrix_under= confusion_matrix(y_test_under,y_pred_under)
print(confusion_matrix_under)
cm = confusion_matrix(y_test_under, y_pred_under)
# Normalise
cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fig, ax = plt.subplots(figsize=(3,3))
sns.heatmap(cmn, annot=True, fmt='.2f',cmap="RdYlGn")
plt.title('Undersampling')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=True)
from sklearn.metrics import classification_report
print(classification_report(y_test_under, y_pred_under))
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
logit_roc_auc = roc_auc_score(y_test_under, y_pred_under)
fpr, tpr, thresholds = roc_curve(y_test_under, logreg3.predict_proba(X_test_under)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Logistic Regression (area = %0.2f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'g-')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.grid(color='grey',linestyle='--', linewidth=0.5)
plt.figure(figsize = (100, 100))
plt.show()<jupyter_output><empty_output><jupyter_text>Comparaison between the three case<jupyter_code>from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
logit_roc_auc1 = roc_auc_score(y_test, y_pred)
fpr1, tpr1, thresholds1 = roc_curve(y_test, logreg.predict_proba(X_test)[:,1])

logit_roc_auc2 = roc_auc_score(y_test_resampled, y_pred_resampled)
fpr2, tpr2, thresholds2 = roc_curve(y_test_resampled, logreg2.predict_proba(X_test_resampled)[:,1])

logit_roc_auc3 = roc_auc_score(y_test_under, y_pred_under)
fpr3, tpr3, thresholds3 = roc_curve(y_test_under, logreg3.predict_proba(X_test_under)[:,1])
plt.figure()
plt.plot(fpr1, tpr1, label='Logistic Regression (area = %0.2f)' % logit_roc_auc1)
plt.plot(fpr2, tpr2, label='Logistic Regression "Oversampling case" (area = %0.2f)' % logit_roc_auc2)
plt.plot(fpr3, tpr3, label='Logistic Regression "Undersampling case"(area = %0.2f)' % logit_roc_auc3)
plt.plot([0, 1], [0, 1],'r-')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.grid(color='grey',linestyle='--', linewidth=0.5)
plt.figure(figsize = (150, 150))
plt.show()<jupyter_output><empty_output><jupyter_text>For improve my discussion, i will make a second model, The random forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn import model_selection
# random forest model creation
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
# predictions
rfc_predict = rfc.predict(X_test)
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, rfc_predict))
print(classification_report(y_test, rfc_predict))
importances = rfc.feature_importances_
std = np.std([tree.feature_importances_ for tree in rfc.estimators_], axis=0)<jupyter_output><empty_output><jupyter_text>I make a Feature importance to know which variable are the most important to define if the water is safe or not.<jupyter_code>#import pandas as pd
feature_names = [f" {i}" for i in data.columns.values.tolist()[:-1]]
forest_importances = pd.Series(importances,index= feature_names)

fig, ax = plt.subplots()
forest_importances.plot.bar(yerr=std, ax=ax)
ax.set_title("Feature importances using MDI")
ax.set_ylabel("Mean decrease in impurity")
fig.tight_layout()
from sklearn.inspection import permutation_importance

result = permutation_importance(
    rfc, X_test, y_test, n_repeats=10, random_state=42, n_jobs=2
)

forest_importances = pd.Series(result.importances_mean, index=feature_names)
fig, ax = plt.subplots()
forest_importances.plot.bar(yerr=result.importances_std, ax=ax)
ax.set_title("Feature importances using permutation on full model")
ax.set_ylabel("Mean accuracy decrease")
fig.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Running the Hyperparameter for best precision<jupyter_code># define models and parameters
model = RandomForestClassifier()
n_estimators = [10, 100, 1000]
max_features = ['sqrt', 'log2']
# define grid search
grid = dict(n_estimators=n_estimators,max_features=max_features)
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='accuracy',error_score=0)
grid_result = grid_search.fit(X_validate, y_validate)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))
# random forest model creation
rfc_best = RandomForestClassifier(max_features = 'log2', n_estimators =1000)
rfc_best.fit(X_train,y_train)
# predictions
rfc_predict_best = rfc_best.predict(X_test)
print("Confusion Matrix")
print(confusion_matrix(y_test, rfc_predict_best))

print("Classification Report ")
print(classification_report(y_test, rfc_predict_best))

cm = confusion_matrix(y_test, rfc_predict_best)
# Normalise
cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fig, ax = plt.subplots(figsize=(3,3))
sns.heatmap(cmn, annot=True, fmt='.2f',cmap="RdYlGn")
plt.title('RFC')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=True)<jupyter_output><empty_output><jupyter_text>With the Data oversampled:<jupyter_code># predictions
rfc_predict_best_resampled = rfc_best.predict(X_test_resampled)
print("Confusion Matrix")
print(confusion_matrix(y_test_resampled, rfc_predict_best_resampled))
print("Classification Report")
print(classification_report(y_test_resampled, rfc_predict_best_resampled))

cm = confusion_matrix(y_test_resampled, rfc_predict_best_resampled)
# Normalise
cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fig, ax = plt.subplots(figsize=(3,3))
sns.heatmap(cmn, annot=True, fmt='.2f',cmap="RdYlGn")
plt.title('Oversampling RFC')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=True)
from yellowbrick.classifier import classification_report


# Specify the target classes
classes = ["not safe", "safe"]


# Instantiate the visualizer
classification_report(
    rfc_best, X_train_resampled, y_train_resampled, X_test_resampled, y_test_resampled,classes = classes, support=True,cmap="RdYlGn")
classification_report(
    logreg, X_train, y_train, X_test, y_test,classes = classes, support=True,cmap="RdYlGn")<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/base.py:451: UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names
  "X does not have valid feature names, but"
{'fraction_non_alphanumeric': 0.13917525773195877, 'fraction_numerical': 0.05625102274586811, 'mean_word_length': 5.056739345887017, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 82, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '525029', 'license': 'MIT', 'n_tokens_mistral': 10049, 'n_tokens_neox': 9111, 'n_words': 1946, 'path': 'Project_machine_Learning.ipynb', 'repo_name': 'Aerotycx/2022_ML_Earth_Env_Sci', 'size': '24444'}