<jupyter_start><jupyter_text>Reading data using pandas<jupyter_code>import pandas as pd
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)
data.head()
data.tail()
data.shape
import seaborn as sns
%matplotlib inline
sns.pairplot(data, x_vars=['TV', 'Radio', 'Newspaper'], y_vars='Sales', size =7, aspect=0.7, kind='reg')<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>features_cols = ['TV', 'Radio', 'Newspaper']
X = data[features_cols]
X.head()
print type(X)
print X.shape
y = data['Sales']
y = data.Sales
y.head()
print type(y)
print y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
print X_test.shape
print X_train.shape
print y_test.shape
print y_train.shape<jupyter_output>(50, 3)
(150, 3)
(50,)
(150,)<jupyter_text>Linear Regression in scikit-learn<jupyter_code>from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(X_train, y_train)
print linreg.intercept_
print linreg.coef_
# pair the feature names with the coefficients
zip(features_cols, linreg.coef_)
y_pred = linreg.predict(X_test)
print y_pred
# Define true and predicted response value
true = [100, 50, 30, 20]
pred = [90, 50, 50, 30]
print (10 + 0 + 20 + 10)/4
from sklearn import metrics
print metrics.mean_absolute_error(true, pred)<jupyter_output>10
10.0<jupyter_text>- Root mean Squared Error<jupyter_code>print (10**2 + 0 + 20**2 + 10**2)/4
print metrics.mean_squared_error(true, pred)

# Calculate RMSE by hand
import numpy as np
print np.sqrt((10**2 + 0 + 20**2 + 10**2)/4)
print np.sqrt(metrics.mean_squared_error(true, pred))<jupyter_output>12.2474487139
12.2474487139<jupyter_text>- Computing the RMSE for our Sales predictions<jupyter_code>print np.sqrt(metrics.mean_squared_error(y_test, y_pred))<jupyter_output>1.40465142303
{'fraction_non_alphanumeric': 0.12891246684350133, 'fraction_numerical': 0.05570291777188329, 'mean_word_length': 6.991525423728813, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '473999', 'license': 'MIT', 'n_tokens_mistral': 808, 'n_tokens_neox': 716, 'n_words': 165, 'path': 'Data-Analysis/pandas_seaborn_scikit-learn.ipynb', 'repo_name': 'AjayKrP/Project', 'size': '1885'}