-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.py
70 lines (52 loc) · 1.82 KB
/
Helper.py
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
# -*- coding: utf-8 -*-
"""
Created on Sun May 07 21:34:16 2017
@author: raghavendra harish
"""
import numpy as np
import pandas as pd
from sklearn import metrics
from sklearn.base import TransformerMixin
class Preprocessor:
@staticmethod
def fill_nans(dat):
'''
Fills in NaNs with either the mean or the most common value.
'''
return DataFrameImputer().fit_transform(dat)
class DataFrameImputer(TransformerMixin):
'''
This class came from http://stackoverflow.com/questions/25239958/
impute-categorical-missing-values-in-scikit-learn
'''
def __init__(self):
'''
Impute missing values.
Columns of dtype object are imputed with the most frequent value in col.
Columns of other types are imputed with mean of column.
'''
def fit(self, X, y = None):
self.fill = pd.Series([X[c].value_counts().index[0]
if X[c].dtype == np.dtype('O') else X[c].mean() for c in X],
index = X.columns)
return self
def transform(self, X, y = None):
return X.fillna(self.fill)
class Performance:
@staticmethod
def get_perf(y, y_pred):
'''
This method outputs several performance metrics for classification.
'''
# Gets Confusion Matrix
#conf_matrix = metrics.confusion_matrix(y_true = y, y_pred = y_pred)
# Gets Accuracy
accuracy = metrics.accuracy_score(y_true = y, y_pred = y_pred)
# Gets Recall
recall = metrics.recall_score(y_true = y, y_pred = y_pred)
# Gets Precision
precision = metrics.precision_score(y_true = y, y_pred = y_pred)
# F1
f1 = metrics.f1_score(y_true = y, y_pred = y_pred)
return {'accuracy': accuracy, 'recall': recall,
'precision': precision, 'F1': f1}