Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn.model_selection import GridSearchCV
- def evaluate_model(model, X_train, y_train, X_test, y_test, hyperparameters=None):
- if hyperparameters is not None:
- # Perform hyperparameter tuning using GridSearchCV
- grid_search = GridSearchCV(model, hyperparameters, scoring='accuracy', cv=5)
- grid_search.fit(X_train, y_train)
- best_model = grid_search.best_estimator_
- else:
- best_model = model
- # Train the best model on the entire training set
- best_model.fit(X_train, y_train)
- # Evaluate the model on the test set
- accuracy = best_model.score(X_test, y_test)
- return accuracy, best_model
- # Example usage
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.svm import SVC
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
- # Load dataset
- iris = load_iris()
- X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
- # Define models
- rf_model = RandomForestClassifier(random_state=42)
- svc_model = SVC(random_state=42)
- # Define hyperparameters for tuning (customize based on your needs)
- rf_hyperparameters = {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20]}
- svc_hyperparameters = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
- # Evaluate Random Forest with hyperparameter tuning
- rf_accuracy, best_rf_model = evaluate_model(rf_model, X_train, y_train, X_test, y_test, hyperparameters=rf_hyperparameters)
- # Evaluate SVC with hyperparameter tuning
- svc_accuracy, best_svc_model = evaluate_model(svc_model, X_train, y_train, X_test, y_test, hyperparameters=svc_hyperparameters)
- print(f"Random Forest Accuracy: {rf_accuracy}")
- print(f"Tuned Random Forest Model: {best_rf_model}")
- print(f"SVC Accuracy: {svc_accuracy}")
- print(f"Tuned SVC Model: {best_svc_model}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement