[text] J

Viewer

  1. from sklearn.ensemble import RandomForestClassifier
  2. from sklearn.metrics import accuracy_score
  3.  
  4. # Load train and test data for classification
  5. train_data = pd.read_excel('your_file.xlsx', sheet_name='Train set - Classification')
  6. test_data = pd.read_excel('your_file.xlsx', sheet_name='Test set - Classification')
  7.  
  8. # Separate features (X) and target variable (y)
  9. X_train_cls = train_data.drop(columns=['price_range'])
  10. y_train_cls = train_data['price_range']
  11. X_test_cls = test_data.drop(columns=['price_range'])
  12. y_test_cls = test_data['price_range']
  13.  
  14. # Train the classifier
  15. classifier = RandomForestClassifier(n_estimators=100, random_state=42)
  16. classifier.fit(X_train_cls, y_train_cls)
  17.  
  18. # Predict on the test set
  19. y_pred_cls = classifier.predict(X_test_cls)
  20.  
  21. # Evaluate the classifier
  22. accuracy = accuracy_score(y_test_cls, y_pred_cls)
  23. print("Accuracy:", accuracy)

Editor

You can edit this paste and save as new:


File Description
  • J
  • Paste Code
  • 27 Apr-2024
  • 881 Bytes
You can Share it: