-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
33 lines (26 loc) · 966 Bytes
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.utils import to_categorical
from datasets import load_dataset
def load_and_preprocess_data():
# Load dataset
dataset = load_dataset("mstz/covertype", "covertype")["train"]
data = dataset.to_pandas()
# Separate features and target
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# One-hot encode the target variable
y = to_categorical(y)
# Normalize the features
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Expand dimensions for GRU input
X_train_seq = np.expand_dims(X_train, axis=-1)
X_test_seq = np.expand_dims(X_test, axis=-1)
return X_train_seq, X_test_seq, y_train, y_test