File size: 2,709 Bytes
f8a4ded
 
 
 
 
 
 
 
 
71dd3e7
f8a4ded
 
 
 
4ccd84b
 
 
 
 
 
 
 
 
 
 
 
 
 
f8a4ded
4ccd84b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.svm import SVC, SVR
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error, mean_absolute_error, r2_score

def train_all_models(X, y):
    """
    Train all models and return a DataFrame with evaluation metrics.
    """
    model_results = []
    models = [
        ("Logistic Regression", LogisticRegression(), 'classification'),
        ("Random Forest", RandomForestClassifier(), 'classification'),
        ("SVM", SVC(), 'classification'),
        ("KNN", KNeighborsClassifier(), 'classification'),
        ("Decision Tree", DecisionTreeClassifier(), 'classification'),
        ("Naive Bayes", GaussianNB(), 'classification'),
        ("Linear Regression", LinearRegression(), 'regression'),
        ("Random Forest Regressor", RandomForestRegressor(), 'regression'),
        ("SVR", SVR(), 'regression'),
        ("KNN Regressor", KNeighborsRegressor(), 'regression'),
        ("Decision Tree Regressor", DecisionTreeRegressor(), 'regression')
    ]
    
    for model_name, model, task_type in models:
        # Split data into train and test sets
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
        
        # Train the model
        model.fit(X_train, y_train)
        
        # Make predictions
        y_pred = model.predict(X_test)
        
        # Evaluate based on task type
        if task_type == 'classification':
            metrics = {
                "Model": model_name,
                "Accuracy": accuracy_score(y_test, y_pred),
                "Precision": precision_score(y_test, y_pred, average='weighted', zero_division=0),
                "Recall": recall_score(y_test, y_pred, average='weighted', zero_division=0),
                "F1 Score": f1_score(y_test, y_pred, average='weighted', zero_division=0)
            }
        else:
            metrics = {
                "Model": model_name,
                "MSE": mean_squared_error(y_test, y_pred),
                "MAE": mean_absolute_error(y_test, y_pred),
                "R2 Score": r2_score(y_test, y_pred)
            }
        model_results.append(metrics)
    
    # Convert to DataFrame for display
    model_results_df = pd.DataFrame(model_results)
    return model_results_df