-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLR_sklearn.py
More file actions
51 lines (35 loc) · 1.04 KB
/
Copy pathMLR_sklearn.py
File metadata and controls
51 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Enes Sadi Uysal
#%%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
df = pd.read_csv('insurance.csv')
df.drop('region', inplace = True, axis = 1)
def sex(each):
if each == 'male':
return 0
else:
return 1
df['sex'] = df['sex'].apply(sex)
def smoker(each):
if each == 'yes':
return 1
else:
return 0
df['smoker'] = df['smoker'].apply(smoker)
df = (df - np.mean(df)) / np.std(df)
x = df.iloc[:, :-1]
y = df.iloc[:,-1]
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.3)
lm = LinearRegression()
lm.fit(x_train, y_train)
thetas = pd.DataFrame(lm.coef_, x.columns,columns = ["Coeff"])
predictions = lm.predict(x_test)
plt.scatter(y_test,predictions)
loss = metrics.mean_squared_error(y_test, predictions)
variance = metrics.explained_variance_score(y_test, predictions)
r2 = metrics.r2_score(y_test, predictions)