-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
66 lines (51 loc) · 2.4 KB
/
task.py
File metadata and controls
66 lines (51 loc) · 2.4 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Load Smart Home Dataset
def load_data(partition_id, num_partitions):
# Load both datasets to ensure consistent label encoding
data_1 = pd.read_csv("hh101.ann.features.csv")
data_2 = pd.read_csv("hh102.ann.features.csv")
# Combine data to fit the LabelEncoder with all possible classes
combined_data = pd.concat([data_1, data_2], axis=0)
# Fit the LabelEncoder on the combined data to ensure consistent encoding
label_encoder = LabelEncoder()
label_encoder.fit(combined_data['activity'])
# Load the correct partition's dataset
if partition_id == 0:
data = data_1
else:
data = data_2
# Separate features and target variable
X = data.drop(columns=['activity'])
y = data['activity']
# Normalize the features column-wise
X = X.apply(lambda x: x / (x.max() + 1e-8), axis=0) # Adding a small constant to avoid division by zero
# Encode the labels using the previously fitted LabelEncoder
y = label_encoder.transform(y) # Consistent encoding across both clients
# Debug: Check the range of the encoded labels
print(f"Max label value for partition {partition_id}: {y.max()}")
# Split into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X.values, y, test_size=0.2, random_state=42)
# Reshape X for LSTM input
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))
return X_train, y_train, X_test, y_test
# Define LSTM Model for Multi-Class Classification
def load_model(input_shape=(45, 1), num_classes=37): # Updated to 37 classes
model = Sequential()
model.add(LSTM(128, input_shape=input_shape, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax')) # Multi-class classification with softmax
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy', # Multi-class classification loss
metrics=['accuracy']
)
return model