<jupyter_start><jupyter_text>El modelo puede ser escrito como:     Total_Spend = -79.41713030137362 + Monthly Income * 0.14753898049205738 +                Transaction Time * 0.15494612549589545 + Gender_Female * (-131.02501325554567) +                Gender_Male * 131.0250132555456 + City_Tier 1 * 76.76432601049527 +                City_Tier 2 * 55.138974309232474 + City_Tier 3 * (-131.9033003197278) +                 Record * 772.2334457445648<jupyter_code># Calcular la Desviacion Tipica de los Residuos
df_new["Prediction"] = -79.41713030137362 + df_new['Monthly Income'] * 0.14753898049205738 + df_new['Transaction Time'] * 0.15494612549589545 + df_new['Gender_Female'] * (-131.02501325554567) + df_new['Gender_Male'] * 131.0250132555456 + df_new['City_Tier 1'] * 76.76432601049527 + df_new['City_Tier 2'] * 55.138974309232474 + df_new['City_Tier 3'] * (-131.9033003197278) + df_new['Record'] * 772.2334457445648
df_new.head()
# Calcula la raiz de la suma de los cuadrados de los errores entre el numero de registros menos las variables predictoras menos 1
SSD = np.sum((df_new['Prediction'] - df_new["Total Spend"])**2)
SSD
RSE = np.sqrt(SSD / (len(df_new) - len(feature_cols) - 1))
RSE
# Promedio de ventas
sales_mean = np.mean(df_new["Total Spend"])
sales_mean
error = RSE / sales_mean
error<jupyter_output><empty_output><jupyter_text>Eliminar variable dummy El modelo orginal:     Total_Spend = -79.41713030137362 + Monthly Income * 0.14753898049205738 +                Transaction Time * 0.15494612549589545 + Gender_Female * (-131.02501325554567) +                Gender_Male * 131.0250132555456 + City_Tier 1 * 76.76432601049527 +                City_Tier 2 * 55.138974309232474 + City_Tier 3 * (-131.9033003197278) +                 Record * 772.2334457445648  Transformación de variables para conseguir una relación no lineal<jupyter_code>import pandas as pd
data_auto = pd.read_csv("/Users/nuelcodes/Data-Science-Python/datasets/auto/auto-mpg.csv")
data_auto.head()
# Numero de filas y columnas
data_auto.shape
import matplotlib.pyplot as plt
%matplotlib inline
data_auto["mpg"] = data_auto["mpg"].dropna() # Eliminando los N/A de la columna mpg
data_auto["horsepower"] = data_auto["horsepower"].dropna() # Eliminando los N/A de la columna horsepower
plt.plot(data_auto["horsepower"], data_auto["mpg"], "r.")
plt.xlabel("Caballos de potencia")
plt.ylabel("Consumo (millas por galeón)")
plt.title("CV vs MPG")<jupyter_output><empty_output><jupyter_text>Modelo de regresión lineal* mpg = a + b * horsepower<jupyter_code># Rellenamos los N/A con el promedio de cada columna
X = data_auto["horsepower"].fillna(data_auto["horsepower"].mean())
Y = data_auto["mpg"].fillna(data_auto["mpg"].mean())
X_data = X[:, np.newaxis]
lm = LinearRegression()
lm.fit(X_data,Y)
%matplotlib inline
plt.plot(X, Y, "r.")
plt.plot(X, lm.predict(X_data), color="blue")
lm.score(X_data, Y)
# Suma de los cuadrados de las desviaciones
SSD = np.sum((Y - lm.predict(X_data))**2)
RSE = np.sqrt(SSD / (len(X_data) - 1))
y_mean = np.mean(Y)
error = RSE / y_mean
SSD, RSE, y_mean, error*100<jupyter_output><empty_output><jupyter_text>Modelo de regresión cuadrático* mpg = a + b * horsepower^2<jupyter_code>X_data = X**2
X_data = X_data[:, np.newaxis]
lm = LinearRegression()
lm.fit(X_data, Y)
lm.score(X_data, Y)
SSD = np.sum((Y - lm.predict(X_data))**2)
RSE = np.sqrt(SSD / (len(X_data) - 1))
y_mean = np.mean(Y)
error = RSE / y_mean
SSD, RSE, y_mean, error*100<jupyter_output><empty_output><jupyter_text>Modelo de regresión lineal y cuadrático* mpg = a + b * horsepower + c * horsepower^2<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
poly = PolynomialFeatures(degree=2)
X_data = poly.fit_transform(X[:, np.newaxis])
lm = linear_model.LinearRegression()
lm.fit(X_data, Y)
# Verificando si l modelo mejoro o no
lm.score(X_data, Y)
def regresion_validation(X_data, Y, Y_pred):
    SSD = np.sum((Y - Y_pred)**2)
    RSE = np.sqrt(SSD / (len(X_data) - 1))
    y_mean = np.mean(Y)
    error = RSE / y_mean
    print("SSD: " + str(SSD) + ", RSE: " + str(RSE) + ", Y_mean: " + str(y_mean) + ", error: " + str(error*100) + "%")
# Ver si aunmentando el grado (con un bucle), nuestro modelo mejora
for d in range (2,12):
    poly = PolynomialFeatures(degree=d)
    X_data = poly.fit_transform(X[:, np.newaxis])
    lm = linear_model.LinearRegression()
    lm.fit(X_data, Y)
    print("Regresión de grado " + str(d))
    print("R2: " + str(lm.score(X_data, Y)))
    print(lm.intercept_)
    print(lm.coef_)
    regresion_validation(X_data, Y, lm.predict(X_data))<jupyter_output>Regresión de grado 2
R2: 0.6439066584257469
55.026192447080355
[ 0.         -0.43404318  0.00112615]
SSD: 8636.180643524502, RSE: 4.61778115803654, Y_mean: 23.51457286432162, error: 19.63795466190689%
Regresión de grado 3
R2: 0.6444678885560744
58.448774111918084
[ 0.00000000e+00 -5.27113167e-01  1.89736722e-03 -1.95723195e-06]
SSD: 8622.56936742855, RSE: 4.614140736300908, Y_mean: 23.51457286432162, error: 19.622473106036676%
Regresión de grado 4
R2: 0.6467674189704502
40.096643848138505
[ 0.00000000e+00  1.62563541e-01 -7.10892744e-03  4.65580255e-05
 -9.15840093e-08]
SSD: 8566.799832491353, RSE: 4.5991947315797574, Y_mean: 23.51457286432162, error: 19.55891250126878%
Regresión de grado 5
R2: 0.6547512489366876
-40.693812896755176
[ 0.00000000e+00  4.00021432e+00 -7.54801920e-02  6.19621369e-04
 -2.36220932e-06  3.41982935e-09]
SSD: 8373.171393636556, RSE: 4.546921735442387, Y_mean: 23.51457286432162, error: 19.336612073193884%
Regresión de grado 6
R2: 0.6572844624926477
-157.0731824[...]<jupyter_text>El problema de los outliers<jupyter_code>plt.plot(data_auto["displacement"], data_auto["mpg"], "ro")
# Un modelo lineal para ajustarse a la recta
X = data_auto["displacement"].fillna(data_auto["displacement"].mean())
X = X[:, np.newaxis]
Y = data_auto["mpg"].fillna(data_auto["mpg"].mean())
lm = LinearRegression()
lm.fit(X, Y)
lm.score(X, Y)
# Dibujamos la recta
%matplotlib inline
plt.plot(X, Y, "ro")
plt.plot(X, lm.predict(X), color="blue")
# Detectando los outliers
data_auto[(data_auto["displacement"] > 250) & (data_auto["mpg"] >35)]
# Detectando los outliers
data_auto[(data_auto["displacement"] > 300) & (data_auto["mpg"] >20)]
# Eliminando los outliers
data_auto_clean = data_auto.drop([395,258,305,372])
X = data_auto_clean["displacement"].fillna(data_auto_clean["displacement"].mean())
X = X[:, np.newaxis]
Y = data_auto_clean["mpg"].fillna(data_auto_clean["mpg"].mean())
lm = LinearRegression()
lm.fit(X, Y)
lm.score(X, Y)
%matplotlib inline
plt.plot(X, Y, "r.")
plt.plot(X, lm.predict(X), color="blue")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14296418402517608, 'fraction_numerical': 0.16799040911134422, 'mean_word_length': 4.974932855863921, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '277450', 'license': 'MIT', 'n_tokens_mistral': 3464, 'n_tokens_neox': 2742, 'n_words': 542, 'path': 'scratch/T4 - 5 - Linear Regression - Problemas con la regresion lineal.ipynb', 'repo_name': 'nuelcodes/python-ml-course', 'size': '6673'}