# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
#data load and look up
import pandas as pd
import numpy as np

data = pd.read_csv('/kaggle/input/insurance/insurance.csv')
data.head()
data.describe()
data.info()
data.isnull().sum() #there is no missing data()
"""
I like to see correlation of features. 
"""
import matplotlib.pyplot as plt
import seaborn as sns
correlation = data.corr()
plt.figure()
sns.heatmap(correlation, annot = True)
"""
there is no clear relationship between numerical features. Let's include categorical features in the game!
"""
data_categ = data.select_dtypes(include=object)
data_categ.head()
#converting categorical data
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

for col in data_categ.head():
    data[col]=le.fit_transform(data_categ[col])
data.head()
data.info()
"""
# Linaer Regression
"""
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X = data.iloc[:,:-1].values
y = data.iloc[:,-1].values

x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state = 42)
linReg=LinearRegression()
linReg.fit(x_train,y_train)
from sklearn.metrics import r2_score

y_pred=linReg.predict(x_test)
print(r2_score(y_test,y_pred))
"""
r2score not bad. Let's see other methods
"""
from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=2)
x_poly = poly.fit_transform(X)

x_train1, x_test1, y_train1, y_test1 = train_test_split(x_poly,y, test_size=0.2, random_state = 42)
polyLR = linReg.fit(x_train1,y_train1)

y_pred1 = polyLR.predict(x_test1)
print(r2_score(y_test1,y_pred))
print(polyLR.score(x_test1,y_test))
"""
Now we have obtained better score. As you see above, you can use both method. r2 score and score is the same****
"""
"""
# Decision Tree
"""
from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor()
tree_reg.fit(x_train,y_train)

print(tree_reg.score(x_test,y_test))
"""
**Random Forest**
"""
from sklearn.ensemble import RandomForestRegressor
rf_reg = RandomForestRegressor()
rf_reg.fit(x_train,y_train)

print(rf_reg.score(x_test,y_test))