File size: 2,638 Bytes
c63475a
 
 
 
 
 
 
 
 
95ffd69
c63475a
 
 
 
 
 
 
 
 
 
 
8f5ebe6
c63475a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f5ebe6
c63475a
 
 
 
 
 
 
 
 
95ffd69
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

class ClassificationModels:
    def __init__(self, X, y):
        self.X = X
        self.y = y

    def split_data(self, test_size=0.2, random_state=42):
        self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
            self.X, self.y, test_size=test_size, random_state=random_state
        )


    def naive_bayes_classifier(self):
        model = GaussianNB()
        model.fit(self.X_train, self.y_train)
        return model

    def logistic_regression(self, params=None):
        model = LogisticRegression()
        if params:
            model = GridSearchCV(model, params, cv=5)
        model.fit(self.X_train, self.y_train)
        return model

    def decision_tree(self, params=None):
        model = DecisionTreeClassifier()
        if params:
            model = GridSearchCV(model, params, cv=5)
        model.fit(self.X_train, self.y_train)
        return model

    def random_forests(self, params=None):
        model = RandomForestClassifier()
        if params:
            model = GridSearchCV(model, params, cv=5)
        model.fit(self.X_train, self.y_train)
        return model

    def support_vector_machines(self, params=None):
        model = SVC()
        if params:
            model = GridSearchCV(model, params, cv=5)
        model.fit(self.X_train, self.y_train)
        return model

    def k_nearest_neighbour(self, params=None):
        model = KNeighborsClassifier()
        if params:
            model = GridSearchCV(model, params, cv=5)
        model.fit(self.X_train, self.y_train)
        return model


    def k_means_clustering(self, n_clusters):
        model = KMeans(n_clusters=n_clusters)
        model.fit(self.X_train)
        return model

    def evaluate_model(self, model):
        y_pred = model.predict(self.X_test)
        accuracy = accuracy_score(self.y_test, y_pred)
        return accuracy
    
    def evaluate_classification_report(self,model):
        y_pred = model.predict(self.X_test)
        return classification_report(self.y_test, y_pred, output_dict=True)
    
    def predict_output(self, model):
        y_pred = model.predict(self.X_test)
        return y_pred