In [2]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn import metrics
%matplotlib inline
In [3]:
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/bikeshare.csv'
bikes = pd.read_csv(url)
bikes.head()
Out[3]:
In [4]:
bikes[['count', 'temp']].corr()
Out[4]:
In [5]:
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))
Out[5]:
In [6]:
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))
Out[6]:
In [7]:
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))
Out[7]:
In [8]:
count_avg = bikes['count'].mean()
null_model_count_predict = [count_avg] * len(bikes)
np.sqrt(metrics.mean_squared_error(count, null_model_count_predict))
Out[8]:
In [31]:
Out[31]:
In [20]:
In [ ]: