Advertisement
brandblox

Lab_ML(10/02/25)

Feb 10th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 KB | None | 0 0
  1. # %%
  2. #import
  3. import pandas as pd
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from matplotlib.colors import  ListedColormap
  7. import seaborn as sns
  8. from sklearn.preprocessing import LabelEncoder
  9. from sklearn.preprocessing import StandardScaler
  10. from sklearn.model_selection import train_test_split
  11. from sklearn.naive_bayes import GaussianNB
  12. from sklearn import metrics
  13. from sklearn.metrics import accuracy_score
  14. from sklearn.metrics import classification_report
  15. from sklearn.metrics import precision_recall_curve
  16. from sklearn.metrics import confusion_matrix
  17. from sklearn.metrics import f1_score
  18.  
  19. # %%
  20. df_net = pd.read_csv('Social_Network_Ads.csv')
  21. df_net.head()
  22.  
  23. # %%
  24. df_net.drop(columns=['User ID'], inplace=True)
  25. df_net.head()
  26.  
  27. # %%
  28. df_net.describe()
  29.  
  30. # %%
  31. sns.displot(df_net['EstimatedSalary'])
  32.  
  33. # %%
  34. le = LabelEncoder()
  35. df_net['Gender'] = le.fit_transform(df_net['Gender'])
  36.  
  37. # %%
  38. df_net.corr()
  39. sns.heatmap(df_net.corr())
  40.  
  41. # %%
  42. df_net.drop(columns=['Gender'],inplace=True)
  43. df_net
  44.  
  45. # %%
  46. X = df_net.iloc[:, :-1].values
  47. y = df_net.iloc[:, -1].values
  48.  
  49. # %%
  50. X_train, X_test, y_train, y_test= train_test_split(X,y,test_size = 0.25, random_state = True)
  51.  
  52.  
  53. # %%
  54. sc = StandardScaler()
  55. X_train = sc.fit_transform(X_train)
  56. X_test = sc.transform(X_test)
  57.  
  58. # %%
  59. classifier = GaussianNB()
  60. classifier.fit(X_train, y_train)
  61.  
  62. # %%
  63. y_pred = classifier.predict(X_test)
  64. print(np.concatenate((y_pred.reshape(len(y_pred),1),y_test.reshape(len(y_test),1)),1))
  65.  
  66. # %%
  67. accuracy_score(y_test, y_pred)
  68.  
  69. # %%
  70. print(f'Classification Reaport: \n{classification_report(y_test,y_pred)}')
  71.  
  72. # %%
  73. print(f'F1 score :{f1_score(y_test,y_pred)}')
  74.  
  75. # %%
  76. cf_matrix = confusion_matrix(y_test,y_pred)
  77. sns.heatmap(cf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)
  78.  
  79. # %%
  80. y_pred_proba = classifier.predict_proba(X_test)[:,1]
  81. precision, recall, thresholds = precision_recall_curve(y_test, y_pred_proba)
  82.  
  83. # %%
  84. fig, ax = plt.subplots(figsize=(6,6))
  85. ax.plot(recall,precision, label = "Naive Bayes Classifier", color = 'firebrick')
  86. ax.set_title('Precison-Recall Curve')
  87. ax.set_xlabel('Recall')
  88. ax.set_ylabel('Precision')
  89. plt.box(False)
  90. ax.legend()
  91.  
  92. # %%
  93. # Plot AUC/ROC curve
  94. y_pred_proba = classifier.predict_proba(X_test)[:,1]
  95. fpr, tpr, thresholds = metrics.roc_curve(y_test, y_pred_proba)
  96. fig, ax = plt.subplots(figsize=(6,6))
  97. ax.plot(fpr, tpr, label='Naive Bayes Classification', color = 'firebrick')
  98. ax.set_title("ROC Curve")
  99. ax.set_xlabel('False Positive Rate')
  100. ax.set_ylabel('True Positive Rate')
  101. plt.box( False)
  102. ax. legend();
  103.  
  104. # %%
  105. # Predict purchase with Age(45) and Salary(97000)
  106. print(classifier.predict(sc.transform([[45, 97000]])))
  107.  
  108. # %%
  109. # Visualize prediction results on the training set
  110. X_set, y_set = sc.inverse_transform(X_train), y_train
  111.  
  112. # Create a meshgrid for plotting
  113. X1, X2 = np.meshgrid(
  114.     np.arange(start=X_set[:, 0].min() - 10, stop=X_set[:, 0].max() + 10, step=1),
  115.     np.arange(start=X_set[:, 1].min() - 1000, stop=X_set[:, 1].max() + 1000, step=1)
  116. )
  117.  
  118. # Plot the decision boundary
  119. plt.contourf(X1, X2,
  120.              classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
  121.              alpha=0.75, cmap=ListedColormap(['red', 'green']))  # Fixed colormap
  122.  
  123. # Set axis limits
  124. plt.xlim(X1.min(), X1.max())
  125. plt.ylim(X2.min(), X2.max())
  126.  
  127. # Scatter plot for each class
  128. for i, j in enumerate(np.unique(y_set)):
  129.     plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
  130.                 c=ListedColormap(['red', 'green'])(i), label=j)
  131.  
  132. # Add labels, title, and legend
  133. plt.title('Naive Bayes (Training set)')
  134. plt.xlabel('Age')
  135. plt.ylabel('Estimated Salary')
  136. plt.legend()
  137.  
  138. # Show plot
  139. plt.show()
  140.  
  141.  
  142. # %%
  143. # Visualize prediction results on the test set
  144. X_set, y_set = sc.inverse_transform(X_test), y_test  # Use the test set
  145.  
  146. # Create a meshgrid for plotting
  147. X1, X2 = np.meshgrid(
  148.     np.arange(start=X_set[:, 0].min() - 10, stop=X_set[:, 0].max() + 10, step=1),
  149.     np.arange(start=X_set[:, 1].min() - 1000, stop=X_set[:, 1].max() + 1000, step=1)
  150. )
  151.  
  152. # Plot the decision boundary
  153. plt.contourf(X1, X2,
  154.              classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
  155.              alpha=0.75, cmap=ListedColormap(['red', 'green']))  # Fixed colormap
  156.  
  157. # Set axis limits
  158. plt.xlim(X1.min(), X1.max())
  159. plt.ylim(X2.min(), X2.max())
  160.  
  161. # Scatter plot for each class
  162. for i, j in enumerate(np.unique(y_set)):
  163.     plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
  164.                 c=ListedColormap(['red', 'green'])(i), label=j)
  165.  
  166. # Add labels, title, and legend
  167. plt.title('Naive Bayes (Test set)')
  168. plt.xlabel('Age')
  169. plt.ylabel('Estimated Salary')
  170. plt.legend()
  171.  
  172. # Show plot
  173. plt.show()
  174.  
  175. # Predict class for Age = 65 and Salary = 160000
  176. prediction = classifier.predict(sc.transform([[65, 160000]]))
  177.  
  178. # Map prediction to label (if 0 = "Not Purchased" and 1 = "Purchased")
  179. labels = ['Not Purchased', 'Purchased']  # Assuming class 0 is 'Not Purchased' and class 1 is 'Purchased'
  180. print(f'Prediction: {labels[prediction[0]]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement