<jupyter_start><jupyter_text>Guia- ¿Cómo uso la ** biblioteca de pandas ** para leer datos en Python?- ¿Cómo uso la biblioteca ** seaborn ** para visualizar datos?- ¿Qué es ** regresión lineal ** y cómo funciona?- ¿Cómo ** entreno e interpreto ** un modelo de regresión lineal en scikit-learn?- ¿Cuáles son algunas ** métricas de evaluación ** para problemas de regresión?- ¿Cómo elijo ** qué características incluir ** en mi modelo? Tipos de aprendizaje supervisado- ** Clasificación: ** Predecir una respuesta categórica- ** Regresión: ** Predecir una respuesta continua  Leer datos usando pandas** Pandas: ** biblioteca Python popular para exploración, manipulación y análisis de datos<jupyter_code>import pandas as pd
# read CSV file from the 'data' subdirectory using a relative path
data = pd.read_csv('Advertising.csv', index_col=0)

# display the first 5 rows
data.head()<jupyter_output><empty_output><jupyter_text>Tipos de objetos primarios:- ** DataFrame: ** filas y columnas (como una hoja de cálculo)- ** Serie: ** una sola columna<jupyter_code># display the last 5 rows
data.tail()
# check the shape of the DataFrame (rows, columns)
data.shape<jupyter_output><empty_output><jupyter_text>¿Cuáles son las características?- ** TV: ** dólares de publicidad gastados en TV para un solo producto en un mercado determinado (en miles de dólares)- ** Radio: ** dólares de publicidad gastados en Radio- ** Periódico: ** dólares publicitarios gastados en el periódicoCual es la respuesta?- ** Ventas: ** ventas de un solo producto en un mercado determinado (en miles de artículos)¿Qué más sabemos?- Debido a que la variable de respuesta es continua, este es un problema de ** regresión **.- Hay 200 ** observaciones ** (representadas por las filas), y cada observación es un mercado único.  Visualizing data using seaborn**Seaborn:** Python library for statistical data visualization built on top of Matplotlib- Anaconda users: run **`conda install seaborn`** from the command line- Other users: [installation instructions](http://seaborn.pydata.org/installing.html)<jupyter_code># conventional way to import seaborn
import seaborn as sns

# allow plots to appear within the notebook
%matplotlib inline
# visualize the relationship between the features and the response using scatterplots
sns.pairplot(data, x_vars=['TV','Radio','Newspaper'], y_vars='Sales', size=7, aspect=0.7, kind='reg')<jupyter_output>/home/bb8/env/lib/python3.6/site-packages/seaborn/axisgrid.py:2065: UserWarning: The `size` parameter has been renamed to `height`; pleaes update your code.
  warnings.warn(msg, UserWarning)<jupyter_text>Linear regression**Pros:** fast, no tuning required, highly interpretable, well-understood**Cons:** unlikely to produce the best predictive accuracy (presumes a linear relationship between the features and response)  Form of linear regression$y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_nx_n$- $y$ is the response- $\beta_0$ is the intercept- $\beta_1$ is the coefficient for $x_1$ (the first feature)- $\beta_n$ is the coefficient for $x_n$ (the nth feature)In this case:$y = \beta_0 + \beta_1 \times TV + \beta_2 \times Radio + \beta_3 \times Newspaper$The $\beta$ values are called the **model coefficients**. These values are "learned" during the model fitting step using the "least squares" criterion. Then, the fitted model can be used to make predictions!  Preparing X and y using pandas- scikit-learn expects X (feature matrix) and y (response vector) to be NumPy arrays.- However, pandas is built on top of NumPy.- Thus, X can be a pandas DataFrame and y can be a pandas Series!<jupyter_code># create a Python list of feature names
feature_cols = ['TV', 'Radio', 'Newspaper']

# use the list to select a subset of the original DataFrame
X = data[feature_cols]

# equivalent command to do this in one line
X = data[['TV', 'Radio', 'Newspaper']]

# print the first 5 rows
X.head()
# check the type and shape of X
print(type(X))
print(X.shape)
# select a Series from the DataFrame
y = data['Sales']

# equivalent command that works if there are no spaces in the column name
y = data.Sales

# print the first 5 values
y.head()
# check the type and shape of y
print(type(y))
print(y.shape)<jupyter_output><class 'pandas.core.series.Series'>
(200,)<jupyter_text>Splitting X and y into training and testing sets<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
# default split is 75% for training and 25% for testing
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)<jupyter_output>(150, 3)
(150,)
(50, 3)
(50,)<jupyter_text>Linear regression in scikit-learn<jupyter_code># import model
from sklearn.linear_model import LinearRegression

# instantiate
linreg = LinearRegression()

# fit the model to the training data (learn the coefficients)
linreg.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Interpreting model coefficients<jupyter_code># print the intercept and coefficients
print(linreg.intercept_)
print(linreg.coef_)
# pair the feature names with the coefficients
list(zip(feature_cols, linreg.coef_))<jupyter_output><empty_output><jupyter_text>$$y = 2.88 + 0.0466 \times TV + 0.179 \times Radio + 0.00345 \times Newspaper$$How do we interpret the **TV coefficient** (0.0466)?- For a given amount of Radio and Newspaper ad spending, **a "unit" increase in TV ad spending** is associated with a **0.0466 "unit" increase in Sales**.- Or more clearly: For a given amount of Radio and Newspaper ad spending, **an additional $1,000 spent on TV ads** is associated with an **increase in sales of 46.6 items**.Important notes:- This is a statement of **association**, not **causation**.- If an increase in TV ad spending was associated with a **decrease** in sales, $\beta_1$ would be **negative**.  Making predictions<jupyter_code># make predictions on the testing set
y_pred = linreg.predict(X_test)<jupyter_output><empty_output><jupyter_text>We need an **evaluation metric** in order to compare our predictions with the actual values!  Model evaluation metrics for regressionEvaluation metrics for classification problems, such as **accuracy**, are not useful for regression problems. Instead, we need evaluation metrics designed for comparing continuous values.Let's create some example numeric predictions, and calculate **three common evaluation metrics** for regression problems:<jupyter_code># define true and predicted response values
true = [100, 50, 30, 20]
pred = [90, 50, 50, 30]<jupyter_output><empty_output><jupyter_text>**Mean Absolute Error** (MAE) is the mean of the absolute value of the errors:$$\frac 1n\sum_{i=1}^n|y_i-\hat{y}_i|$$<jupyter_code># calculate MAE by hand
print((10 + 0 + 20 + 10)/4.)

# calculate MAE using scikit-learn
from sklearn import metrics
print(metrics.mean_absolute_error(true, pred))<jupyter_output>10.0
10.0<jupyter_text>**Mean Squared Error** (MSE) is the mean of the squared errors:$$\frac 1n\sum_{i=1}^n(y_i-\hat{y}_i)^2$$<jupyter_code># calculate MSE by hand
print((10**2 + 0**2 + 20**2 + 10**2)/4.)

# calculate MSE using scikit-learn
print(metrics.mean_squared_error(true, pred))<jupyter_output>150.0
150.0<jupyter_text>**Root Mean Squared Error** (RMSE) is the square root of the mean of the squared errors:$$\sqrt{\frac 1n\sum_{i=1}^n(y_i-\hat{y}_i)^2}$$<jupyter_code># calculate RMSE by hand
import numpy as np
print(np.sqrt((10**2 + 0**2 + 20**2 + 10**2)/4.))

# calculate RMSE using scikit-learn
print(np.sqrt(metrics.mean_squared_error(true, pred)))<jupyter_output>12.24744871391589
12.24744871391589<jupyter_text>Comparing these metrics:- **MAE** is the easiest to understand, because it's the average error.- **MSE** is more popular than MAE, because MSE "punishes" larger errors.- **RMSE** is even more popular than MSE, because RMSE is interpretable in the "y" units.  Computing the RMSE for our Sales predictions<jupyter_code>print(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))<jupyter_output>1.4046514230328953<jupyter_text>Feature selectionDoes **Newspaper** "belong" in our model? In other words, does it improve the quality of our predictions?Let's **remove it** from the model and check the RMSE!<jupyter_code># create a Python list of feature names
feature_cols = ['TV', 'Radio']

# use the list to select a subset of the original DataFrame
X = data[feature_cols]

# select a Series from the DataFrame
y = data.Sales

# split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# fit the model to the training data (learn the coefficients)
linreg.fit(X_train, y_train)

# make predictions on the testing set
y_pred = linreg.predict(X_test)

# compute the RMSE of our predictions
print(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))<jupyter_output>1.3879034699382888
{'fraction_non_alphanumeric': 0.10588633288227334, 'fraction_numerical': 0.02525935949481281, 'mean_word_length': 5.683496608892238, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '404126', 'license': 'MIT', 'n_tokens_mistral': 3021, 'n_tokens_neox': 2807, 'n_words': 1035, 'path': '.ipynb_checkpoints/regresionlinealMultivariada-checkpoint.ipynb', 'repo_name': 'deltareflo/cursoSociedadCientifica', 'size': '8868'}