ml.py 846 B

12345678910111213141516171819202122232425262728
  1. # train_model.py
  2. import pandas as pd
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.ensemble import RandomForestClassifier
  5. from sklearn.metrics import accuracy_score
  6. import joblib
  7. # Load the collected data from CSV
  8. data = pd.read_csv('wifi_signals.csv')
  9. # Features (SignalStrength) and target (Location)
  10. X = data[['SignalStrength']]
  11. y = data['Location']
  12. # Split the data into training and test sets (80% train, 20% test)
  13. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  14. # Train a Random Forest Classifier
  15. model = RandomForestClassifier()
  16. model.fit(X_train, y_train)
  17. # Evaluate the model
  18. y_pred = model.predict(X_test)
  19. accuracy = accuracy_score(y_test, y_pred)
  20. print(f'Model Accuracy: {accuracy * 100:.2f}%')
  21. # Save the trained model to a file
  22. joblib.dump(model, 'wifi_model.pkl')