Certainly! Here's how you might approach each of the General EDA Questions and Machine Learning Model Building tasks in Python, using pandas for data manipulation, matplotlib and seaborn for visualization, and scikit-learn for machine learning.
### General EDA Questions:
1.
Proportion of rides by member vs. casual users:
member_ride_count = df[df['user_type'] == 'Member'].shape[0]
casual_ride_count = df[df['user_type'] == 'Casual'].shape[0]
total_rides = df.shape[0]
member_ride_proportion = member_ride_count / total_rides
casual_ride_proportion = casual_ride_count / total_rides
2.
Change in proportions of free and paying rides:
# Assuming 'ride_type' column indicates if a ride is free or paid
free_rides = df[df['ride_type'] == 'Free']
paid_rides = df[df['ride_type'] == 'Paid']
# Group by day of the week and calculate proportions
free_rides['day_of_week'] = free_rides['started_at'].dt.day_name()
paid_rides['day_of_week'] = paid_rides['started_at'].dt.day_name()
free_ride_proportions = free_rides['day_of_week'].value_counts(normalize=True)
paid_ride_proportions = paid_rides['day_of_week'].value_counts(normalize=True)
3.
Distribution of ride durations between electric bikes and classic bikes:
import seaborn as sns
import matplotlib.pyplot as plt
sns.boxplot(x='bike_type', y='duration', data=df)
plt.show()
### Building Machine Learning Models:
-
Feature Selection and Preprocessing:
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Selecting features and target
X = df[['start_station', 'end_station', 'start_time', 'end_time']] # example features
y = df['user_type'].apply(lambda x: 1 if x == 'Member' else 0) # binary encoding of the target
# Encoding categorical variables and scaling
X_encoded = pd.get_dummies(X, drop_first=True)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_encoded)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
-
Model Training and Evaluation:
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
# Initialize the classifier
rf_clf = RandomForestClassifier(random_state=42)
# Train the model
rf_clf.fit(X_train, y_train)
# Predictions
y_pred = rf_clf.predict(X_test)
# Evaluate the model
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
-
Model Optimization:
from sklearn.model_selection import GridSearchCV
# Define the parameter grid
param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [5, 10, 15],
'min_samples_split': [2, 5, 10]
}
# Initialize the grid search
grid_search = GridSearchCV(estimator=rf_clf, param_grid=param_grid, cv=5, scoring='accuracy')
# Perform grid search
grid_search.fit(X_train, y_train)
# Best parameters
print(grid_search.best_params_)
Remember to install the required libraries using
pip if you haven't already:
pip install pandas scikit-learn matplotlib seaborn
Each snippet of code above is a simplified example and will need to be adapted to the specifics of your dataset, such as the correct column names and the appropriate preprocessing steps for your particular features and target variable.