<jupyter_start><jupyter_text>Multiple Linear Regression  Importing the libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Importing the dataset<jupyter_code>dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
print(X[:11])
print(type(X))<jupyter_output>[[165349.2 136897.8 471784.1 'New York']
 [162597.7 151377.59 443898.53 'California']
 [153441.51 101145.55 407934.54 'Florida']
 [144372.41 118671.85 383199.62 'New York']
 [142107.34 91391.77 366168.42 'Florida']
 [131876.9 99814.71 362861.36 'New York']
 [134615.46 147198.87 127716.82 'California']
 [130298.13 145530.06 323876.68 'Florida']
 [120542.52 148718.95 311613.29 'New York']
 [123334.88 108679.17 304981.62 'California']
 [101913.08 110594.11 229160.95 'Florida']]
<class 'numpy.ndarray'><jupyter_text>Encoding categorical data<jupyter_code>from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder 

# codificamos la columna de la ciudades = [3]
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [3])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
print(X[:11])
print(type(X))<jupyter_output>[[0.0 0.0 1.0 165349.2 136897.8 471784.1]
 [1.0 0.0 0.0 162597.7 151377.59 443898.53]
 [0.0 1.0 0.0 153441.51 101145.55 407934.54]
 [0.0 0.0 1.0 144372.41 118671.85 383199.62]
 [0.0 1.0 0.0 142107.34 91391.77 366168.42]
 [0.0 0.0 1.0 131876.9 99814.71 362861.36]
 [1.0 0.0 0.0 134615.46 147198.87 127716.82]
 [0.0 1.0 0.0 130298.13 145530.06 323876.68]
 [0.0 0.0 1.0 120542.52 148718.95 311613.29]
 [1.0 0.0 0.0 123334.88 108679.17 304981.62]
 [0.0 1.0 0.0 101913.08 110594.11 229160.95]]
<class 'numpy.ndarray'><jupyter_text>Splitting the dataset into the Training set and Test set<jupyter_code>from sklearn.model_selection import train_test_split
# dividimos los datos en X_train, X_test, y_train, y_test con el 20% testing 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
print("X_train: ",X_train[:11])
print("X_test: ",X_test[:11])<jupyter_output>X_train:  [[0.0 1.0 0.0 55493.95 103057.49 214634.81]
 [0.0 0.0 1.0 46014.02 85047.44 205517.64]
 [0.0 1.0 0.0 75328.87 144135.98 134050.07]
 [1.0 0.0 0.0 46426.07 157693.92 210797.67]
 [0.0 1.0 0.0 91749.16 114175.79 294919.57]
 [0.0 1.0 0.0 130298.13 145530.06 323876.68]
 [0.0 1.0 0.0 119943.24 156547.42 256512.92]
 [0.0 0.0 1.0 1000.23 124153.04 1903.93]
 [0.0 0.0 1.0 542.05 51743.15 0.0]
 [0.0 0.0 1.0 65605.48 153032.06 107138.38]
 [0.0 0.0 1.0 114523.61 122616.84 261776.23]]
X_test:  [[0.0 1.0 0.0 66051.52 182645.56 118148.2]
 [1.0 0.0 0.0 100671.96 91790.61 249744.55]
 [0.0 1.0 0.0 101913.08 110594.11 229160.95]
 [0.0 1.0 0.0 27892.92 84710.77 164470.71]
 [0.0 1.0 0.0 153441.51 101145.55 407934.54]
 [0.0 0.0 1.0 72107.6 127864.55 353183.81]
 [0.0 0.0 1.0 20229.59 65947.93 185265.1]
 [0.0 0.0 1.0 61136.38 152701.92 88218.23]
 [0.0 1.0 0.0 73994.56 122782.75 303319.26]
 [0.0 1.0 0.0 142107.34 91391.77 366168.42]]<jupyter_text>Training the Multiple Linear Regression model on the Training set<jupyter_code>from sklearn.linear_model import LinearRegression
# regrecion lineal clasica
regressor = LinearRegression()
regressor.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predicting the Test set results<jupyter_code>y_pred = regressor.predict(X_test)
print(y_pred) # valore de "Y" predictores

print(y_pred.reshape(len(y_pred),1)) # lo dimenciono a vertical
np.set_printoptions(precision=2) # tomo los datos con 2 decimas
# concatenate((vector de gancias predichas), (vector de ganacias reales))
#.reshape(len(y_pred),1) >> redimenciono el vector a horizontal 
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1)) # 0 : vertical y 1 : horizontal<jupyter_output>[[103015.2  103282.38]
 [132582.28 144259.4 ]
 [132447.74 146121.95]
 [ 71976.1   77798.83]
 [178537.48 191050.39]
 [116161.24 105008.31]
 [ 67851.69  81229.06]
 [ 98791.73  97483.56]
 [113969.44 110352.25]
 [167921.07 166187.94]]<jupyter_text>La primera columna son las Prediciones y la segunda los valores de Y con los datos de texteo  Ejemplo de predicionHacer una sola predicción (por ejemplo, el beneficio de una startup con I + D Gasto = 160000, Gasto de administración = 130000, Gasto de marketing = 300000 y Estado = 'California')<jupyter_code>pred_dato = regressor.predict([[1, 0, 0, 160000, 130000, 300000]])
print(pred_dato)<jupyter_output>[181566.92]<jupyter_text>Por lo tanto, nuestro modelo predice que la ganancia de una startup californiana que gastó 160000 en I + D, 130000 en Administración y 300000 en Marketing es $ 181566,92.**Important note** 1: Observe que los valores de las características se ingresaron en un par doble de corchetes. Eso es porque el método "predecir" siempre espera una matriz 2D como el formato de sus entradas. Y poner nuestros valores en un par doble de corchetes hace que la entrada sea exactamente una matriz 2D. Simplemente pon:**1,0,0,160000,130000,300000→scalars****[1,0,0,160000,130000,300000]→1D array** **[[1,0,0,160000,130000,300000]]→2D array****Nota importante 2**: Observe también que el estado "California" no se ingresó como una cadena en la última columna sino como "1, 0, 0" en las primeras tres columnas. Eso es porque, por supuesto, el método de predicción espera los valores codificados en caliente del estado y, como vemos en la segunda fila de la matriz de características X, "California" se codificó como "1, 0, 0". Y tenga cuidado de incluir estos valores en las primeras tres columnas, no en las últimas tres, porque las variables ficticias siempre se crean en las primeras columnas.  Obtener la ecuación de regresión lineal final con los valores de los coeficientes<jupyter_code>print(regressor.coef_)
print(regressor.intercept_)<jupyter_output>[ 8.66e+01 -8.73e+02  7.86e+02  7.73e-01  3.29e-02  3.66e-02]
42467.52924853204<jupyter_text>Por lo tanto, la ecuación de nuestro modelo de regresión lineal múltiple es:  Beneficio = 86.6 × Estado ficticio 1−873 × Estado ficticio 2 + 786 × Estado ficticio 3−0.773 × Gasto en I + D + 0.0329 × Administración + 0.0366 × Gasto en marketing + 42467.53  **Nota importante**: Para obtener estos coeficientes, llamamos a los atributos "coef_" e "intercept_" de nuestro objeto regresor. Los atributos en Python son diferentes a los métodos y generalmente devuelven un valor simple o una matriz de valores.  Eliminación hacia atrás Regresión lineal múltiple<jupyter_code># Regresión lineal múltiple

# Importando las bibliotecas

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
print(X[:10])
# Encoding categorical data
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [3])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
print(X[:10])
# Avoiding the Dummy Variable Trap (Evitar la trampa variable ficticia)
X = X[:, 1:]  # elimina la Columna 0
print(X[:10])
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Training the Multiple Linear Regression model on the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Predicting the Test set results
y_pred = regressor.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))<jupyter_output>[[103015.2  103282.38]
 [132582.28 144259.4 ]
 [132447.74 146121.95]
 [ 71976.1   77798.83]
 [178537.48 191050.39]
 [116161.24 105008.31]
 [ 67851.69  81229.06]
 [ 98791.73  97483.56]
 [113969.44 110352.25]
 [167921.07 166187.94]]<jupyter_text>Construyendo el modelo óptimo usando Backward Elimination<jupyter_code># Building the optimal model using Backward Elimination
import statsmodels.api as sm
X = np.append(arr = np.ones((50, 1)).astype(int), values = X, axis = 1)
X_opt = X[:, [0, 1, 2, 3, 4, 5]]
X_opt = X_opt.astype(np.float64)
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary() 
X_opt = X[:, [0, 1, 3, 4, 5]]
X_opt = X_opt.astype(np.float64)
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3, 4, 5]]
X_opt = X_opt.astype(np.float64)
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3, 5]]
X_opt = X_opt.astype(np.float64)
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
X_opt = X[:, [0, 3]]
X_opt = X_opt.astype(np.float64)

regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()<jupyter_output>/usr/local/lib/python3.6/dist-packages/statsmodels/regression/linear_model.py:1657: RuntimeWarning: invalid value encountered in double_scalars
  return self.ess/self.df_model
{'fraction_non_alphanumeric': 0.12692014380651487, 'fraction_numerical': 0.19522823837019282, 'mean_word_length': 4.884615384615385, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '557920', 'license': 'MIT', 'n_tokens_mistral': 4871, 'n_tokens_neox': 3725, 'n_words': 992, 'path': 'Part 2 - Regression/Section 5 - Multiple Linear Regression/Python/multiple_linear_regression.ipynb', 'repo_name': 'INFINITY-RUBER/Machine_Learning_A-Z_Hands-On-Python-R-In-Data-Science', 'size': '9179'}