0
0
ML Pythonml~20 mins

Date and time feature extraction in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DateTime Feature Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Extracting the hour from a datetime column
What is the output of this code that extracts the hour from a datetime column in a pandas DataFrame?
ML Python
import pandas as pd

df = pd.DataFrame({'timestamp': pd.to_datetime(['2024-06-01 08:15:27', '2024-06-01 22:45:00'])})
df['hour'] = df['timestamp'].dt.hour
print(df['hour'].tolist())
A[8, 22]
B[15, 45]
C[2024, 2024]
D[1, 1]
Attempts:
2 left
💡 Hint
The .dt accessor helps extract parts of datetime like hour, minute, or day.
🧠 Conceptual
intermediate
1:00remaining
Why extract date and time features?
Why do we extract features like day of week or month from datetime data in machine learning?
ATo reduce the size of the dataset by removing datetime columns.
BTo convert datetime into text for natural language processing.
CTo help the model learn patterns related to time, like weekends or seasons.
DTo encrypt the datetime data for security.
Attempts:
2 left
💡 Hint
Think about how time affects behavior or events in real life.
Metrics
advanced
1:30remaining
Evaluating model with time-based features
You added 'day_of_week' and 'hour' features extracted from timestamps to your model. Which metric best shows if these features improved the model's ability to predict hourly sales?
AMean Absolute Error (MAE) on a test set split randomly
BAccuracy score on a classification task
CConfusion matrix on training data
DMean Squared Error (MSE) on a test set split by time (e.g., last month)
Attempts:
2 left
💡 Hint
Think about how to fairly test time-related predictions.
🔧 Debug
advanced
1:30remaining
Fixing incorrect extraction of month from datetime
What error does this code raise when trying to extract the month from a datetime column?
ML Python
import pandas as pd

df = pd.DataFrame({'date': ['2024-06-01', '2024-07-15']})
df['month'] = df['date'].dt.month
print(df['month'])
AAttributeError: Can only use .dt accessor with datetimelike values
BKeyError: 'month' not found
CTypeError: unsupported operand type(s) for -: 'str' and 'int'
DNo error, prints [6, 7]
Attempts:
2 left
💡 Hint
Check the data type of the 'date' column before using .dt accessor.
Model Choice
expert
2:00remaining
Choosing model type for datetime feature importance
You have extracted multiple datetime features (hour, day_of_week, month) for a sales forecasting task. Which model type is best to capture complex interactions between these features and other variables?
ALinear Regression without interaction terms
BDecision Tree or Random Forest models
CK-Nearest Neighbors (KNN) without feature scaling
DNaive Bayes classifier
Attempts:
2 left
💡 Hint
Consider models that handle non-linear relationships and feature interactions well.