<jupyter_start><jupyter_text>A Linear Regression Example from the book "Principles of Data Science"<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn import metrics
%matplotlib inline
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/bikeshare.csv'
bikes = pd.read_csv(url)
bikes.head()
bikes[['count', 'temp']].corr()
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(bikes[['temp']], bikes['count'])
print(linreg.intercept_)
print(linreg.coef_)
count_predict = linreg.predict(bikes[['temp']])
np.sqrt(metrics.mean_squared_error(bikes['count'], count_predict))
features = ['temp', 'season', 'weather', 'humidity']
linreg = LinearRegression()
linreg.fit(bikes[features], bikes['count'])
linreg.coef_
sns.pairplot(bikes, x_vars=features, y_vars='count', kind='reg')
count_predict = linreg.predict(bikes[features])
np.sqrt(metrics.mean_squared_error(bikes['count'], count_predict))
from sklearn.model_selection import train_test_split
features = bikes[['temp']]
count = bikes['count']
features_train, features_test, count_train, count_test = train_test_split(features, count)
print("total feature size: {}, train feature size: {}, test feature size: {}".format(len(features), len(features_train), len(features_test)))
linreg = LinearRegression()
linreg.fit(features_train, count_train)
count_predict = linreg.predict(features_test)
np.sqrt(metrics.mean_squared_error(count_test, count_predict))
count_avg = bikes['count'].mean()
null_model_count_predict = [count_avg] * len(bikes)
np.sqrt(metrics.mean_squared_error(count, null_model_count_predict))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1378299120234604, 'fraction_numerical': 0.0005865102639296188, 'mean_word_length': 9.33939393939394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '586861', 'license': 'MIT', 'n_tokens_mistral': 583, 'n_tokens_neox': 561, 'n_words': 112, 'path': 'bikes-linear-regression.ipynb', 'repo_name': 'guozheng/data-science-ml-basics', 'size': '1705'}