Advertisement
amu2002

ann

Nov 20th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1.  
  2. import pandas as pd
  3. import seaborn as sns
  4.  
  5. df = pd.read_csv('./Churn_Modelling.csv')
  6.  
  7. df.shape
  8.  
  9. df.columns
  10.  
  11. df.head()
  12.  
  13. x = df[['CreditScore','Age','Tenure','Balance','NumOfProducts','HasCrCard',
  14.         'IsActiveMember','EstimatedSalary']]
  15.  
  16. y = df['Exited']
  17.  
  18. x
  19.  
  20. sns.countplot(x = y);
  21.  
  22. y.value_counts()
  23.  
  24. from sklearn.preprocessing import StandardScaler
  25.  
  26. scaler = StandardScaler()
  27.  
  28. x_scaled = scaler.fit_transform(x)
  29.  
  30. x_scaled
  31.  
  32. from sklearn.model_selection import train_test_split
  33.  
  34. x_train, x_test, y_train, y_test = train_test_split(x_scaled,y,random_state =0, test_size=0.25)
  35.  
  36. x.shape
  37.  
  38. x_test.shape
  39.  
  40. x_train.shape
  41.  
  42. from sklearn.neural_network import MLPClassifier
  43.  
  44. ann = MLPClassifier(hidden_layer_sizes=(100,100,100), random_state=0,
  45.                     max_iter=100, activation = 'relu')
  46.  
  47. ann.fit(x_train, y_train)
  48.  
  49. y_pred = ann.predict(x_test)
  50.  
  51. y_pred
  52.  
  53. from sklearn.metrics import ConfusionMatrixDisplay, classification_report
  54. from sklearn.metrics import accuracy_score
  55.  
  56. y_test.value_counts()
  57.  
  58. ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
  59.  
  60. before = accuracy_score(y_test, y_pred)
  61. before
  62.  
  63. print(classification_report(y_test, y_pred))
  64.  
  65. from imblearn.over_sampling import RandomOverSampler
  66. ros = RandomOverSampler(random_state=0)
  67.  
  68. x_res, y_res = ros.fit_resample(x, y)
  69.  
  70. y_res.value_counts()
  71.  
  72. scaler = StandardScaler()
  73. x_scaled = scaler.fit_transform(x_res)
  74. x_scaled
  75.  
  76. x_train, x_test, y_train, y_test = train_test_split(x_scaled,y_res,random_state =0, test_size=0.25)
  77.  
  78. x_res.shape
  79.  
  80. x_test.shape
  81.  
  82. x_train.shape
  83.  
  84. ann = MLPClassifier(hidden_layer_sizes=(100,100,100), random_state=0,
  85.                     max_iter=100, activation = 'relu')
  86.  
  87. ann.fit(x_train, y_train)
  88.  
  89. y_pred = ann.predict(x_test)
  90. y_pred
  91.  
  92. y_test.value_counts()
  93.  
  94. ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
  95.  
  96. after = accuracy_score(y_test, y_pred)
  97. after
  98.  
  99. print(classification_report(y_test, y_pred))
  100.  
  101. import matplotlib.pyplot as plt
  102.  
  103. accuracy_scores = [before, after]
  104.  
  105. labels = ['Before Optimization', 'After Optimization']
  106.  
  107. plt.figure(figsize=(8, 5))
  108. plt.plot(labels, accuracy_scores, marker='o', linestyle='-', color='b')
  109. plt.title('Comparison of ANN Accuracy Scores')
  110. plt.xlabel('Optimization Status')
  111. plt.ylabel('Accuracy Score')
  112. plt.ylim(0.8, 0.9)
  113. plt.grid(True)
  114. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement