Spaces:
Build error
Build error
Upload time_series_splitter.py
Browse files- time_series_splitter.py +17 -0
time_series_splitter.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
def split_time_series_data(date_column,X, y, test_size=0.2):
|
| 4 |
+
"""Split time series data into training and testing sets."""
|
| 5 |
+
|
| 6 |
+
# Sort the data by date
|
| 7 |
+
X_sorted = X.sort_values(by=date_column)
|
| 8 |
+
y_sorted = y.loc[X_sorted.index]
|
| 9 |
+
|
| 10 |
+
# Calculate the split index
|
| 11 |
+
split_index = int(len(X_sorted) * (1 - test_size))
|
| 12 |
+
|
| 13 |
+
# Split the data
|
| 14 |
+
X_train, X_test = X_sorted.iloc[:split_index], X_sorted.iloc[split_index:]
|
| 15 |
+
y_train, y_test = y_sorted.iloc[:split_index], y_sorted.iloc[split_index:]
|
| 16 |
+
|
| 17 |
+
return X_train, X_test, y_train, y_test
|