# Normalize data scaler = MinMaxScaler() data_scaled = scaler.fit_transform(data)
```
iso_forest=IsolationForest(contamination=0.03,random_state=42) data[\"Anomaly_Score_IF\"]=iso_forest.fit_predict(data_scaled) data[\"Anomaly_IF\"]=data[\"Anomaly_Score_IF\"].apply(lambdax:1 ifx=-1 else0) #Display anomaly detection results print(data.head())
```  
_Appendix A.2 LSTM Autoencoder Implementation (Code 2)_  
LSTM Autoencoder model for sequential anomaly detection, optimized for Edge AI deployment. ```
importnumpyasnp importpandasaspd importtensorflowastf fromtensorflow.keras.modelsimportSequential fromtensorflow.keras.layers importLSTM,Dense,Dropout,RepeatVector, TimeDistributed from sklearn.preprocessingimportMinMaxScaler #GeneratesyntheticIoTata np.random.seed(42) time_steps=1000
```  
temperature=np.random.normal(loc=22,scale=2,size=time_steps) motion=np.random.choice([0,1],size=time_steps,\(p=[0.95,0.05]) energy_usage=np.random.normal(loc=500,scale=50,size=time_steps)  
#Injectanomalies anomaly_indices=np.random.choice(time_steps,size=30,replace=False) temperature[anomaly_indices]+=np.random.uniform(5,10,size=30) motion[anomaly_indices]=1 energy_usage[anomaly_indices]+=np.random.uniform(200,400,size=30)  
#CreateaDataFrameFrame data=pd.DataFrameFrame([\"Temperature\":temperature,\"Motion\":motion, \"Ener-gy_Usage\":energy_usage])
#Normalizedata scaler=MinMaxScaler() data_scaled=scaler.fit_transform(data) X_train=data_scaled.reshape((data_scaled.shape[0],1,data_scaled.shape[1]))#ReshapeforLSTM  
#DefineLSTM Autoencoder model=Sequential([ LSTM(32,activation='relu',return_sequences=True,input_shape=(1,X_train.shape[2])), Dropout(0.2), LSTM(16,activation='relu',return_sequences=False), RepeatVector(1), LSTM(16,activation='relu',return_sequences=True), Dropout(0.2), LSTM(32,activation='relu',return_sequences=True), TimeDistributed(Dense(X_train.shape[2])) ] model.compile(optimizer='adam',loss='mse')  
## References  
* (1) Advantech Co., Ltd. Edge AI Explained: Uses, How it Works & More. Available online: [[https://www.advantech.com/en-us/resources/industry-focus/edge-ai](https://www.advantech.com/en-us/resources/industry-focus/edge-ai)]([https://www.advantech.com/en-us/resources/industry-focus/edge-ai](https://www.advantech.com/en-us/resources/industry-focus/edge-ai)) (accessed on 13 February 2025). * (2) [PERSON]; [PERSON]; [PERSON]; [PERSON] CADeSH: Collaborative Anomaly Detection for Smart Homes. _IEEE Int. Things J._**2022**, _10_, 8514-8532. [CrossRef]
* (3) [PERSON] Edge AI for Smart Home Applications. Available online: [[https://www.xenonstack.com/use-cases/edge-ai-for-home-applications](https://www.xenonstack.com/use-cases/edge-ai-for-home-applications)]([https://www.xenonstack.com/use-cases/edge-ai-for-home-applications](https://www.xenonstack.com/use-cases/edge-ai-for-home-applications)) (accessed on 13 February 2025). * (4) [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; et al. Make Your Home Safe: Time-aware Unsupervised User Behavior Anomaly Detection in Smart Homes via Loss-guided Mask. In Proceedings of the 30 th ACM SIGKDD Conference on Knowledge Discovery and Data Mining, Barcelona, Spain, 25-29 August 2024; pp. 3551-3562. [CrossRef]
* (5) [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON] IoT Network Anomaly Detection in Smart Homes Using Machine Learning. _IEEE Access_**2023**, _11_, 119462-119480. [CrossRef]
* (6) [PERSON]; [PERSON] Enhancing unsupervised anomaly-based cyberattacks detection in smart homes through hyperparameter optimization. _Int. J. Inf. Secur._**2024**, _24_, 45. [CrossRef]
* (7) [PERSON] Deep Learning Anomaly Detection Using Edge AI. Master's Thesis, University of Central Florida, Orlando, FL, USA, January 2022. Available online: [[https://stars.library.ucf.edu/etd2020/1026](https://stars.library.ucf.edu/etd2020/1026)]([https://stars.library.ucf.edu/etd2020/1026](https://stars.library.ucf.edu/etd2020/1026)) (accessed on 14 March 2025). * (8) Arm Ltd. Edge AI, Arm! The Architecture for the Digital World.