[text] G

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) for both train and test sets
  9. X_train_cls = train_data.drop(columns=['price_range'])
  10. y_train_cls = train_data['price_range']
  11. X_test_cls = test_data.dropna().drop(columns=['price_range'])  # Remove rows with missing values and drop 'price_range' column
  12. y_test_cls = test_data.dropna()['price_range']  # Select corresponding labels for non-missing rows
  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
  • G
  • Paste Code
  • 27 Apr-2024
  • 1.02 Kb
You can Share it: