[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. # Check if 'price_range' column is present in the test data
  9. if 'price_range' not in test_data.columns:
  10.     print("Error: 'price_range' column not found in the test data.")
  11. else:
  12.     # Separate features (X) and target variable (y)
  13.     X_train_cls = train_data.drop(columns=['price_range'])
  14.     y_train_cls = train_data['price_range']
  15.     X_test_cls = test_data.drop(columns=['price_range'])  # Ensure 'price_range' column is not dropped
  16.     y_test_cls = test_data['price_range']
  17.  
  18.     # Train the classifier
  19.     classifier = RandomForestClassifier(n_estimators=100, random_state=42)
  20.     classifier.fit(X_train_cls, y_train_cls)
  21.  
  22.     # Predict on the test set
  23.     y_pred_cls = classifier.predict(X_test_cls)
  24.  
  25.     # Evaluate the classifier
  26.     accuracy = accuracy_score(y_test_cls, y_pred_cls)
  27.     print("Accuracy:", accuracy)

Editor

You can edit this paste and save as new:


File Description
  • J
  • Paste Code
  • 27 Apr-2024
  • 1.13 Kb
You can Share it: