Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # %%
- #insurance
- # %%
- import pandas as pd
- df = pd.read_csv('insurance.csv')
- df
- # %%
- df['sex'] = df['sex'].astype('category')
- df['sex'] = df['sex'].cat.codes
- df
- # %%
- df['smoker'] = df['smoker'].astype('category')
- df['smoker'] = df['smoker'].cat.codes
- df['region'] = df['region'].astype('category')
- df['region'] = df['region'].cat.codes
- df
- # %%
- df.isnull().sum()
- # %%
- x = df.drop(columns='charges')
- x
- # %%
- y = df['charges']
- from sklearn.model_selection import train_test_split
- X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=0.3, random_state=0)
- from sklearn.linear_model import LinearRegression
- lr = LinearRegression()
- lr.fit(X_train,y_train)
- # %%
- c = lr.intercept_
- c
- # %%
- m=lr.coef_
- m
- # %%
- y_pred_train = lr.predict(X_train)
- y_pred_train
- # %%
- import matplotlib.pyplot as plt
- # Create the scatter plot
- fig, ax = plt.subplots()
- scatter = ax.scatter(y_train, y_pred_train, c='#3f3f3f', picker=True) # Enable picking
- plt.xlabel('Actual Charges')
- plt.ylabel('Predicted Charges')
- # Function to handle pick events (clicking on a point)
- def on_pick(event):
- ind = event.ind[0] # Get the index of the clicked point
- actual = y_train.iloc[ind]
- predicted = y_pred_train[ind]
- # Create or update the annotation
- if hasattr(on_pick, 'annotation'):
- on_pick.annotation.remove() # Remove the previous annotation
- on_pick.annotation = ax.annotate(
- f"Actual: {actual:.2f}\nPredicted: {predicted:.2f}",
- xy=(y_train.iloc[ind], y_pred_train[ind]),
- xytext=(10, 10), textcoords='offset points',
- bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
- arrowprops=dict(arrowstyle='->')
- )
- fig.canvas.draw() # Redraw the figure to show the annotation
- # Connect the pick event to the function
- fig.canvas.mpl_connect('pick_event', on_pick)
- # Show the plot
- plt.show()
- # %%
- from sklearn.metrics import mean_absolute_error, r2_score
- mae = mean_absolute_error(y_train, y_pred_train)
- r2 = r2_score(y_train, y_pred_train)
- print(f"MAE: {mae}")
- print(f"R-squared: {r2}")
- # %%
- y_pred_test = lr.predict(X_test)
- y_pred_test
- # %%
- import matplotlib.pyplot as plt
- # Create the scatter plot
- fig, ax = plt.subplots()
- scatter = ax.scatter(y_train, y_pred_train, c='#3f3f3f', picker=True) # Enable picking
- plt.xlabel('Actual Charges')
- plt.ylabel('Predicted Charges')
- # Function to handle pick events (clicking on a point)
- def on_pick(event):
- ind = event.ind[0] # Get the index of the clicked point
- actual = y_test.iloc[ind]
- predicted = y_pred_test[ind]
- # Create or update the annotation
- if hasattr(on_pick, 'annotation'):
- on_pick.annotation.remove() # Remove the previous annotation
- on_pick.annotation = ax.annotate(
- f"Actual: {actual:.2f}\nPredicted: {predicted:.2f}",
- xy=(y_test.iloc[ind], y_pred_test[ind]),
- xytext=(10, 10), textcoords='offset points',
- bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
- arrowprops=dict(arrowstyle='->')
- )
- fig.canvas.draw() # Redraw the figure to show the annotation
- # Connect the pick event to the function
- fig.canvas.mpl_connect('pick_event', on_pick)
- # Show the plot
- plt.show()
- # %%
- from sklearn.metrics import mean_absolute_error, r2_score
- mae = mean_absolute_error(y_train, y_pred_train)
- r2 = r2_score(y_test, y_pred_test)
- print(f"MAE: {mae}")
- print(f"R-squared: {r2}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement