Advertisement
brandblox

Lab_ML(27/01/25)

Jan 27th, 2025 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. # %%
  2. #insurance
  3.  
  4. # %%
  5. import pandas as pd
  6. df = pd.read_csv('insurance.csv')
  7. df
  8.  
  9. # %%
  10. df['sex'] = df['sex'].astype('category')
  11. df['sex'] = df['sex'].cat.codes
  12. df
  13.  
  14. # %%
  15. df['smoker'] = df['smoker'].astype('category')
  16. df['smoker'] = df['smoker'].cat.codes
  17.  
  18. df['region'] = df['region'].astype('category')
  19. df['region'] = df['region'].cat.codes
  20. df
  21.  
  22. # %%
  23. df.isnull().sum()
  24.  
  25. # %%
  26. x = df.drop(columns='charges')
  27. x
  28.  
  29. # %%
  30. y = df['charges']
  31.  
  32. from sklearn.model_selection import train_test_split
  33. X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=0.3, random_state=0)
  34.  
  35. from sklearn.linear_model import LinearRegression
  36. lr = LinearRegression()
  37. lr.fit(X_train,y_train)
  38.  
  39. # %%
  40. c = lr.intercept_
  41. c
  42.  
  43. # %%
  44. m=lr.coef_
  45. m
  46.  
  47. # %%
  48. y_pred_train = lr.predict(X_train)
  49. y_pred_train
  50.  
  51. # %%
  52. import matplotlib.pyplot as plt
  53.  
  54. # Create the scatter plot
  55. fig, ax = plt.subplots()
  56. scatter = ax.scatter(y_train, y_pred_train, c='#3f3f3f', picker=True)  # Enable picking
  57. plt.xlabel('Actual Charges')
  58. plt.ylabel('Predicted Charges')
  59.  
  60. # Function to handle pick events (clicking on a point)
  61. def on_pick(event):
  62.     ind = event.ind[0]  # Get the index of the clicked point
  63.     actual = y_train.iloc[ind]
  64.     predicted = y_pred_train[ind]
  65.    
  66.     # Create or update the annotation
  67.     if hasattr(on_pick, 'annotation'):
  68.         on_pick.annotation.remove()  # Remove the previous annotation
  69.     on_pick.annotation = ax.annotate(
  70.         f"Actual: {actual:.2f}\nPredicted: {predicted:.2f}",
  71.         xy=(y_train.iloc[ind], y_pred_train[ind]),
  72.         xytext=(10, 10), textcoords='offset points',
  73.         bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
  74.         arrowprops=dict(arrowstyle='->')
  75.     )
  76.     fig.canvas.draw()  # Redraw the figure to show the annotation
  77.  
  78. # Connect the pick event to the function
  79. fig.canvas.mpl_connect('pick_event', on_pick)
  80.  
  81. # Show the plot
  82. plt.show()
  83.  
  84. # %%
  85. from sklearn.metrics import mean_absolute_error, r2_score
  86.  
  87. mae = mean_absolute_error(y_train, y_pred_train)
  88. r2 = r2_score(y_train, y_pred_train)
  89. print(f"MAE: {mae}")
  90. print(f"R-squared: {r2}")
  91.  
  92. # %%
  93. y_pred_test = lr.predict(X_test)
  94. y_pred_test
  95.  
  96. # %%
  97. import matplotlib.pyplot as plt
  98.  
  99. # Create the scatter plot
  100. fig, ax = plt.subplots()
  101. scatter = ax.scatter(y_train, y_pred_train, c='#3f3f3f', picker=True)  # Enable picking
  102. plt.xlabel('Actual Charges')
  103. plt.ylabel('Predicted Charges')
  104.  
  105. # Function to handle pick events (clicking on a point)
  106. def on_pick(event):
  107.     ind = event.ind[0]  # Get the index of the clicked point
  108.     actual = y_test.iloc[ind]
  109.     predicted = y_pred_test[ind]
  110.    
  111.     # Create or update the annotation
  112.     if hasattr(on_pick, 'annotation'):
  113.         on_pick.annotation.remove()  # Remove the previous annotation
  114.     on_pick.annotation = ax.annotate(
  115.         f"Actual: {actual:.2f}\nPredicted: {predicted:.2f}",
  116.         xy=(y_test.iloc[ind], y_pred_test[ind]),
  117.         xytext=(10, 10), textcoords='offset points',
  118.         bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
  119.         arrowprops=dict(arrowstyle='->')
  120.     )
  121.     fig.canvas.draw()  # Redraw the figure to show the annotation
  122.  
  123. # Connect the pick event to the function
  124. fig.canvas.mpl_connect('pick_event', on_pick)
  125.  
  126. # Show the plot
  127. plt.show()
  128.  
  129. # %%
  130. from sklearn.metrics import mean_absolute_error, r2_score
  131.  
  132. mae = mean_absolute_error(y_train, y_pred_train)
  133. r2 = r2_score(y_test, y_pred_test)
  134. print(f"MAE: {mae}")
  135. print(f"R-squared: {r2}")
  136.  
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement