<jupyter_start><jupyter_text>Regresión lineal simple en Python El paquete statsmodel para regresión lineal<jupyter_code>import pandas as pd
import numpy as np
data = pd.read_csv("../datasets/ads/Advertising.csv")
data.head()
import statsmodels.formula.api as smf
lm = smf.ols(formula="Sales~TV", data = data).fit()
lm.params<jupyter_output><empty_output><jupyter_text>El modelo lineal predictivo sería Sales = 7.032594 + 0.047537 * TV<jupyter_code>lm.pvalues
lm.rsquared
lm.rsquared_adj
lm.summary()
sales_pred = lm.predict(pd.DataFrame(data["TV"]))
sales_pred
import matplotlib.pyplot as plt
%matplotlib inline
data.plot(kind = "scatter", x = "TV", y ="Sales")
plt.plot(pd.DataFrame(data["TV"]), sales_pred, c="red", linewidth = 2)
data["sales_pred"] = 7.032594 + 0.047537*data["TV"]
data["RSE"] = (data["Sales"]-data["sales_pred"])**2
SSD = sum(data["RSE"])
SSD
RSE = np.sqrt(SSD/(len(data)-2))
RSE
sales_m = np.mean(data["Sales"])
sales_m
error = RSE/sales_m
error
plt.hist((data["Sales"]-data["sales_pred"]))<jupyter_output><empty_output><jupyter_text>Regresión lineal múltiple en Python El paquete statsmodel para regresión múltiple* Sales ~TV* Sales ~Newspaper* Sales ~Radio* Sales ~TV+Newspaper* Sales ~TV+Radio* Sales ~Newspaper+Radio* Sales ~TV+Newspaper+Radio<jupyter_code>#Añadir el Newspaper al modelo existente
lm2 = smf.ols(formula="Sales~TV+Newspaper", data = data).fit()
lm2.params
lm2.pvalues<jupyter_output><empty_output><jupyter_text>Sales = 5.774948+0.046901*TV + 0.044219*Newspaper<jupyter_code>lm2.rsquared
lm2.rsquared_adj
sales_pred = lm2.predict(data[["TV", "Newspaper"]])
sales_pred
SSD = sum((data["Sales"]-sales_pred)**2)
SSD
RSE = np.sqrt(SSD/(len(data)-2-1))
RSE
error = RSE / sales_m
error
lm2.summary()
#Añadir la Radio al modelo existente
lm3 = smf.ols(formula="Sales~TV+Radio", data = data).fit()
lm3.summary()
sales_pred = lm3.predict(data[["TV", "Radio"]])
SSD = sum((data["Sales"]-sales_pred)**2)
RSE = np.sqrt(SSD/(len(data)-2-1))
RSE
RSE/sales_m
#Añadir la Radio al modelo existente
lm4 = smf.ols(formula="Sales~TV+Radio+Newspaper", data = data).fit()
lm4.summary()
sales_pred = lm4.predict(data[["TV", "Radio","Newspaper"]])
SSD = sum((data["Sales"]-sales_pred)**2)
RSE = np.sqrt(SSD/(len(data)-3-1))
RSE
RSE/sales_m<jupyter_output><empty_output><jupyter_text>Multicolinealidad  Factor Inflación de la Varianza* VIF = 1 : Las variables no están correlacionadas* VIF < 5 : Las variables tienen una correlación moderada y se pueden quedar en el modelo* VIF >5 : Las variables están altamente correlacionadas y deben desaparecer del modelo.<jupyter_code># Newspaper ~ TV + Radio -> R^2 VIF = 1/(1-R^2)
lm_n = smf.ols(formula="Newspaper~TV+Radio", data = data).fit()
rsquared_n = lm_n.rsquared
VIF = 1/(1-rsquared_n)
VIF
# TV ~ Newspaper + Radio -> R^2 VIF = 1/(1-R^2)
lm_tv = smf.ols(formula="TV~Newspaper+Radio", data=data).fit()
rsquared_tv = lm_tv.rsquared
VIF = 1/(1-rsquared_tv)
VIF
# Radio ~ TV + Newspaper -> R^2 VIF = 1/(1-R^2)
lm_r = smf.ols(formula="Radio~Newspaper+TV", data=data).fit()
rsquared_r = lm_r.rsquared
VIF = 1/(1-rsquared_r)
VIF
lm3.summary()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16379310344827586, 'fraction_numerical': 0.03065134099616858, 'mean_word_length': 6.477326968973747, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '459712', 'license': 'MIT', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1285, 'n_words': 255, 'path': 'notebooks/T4 - 2 - Linear Regression - Regresión lineal con statsmodel.ipynb', 'repo_name': '1214345/python-ml-course', 'size': '3132'}