This project consists of two parts. In Part 1 of the project, you should have completed the questions in Problem Sets 2, 3, and 4 in the Introduction to Data Science course. This document addresses part 2 of the project. Please use this document as a template and answer the following questions to explain your reasoning and conclusion behind your work in the problem sets. You will attach a document with your answers to these questions as part of your final project submission.
In [2]:
import pandas as pd
import pandasql as pdsql
import datetime as dt
import numpy as np
import scipy as sc
import scipy.stats
import statsmodels.api as sm
from sklearn.linear_model import SGDRegressor
from ggplot import *
%matplotlib inline
In [ ]:
Your reasons might be based on intuition. For example, response for fog might be: “I decided to use fog because I thought that when it is very foggy outside people might decide to use the subway more often.” Your reasons might also be based on data exploration and experimentation, for example: “I used feature X because as soon as I included it in my model, it drastically improved my R2 value.”
Please include two visualizations that show the relationships between two or more variables in the NYC subway data. Remember to add appropriate titles and axes labels to your plots. Also, please add a short description below each figure commenting on the key insights depicted in the figure.
You can combine the two histograms in a single plot or you can use two separate plots. If you decide to use to two separate plots for the two histograms, please ensure that the x-axis limits for both of the plots are identical. It is much easier to compare the two in that case. For the histograms, you should have intervals representing the volume of ridership (value of ENTRIESn_hourly) on the x-axis and the frequency of occurrence on the y-axis. For example, each interval (along the x-axis), the height of the bar for this interval will represent the number of records (rows in our data) that have ENTRIESn_hourly that falls in this interval. Remember to increase the number of bins in the histogram (by having larger number of bars). The default bin width is not sufficient to capture the variability in the two samples.
Ridership by time-of-day Ridership by day-of-week
Please address the following questions in detail. Your answers should be 1-2 paragraphs long.
Please address the following questions in detail. Your answers should be 1-2 paragraphs long.
In [3]:
weather_data = pd.read_csv("turnstile_weather_v2.csv")
weather_data["hour"] = weather_data["hour"].astype('category')
weather_data["rain"] = (weather_data["rain"]+1).astype('category')
weather_data["fog"] = (weather_data["fog"]+1).astype('category')
weather_data.head(3)
Out[3]:
In [24]:
p = ggplot(aes(x = 'rain', y='ENTRIESn_hourly', color = "meantempi"), data=weather_data)
p + geom_point(position = "jitter", alpha = 0.7) + scale_y_continuous(limits = [0,45000]) + \
facet_wrap('hour') + ggtitle("Number of Riders over the Day Dependent on Rain") + theme_bw()
Out[24]:
In [23]:
p = ggplot(aes(x = 'rain', y='ENTRIESn_hourly', color = "meantempi"), data=weather_data)
p + geom_point(position = "jitter", alpha = 0.7) + scale_y_continuous(limits = [0,45000]) + theme_bw() + \
facet_wrap('day_week', nrow = 4) + ggtitle("Number of Riders over the Week Dependent on Rain")
Out[23]:
In [22]:
p = ggplot(aes(x = 'fog', y='ENTRIESn_hourly', color = "meantempi"), data=weather_data)
p + geom_point(position = "jitter", alpha = 0.7) + scale_y_continuous(limits = [0,45000]) + \
facet_wrap('hour') + ggtitle("Number of Riders over the Day Dependent on Fog") + theme_bw()
Out[22]:
In [26]:
p = ggplot(aes(x = 'ENTRIESn_hourly', color = 'rain'), data=weather_data)
p + geom_density(size = 3, alpha = 0.25) + theme_bw() + \
scale_x_continuous(limits = [-1000,5000]) + ggtitle("Number of Riders Dependent on Rain")
Out[26]:
Based on the plot, the sample of entries does not seem normally distributed. Hence, the Mann-Whitney-Wilcoxon RankSum test (no assumptions about any underlying distributions) is conducted to test if the two samples of the number of entries in the NYC subway on rainy and non rainy days come from the same population:
H0: The distribution of number of entries on rainy days $F_{rain}(x)$ is identical with the distribution on non rainy days $F_{no-rain}(x-a)$, hence a = 0
H1: The distributions are not the same, a $\neq$ 0
In [52]:
no_rain = weather_data["ENTRIESn_hourly"][weather_data["rain"]==1].dropna()
with_rain = weather_data["ENTRIESn_hourly"][weather_data["rain"]==2].dropna()
print no_rain.head()
print with_rain.head()
without_rain_mean = np.mean(no_rain)
with_rain_mean = np.mean(with_rain)
print without_rain_mean
print with_rain_mean
U, p = sc.stats.mannwhitneyu(no_rain, with_rain)
z, pval = sc.stats.ranksums(no_rain, with_rain)
print U, p
print z, pval
In [ ]:
In [ ]: