# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd

from sklearn import model_selection

import matplotlib.pyplot as plt

# %matplotlib inline


# -

df = pd.read_csv("/data/insurance.csv")
df.head()

# +
target = "charges"

y = df[target]
X = df.drop(columns=[target])
# -

pd.get_dummies(X, drop_first=True)

# +
target = "charges"

y = df[target]
X = df.drop(columns=[target])

X = pd.get_dummies(X, drop_first=True)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y
                                        , test_size = 0.3, random_state = 1)

# -

import numpy as np

np.random.seed(1234)
np.random.randn(5)

np.random.randn(5)

from sklearn import linear_model

# +
target = "charges"

y = df[target]
X = df.drop(columns=[target])

X = pd.get_dummies(X, drop_first=True)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y
                                        , test_size = 0.3, random_state = 1)


est = linear_model.LinearRegression()
est.fit(X_train, y_train)
# -

y_train_pred = est.predict(X_train)
y_test_pred = est.predict(X_test)

residual = y_train_pred - y_train
plt.scatter(y_train, residual)
plt.xlabel("Actual")
plt.ylabel("Residual")
plt.title("Residual plot")

plt.hist(residual, bins = 35);

SSE_train = ((y_train_pred - y_train) ** 2).sum()
SSE_test = ((y_test_pred - y_test) ** 2).sum()

MSE_train = ((y_train_pred - y_train) ** 2).mean()
MSE_test = ((y_test_pred - y_test) ** 2).mean()

MSE_train/MSE_test

SST = ((y_train.mean() - y_train) ** 2).sum()

SSE_train/SST

r2 = 1 - SSE_train/SST
r2

from sklearn import metrics

metrics.r2_score(y_train, y_train_pred)

metrics.r2_score(y_test, y_test_pred)

# +
target = "charges"

y = df[target]
X = df.drop(columns=[target])

X = pd.get_dummies(X, drop_first=True)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y
                                        , test_size = 0.3, random_state = 1)


est = linear_model.LinearRegression()
est.fit(X_train, y_train)

y_train_pred = est.predict(X_train)
y_test_pred = est.predict(X_test)

print("r2 train", metrics.r2_score(y_train, y_train_pred))
print("r2 test", metrics.r2_score(y_test, y_test_pred))

print("rmse train", np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print("rmse test", np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# -

6063 / np.std(y_train)

est.coef_

pd.DataFrame({"feature": X.columns, "coefficient": est.coef_})

est.intercept_

from sklearn import preprocessing

# +
target = "charges"

y = np.log(df[target])
X = df.drop(columns=[target])
X["high_bmi"] = np.where(X.bmi>30, 1, 0)
X["age_group"] = np.where(X.age>60, 1, 0)


X = pd.get_dummies(X, drop_first=True)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X.values.astype("float")
                                    , y , test_size = 0.3, random_state = 1)



poly = preprocessing.PolynomialFeatures(degree=2, include_bias=False)
X_train = poly.fit_transform(X_train)
X_test = poly.fit_transform(X_test)

scaler = preprocessing.StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

est = linear_model.SGDRegressor(random_state=1, eta0=0.001, tol=1e-5, learning_rate="constant"
                            , max_iter= 10000)
est.fit(X_train, y_train)

y_train_pred = est.predict(X_train)
y_test_pred = est.predict(X_test)

print("r2 train", metrics.r2_score(y_train, y_train_pred))
print("r2 test", metrics.r2_score(y_test, y_test_pred))

print("rmse train", np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print("rmse test", np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# -

a = np.random.random((4, 100))
print(a.shape)
preprocessing.PolynomialFeatures(degree=3,include_bias=False).fit_transform(a).shape





X

est.coef_

pd.DataFrame(X_train).describe()

X

# +
a = np.array([
    [1, 2, 3],
    [1, 1, 1],
    [4, 3, 2],
    [-1, 0, 1]
])

poly = preprocessing.PolynomialFeatures(degree=2, include_bias=False)
poly.fit_transform(a)
# -

x = np.random.random([1000])
y = 1 - 3.5 * x + 2.4 * x ** 2
plt.scatter(x, y)

lr = linear_model.LinearRegression()
lr.fit(x.reshape((-1,1)), y)
y_pred = lr.predict(x.reshape((-1,1)))
plt.scatter(x, y, color = "red", label = "actual")
plt.scatter(x, y_pred, color = "blue", label = "prediction")
plt.legend()

lr.coef_, lr.intercept_


# +
x = np.random.random([1000])
y = 1 - 3.5 * x + 2.4 * x ** 2
x = x.reshape((-1, 1))
plt.scatter(x, y)

pipe = pipeline.Pipeline([
    ("poly", preprocessing.PolynomialFeatures(degree=2, include_bias=False)),
    ("scaler", preprocessing.StandardScaler()),
    ("est",linear_model.SGDRegressor(random_state=13123, eta0=0.001, tol=1e-5, alpha = 0.0
                            , learning_rate="constant"
                            , max_iter= 1000))
])
                               
pipe.fit(x, y)

y_pred = pipe.predict(x)
plt.scatter(x.flatten(), y, color = "red", label = "actual", )
plt.scatter(x.flatten(), y_pred, color = "blue", label = "prediction", alpha = 0.2)
est = pipe.steps[-1][-1]
print(est.coef_, est.intercept_)
# -

x_poly



lr.intercept_, lr.coef_

from sklearn import pipeline

# +
target = "charges"

y = np.log(df[target])
X = df.drop(columns=[target])
X["high_bmi"] = np.where(X.bmi>30, 1, 0)
X["age_group"] = np.where(X.age>60, 1, 0)


X = pd.get_dummies(X, drop_first=True)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X.values.astype("float")
                                    , y , test_size = 0.3, random_state = 1)


pipe = pipeline.Pipeline([
    ("poly", preprocessing.PolynomialFeatures(degree=2, include_bias=False)),
    ("scaler", preprocessing.StandardScaler()),
    ("est",linear_model.SGDRegressor(random_state=1, eta0=0.001, tol=1e-5
                            , learning_rate="constant"
                            , max_iter= 10000))
])
pipe.fit(X_train, y_train)

y_train_pred = pipe.predict(X_train)
y_test_pred = pipe.predict(X_test)

print("r2 train", metrics.r2_score(y_train, y_train_pred))
print("r2 test", metrics.r2_score(y_test, y_test_pred))

print("rmse train", np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print("rmse test", np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# -

import pickle

with open("model.pkl", "wb") as f:
    pickle.dump(pipe, f)

with open("model.pkl", "rb") as f:
    model = pickle.load(f)

model

# +
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

print("r2 train", metrics.r2_score(y_train, y_train_pred))
print("r2 test", metrics.r2_score(y_test, y_test_pred))

print("rmse train", np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print("rmse test", np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# -



StarcoderdataJupyterScriptsDedupFiltered