Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import seaborn as sns
- df = pd.read_csv('./Churn_Modelling.csv')
- df.shape
- df.columns
- df.head()
- x = df[['CreditScore','Age','Tenure','Balance','NumOfProducts','HasCrCard',
- 'IsActiveMember','EstimatedSalary']]
- y = df['Exited']
- x
- sns.countplot(x = y);
- y.value_counts()
- from sklearn.preprocessing import StandardScaler
- scaler = StandardScaler()
- x_scaled = scaler.fit_transform(x)
- x_scaled
- from sklearn.model_selection import train_test_split
- x_train, x_test, y_train, y_test = train_test_split(x_scaled,y,random_state =0, test_size=0.25)
- x.shape
- x_test.shape
- x_train.shape
- from sklearn.neural_network import MLPClassifier
- ann = MLPClassifier(hidden_layer_sizes=(100,100,100), random_state=0,
- max_iter=100, activation = 'relu')
- ann.fit(x_train, y_train)
- y_pred = ann.predict(x_test)
- y_pred
- from sklearn.metrics import ConfusionMatrixDisplay, classification_report
- from sklearn.metrics import accuracy_score
- y_test.value_counts()
- ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
- before = accuracy_score(y_test, y_pred)
- before
- print(classification_report(y_test, y_pred))
- from imblearn.over_sampling import RandomOverSampler
- ros = RandomOverSampler(random_state=0)
- x_res, y_res = ros.fit_resample(x, y)
- y_res.value_counts()
- scaler = StandardScaler()
- x_scaled = scaler.fit_transform(x_res)
- x_scaled
- x_train, x_test, y_train, y_test = train_test_split(x_scaled,y_res,random_state =0, test_size=0.25)
- x_res.shape
- x_test.shape
- x_train.shape
- ann = MLPClassifier(hidden_layer_sizes=(100,100,100), random_state=0,
- max_iter=100, activation = 'relu')
- ann.fit(x_train, y_train)
- y_pred = ann.predict(x_test)
- y_pred
- y_test.value_counts()
- ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
- after = accuracy_score(y_test, y_pred)
- after
- print(classification_report(y_test, y_pred))
- import matplotlib.pyplot as plt
- accuracy_scores = [before, after]
- labels = ['Before Optimization', 'After Optimization']
- plt.figure(figsize=(8, 5))
- plt.plot(labels, accuracy_scores, marker='o', linestyle='-', color='b')
- plt.title('Comparison of ANN Accuracy Scores')
- plt.xlabel('Optimization Status')
- plt.ylabel('Accuracy Score')
- plt.ylim(0.8, 0.9)
- plt.grid(True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement