"""
# Ford GoBike System Data Exploration 1

## Preliminary Wrangling

This document explores a dataset containing bike trips of 2/2019 for approximately 174,000 record. with 11 feature 
"""
# update required modules # to be used once 
#pip install numpy --upgrade
#pip install pandas --upgrade
#pip install matplotlib --upgrade
#pip install seaborn --upgrade

from datetime import datetime
datetime_1 = datetime.now()
print(datetime_1)

# import all packages and set plots to be embedded inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb

%matplotlib inline

import warnings
warnings.filterwarnings("ignore")
"""
## Data extraction
"""
# load in the dataset into a pandas dataframe, print statistics
#fordgobike = pd.read_csv('201902-fordgobike-tripdata.csv')
#df = fordgobike.copy()
df = pd.read_csv("../input/ford-gobike-2019feb-tripdata/201902-fordgobike-tripdata.csv")
# high-level overview of data shape and composition
print(df.shape)
print(df.info())
df.sample(5)
df.start_station_name.nunique()
df.end_station_name.nunique()
df['member_gender'].unique()
df['bike_share_for_all_trip'].unique()
df['user_type'].unique()
"""
### What is the structure of your dataset?

There are 183412 trip in the dataset with 16 features (duration_sec, start_time, end_time, start_station_id, start_station_name, start_station_latitude, start_station_longitude, end_station_id, end_station_name, end_station_latitude, end_station_longitude, bike_id, user_type, member_birth_year, member_gender and bike_share_for_all_trip). 

Most variables are numeric in nature, but the variables start_station_name, end_station_name, user_type ,member_gender and bike_share_for_all_trip are ordered factor variables with the following levels.


user_type: 'Customer', 'Subscriber' <br>
bike_share_for_all_trip: 'No', 'Yes' <br>
member_gender: 'Male', 'Other', 'Female'

### What is/are the main feature(s) of interest in your dataset?

I'm most interested in figuring out what features are best for analysing the the most used trip and the customer behaviour in the dataset.

### What features in the dataset do you think will help support your investigation into your feature(s) of interest?

I expect that start_station_name and end_station_name will have the strongest effect on most used trip: most repeated stations , the higher the used. I also think that the analysis of type and gender will give clear picture of the bike using.

"""
"""
## Cleaning Process
"""
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])
# as we found many null data we need to check it 
df.notnull().sum()
df = df.dropna()
print(df.shape)
df.notnull().sum()
# drop the columns that not needed for the analysis
df.drop(['start_station_latitude', 'start_station_longitude', 'end_station_latitude', 'end_station_longitude','start_station_id','end_station_id'], inplace = True, axis=1)
df.sample(5)
df.describe()
sb.pairplot(df)
df.info()
"""
## Univairate Exploration
**Analysied features**
* Gender
* Users type
* Age
* Duration
"""
# Explore members gender 
sorted_gender = df['member_gender'].value_counts()

plt.figure(figsize = [7, 7], dpi= 80, facecolor='w', edgecolor='k')
colors = sb.color_palette()
plt.title("Explore members gender ");
explode = (0, 0.1, 0.1)
plt.pie(
    sorted_gender,
    labels=sorted_gender.index,
    startangle = 0,
    colors = colors,
    autopct = '%1.1f%%',
    explode=explode,);

plt.show()
"""
Males are around 75% of the users
"""
# Explore members user type
sorted_type = df['user_type'].value_counts()

plt.figure(figsize = [7, 7], dpi= 80, facecolor='w', edgecolor='k')
colors = sb.color_palette()
plt.title("Explore members user type");
explode = (0.1, 0)
plt.pie(
    sorted_type,
    labels = sorted_type.index,
    startangle = 0,
    colors = colors,
    autopct = '%1.1f%%',
    explode=explode,);

plt.show()
"""
Around 90% of users are Subscribers
"""
df['age']=0
df.age = 2001 - df.member_birth_year
df['age'] = df.apply( lambda row: 2019 - row.member_birth_year , axis=1)
df.describe()
plt.figure(figsize=(15, 7));

plt.subplot(2, 1, 1);
plt.title("Explore members age");
sb.histplot(data = df, x = 'age', kde=True);
age_mean = df.age.mean();
#plt.axvline(age_mean,label= 'Mean age '+str(round(age_mean)) , linestyle = '--', color='r');
plt.xlabel("age");
#plt.legend();

plt.subplot(2, 1, 2);
sb.boxplot(df['age']);
plt.xlabel("age");

plt.show()
"""
1. mean age of the customers is 35 and the min is 18
2. there are users has age of 141 , and there are some has age greter than 100 , it is strange point
"""
# duration exploration
# change duration sec to min for easy calculation
df['duration_min'] = df['duration_sec']/60

plt.figure(figsize=(15, 7))

plt.subplot(2, 1, 1)
plt.title("Duration exploration");
sb.boxplot(df['duration_min'])

plt.subplot(2, 1, 2)
bins = np.arange(df['duration_min'].min(), df['duration_min'].max()+ 30, 10)
sb.histplot(data=df, x="duration_min", label="Duration min", kde=True)
plt.xlabel ('Duration exploration zoomed to 60 Min')
duration_mean=df.duration_min.mean()
plt.axvline(duration_mean,label= 'Mean duration '+str(round(duration_mean)) , linestyle = '--', color='r')
plt.legend() 
plt.xlim((0,60));

plt.show()
"""
1. the average time of using the bikes is arround 10mins
2. few users reserve the bikes for more than 20 mins (and this is strange, may be due to users keeping their bike rented during work or forget to log off after finishing rides)
"""
"""
## Bivairate Exploration
**Analysied Features**
* Gender vs Age
* Duration vs Age
* User type and Gender
"""
plt.figure(figsize=(15, 7))
plt.subplot(2, 1, 1)
plt.title("Gender vs Age");
sb.set(style="darkgrid")
sb.violinplot(x=df["age"], y=df["member_gender"])
plt.subplot(2, 1, 2)
sb.kdeplot(data=df, x='age', hue = 'member_gender', shade=True)
plt.axvline(age_mean,label= 'Mean age '+str(round(age_mean)) , linestyle = '--', color='r')
plt.legend() 
plt.show()
"""
1. Males are using bikes than females
2. females age is more than males
3. few customers dont prefer to mention there gender , and they can be neglected
"""
plt.figure(figsize=(15, 7))
plt.title("Age vs Duration");
plt.scatter(data = df, x="age", y="duration_min", alpha = 1/6)
plt.ylabel('Duration min')
plt.xlabel('Age')
plt.axvline(age_mean,label= 'Mean age '+str(round(age_mean)) , linestyle = '--', color='r')
plt.legend() 
plt.show()
"""
1. members ages are between 20 - 40 years tend to do the longest rides durations.
2. above age 60 riders use the bikes for short duration
"""
# Relation between member user types and genders 
plt.figure(figsize=(15, 7))
plt.title("User Type vs Gender");
sb.countplot(data = df, x = 'member_gender', hue = 'user_type');
plt.show()
"""
1. both Gender prefer user type Subscriber
2. Again Males appear as a domenant
"""
"""
## Multivariate exploration
**Analysied Features**
* Sharing vs User type vs Gender
* Start time vs End time vs Gender
* Days of the month vs Start time vs Gender
"""
plt.figure(figsize=(15, 7))
#plt.title("Sharing vs User type vs Gender");
sb.catplot(x="user_type", hue="member_gender", col="bike_share_for_all_trip",
                data=df, kind="count",);
plt.show()
"""
1. most of useres are type of Subscriber
2. few of Subscribers share there trips
3. males are the most users 
"""
fig = plt.figure(figsize = [15,7])
ax = plt.subplot(2, 2, 1)

plt.title("Male Trips by Hour of Day");
df.groupby([df[df['member_gender']=='Male']['start_time'].dt.hour]).size().plot(c='b')

plt.axvline(8, linestyle = '--', color='r')
plt.xticks([_ for _ in range(24)])
plt.axvline(17,linestyle = '--', color='r');

ax = plt.subplot(2, 2, 2)
plt.title("Female Trips by Hour of Day");
df.groupby([df[df['member_gender']=='Female']['start_time'].dt.hour]).size().plot(c='DeepPink')
plt.axvline(8, linestyle = '--', color='r')
plt.xticks([_ for _ in range(24)])
plt.axvline(17,linestyle = '--', color='r');

ax = plt.subplot(2, 2, 3)
df.groupby([df[df['member_gender']=='Male']['end_time'].dt.hour]).size().plot(c='b')
plt.axvline(8, linestyle = '--', color='r')
plt.xticks([_ for _ in range(24)])
plt.axvline(17,linestyle = '--', color='r');

ax = plt.subplot(2, 2, 4)
df.groupby([df[df['member_gender']=='Female']['end_time'].dt.hour]).size().plot(c='DeepPink')
plt.axvline(8, linestyle = '--', color='r')
plt.xticks([_ for _ in range(24)])
plt.axvline(17,linestyle = '--', color='r');
plt.show()

"""
 1. most of the trips start at 8AM and at 5PM
 2. Males prefer to use the bikes in 5pm more than 8am
 3. Females prefer to use bikes in the morning than afternoon
"""
fig = plt.figure(figsize = [15,7])
ax = plt.subplot(2, 1, 1)

plt.title("Trips by days of month");
df.groupby([df[df['member_gender']=='Male']['start_time'].dt.day]).size().plot(c='b', label='Male')

#ax = plt.subplot(2, 1, 2)
#plt.title("Female Trips by days of month");
df.groupby([df[df['member_gender']=='Female']['start_time'].dt.day]).size().plot(c='DeepPink', label='Female')
#plt.xlabel('X')
plt.ylabel('Trips')
plt.legend(loc='best')  # legend text comes from the plot's label parameter.
plt.show()
"""
1. the behaviour of both Males and Females customers are the same all over the month
2. we can recognize that some days didnt prefer to use bikes , may be the weather was not good
"""
"""
## Talk about some of the relationships you observed in this part of the investigation. Were there features that strengthened each other in terms of looking at your feature(s) of interest?
In the exploration, I found that  mean age of the customers is 35 and the minimum is 18 ,Males are using bikes more than females, also most of useres are type of Subscriber, and few of Subscribers share there trips, plus the average time of using 
the bikes is arround 10 mins. Most of the trips start at 8AM and at 5PM, also the behaviour of both Males and Females customers are the same all over the month. we can recognize that some days didnt prefer to use bikes , may be the weather was not good.



## Were there any interesting or surprising interactions between features?
Outside of the main variables of interest,there are users has age of 141 , and there are some has age greter than 100 , it is strange point females age is more than males and few users reserve the bikes for more than 20 mins (and this is strange) Males prefer to use the bikes in 5pm more than 8am while Females prefer to use bikes in the morning than afternoon.
"""
datetime_2 = datetime.now() - datetime_1
print(datetime_2)