<jupyter_start><jupyter_text>Dividir el dataset en conjunto de entrenamiento y de testing<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("../datasets/ads/Advertising.csv")
a = np.random.randn(len(data))
plt.hist(a)
check = (a<0.8)
training = data[check]
testing = data[~check]
len(training), len(testing)
import statsmodels.formula.api as smf
lm = smf.ols(formula="Sales~TV+Radio", data=training).fit()
lm.summary()<jupyter_output><empty_output><jupyter_text>Sales = 2.9336 + 0.0465 * TV + 0.1807 * Radio  Validación del modelo con el conjunto de testing<jupyter_code>sales_pred = lm.predict(testing)
sales_pred
SSD = sum((testing["Sales"]-sales_pred)**2)
SSD
RSE = np.sqrt(SSD/(len(testing)-2-1))
RSE
sales_mean = np.mean(testing["Sales"])
error = RSE/sales_mean
error
%matplotlib inline
data.plot(kind = "scatter", x = "TV", y ="Sales")
plt.plot(pd.DataFrame(data["TV"]), sales_pred, c="red", linewidth = 2)
from IPython.display import Image
Image(filename="resources/summary-lm.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15205223880597016, 'fraction_numerical': 0.01958955223880597, 'mean_word_length': 6.609929078014185, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '506113', 'license': 'MIT', 'n_tokens_mistral': 426, 'n_tokens_neox': 400, 'n_words': 86, 'path': 'notebooks/T4 - 3 - Linear Regression - Validación del modelo.ipynb', 'repo_name': '1214345/python-ml-course', 'size': '1072'}