-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_learning_error_metrics.py
65 lines (37 loc) · 1.18 KB
/
machine_learning_error_metrics.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""Machine Learning Error Metrics.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1T1i11V5YlyZE2RsEj1V3zzDqWwlJFxXk
"""
# MSE RMSE MAE MAPE
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error,mean_absolute_percentage_error
df=pd.read_csv('insurance.csv')
df.head(3)
df = pd.get_dummies(df, columns=["sex", "smoker", "region"])
y=df[["charges"]]
x=df.drop("charges",axis=1)
lm=LinearRegression()
model=lm.fit(x,y)
model.score(x,y)
df.head(3)
model.predict([[22,27,0,0,1,0,1,0,0,0,1]])
df_hata=pd.DataFrame()
df_hata["y"]=y
y_tahmin=model.predict(x)
df_hata["tahmin"]=y_tahmin
df_hata.head(3)
df_hata["error"]=y-y_tahmin
df_hata.head(7)
"""Long Way"""
df_hata["squared_error"]=np.square(df_hata["error"]) #df_hata["squared_error"]=df_hata["error"]**2
df_hata["abs_error"]=np.abs(df_hata["error"])
df_hata["percent_error"]=np.abs((y-y_tahmin)/y)
df_hata.mean()
"""Short Way with Methods"""
mean_squared_error(y,y_tahmin)
mean_absolute_error(y,y_tahmin)
mean_absolute_percentage_error(y,y_tahmin)