-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
70 lines (58 loc) · 1.88 KB
/
data.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
#!/usr/bin/python3
'''
Author: Ambareesh Ravi
Date: 27 July, 2021
File: data.py
Description:
Handles the loading of dataset
'''
# imports
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
class Dataset:
# Class to load data as csv file
def __init__(self, file_name = "data/spam.csv"):
'''
Initializes the class object
Args:
file_name: file path as <str>
Returns:
-
Exception:
-
'''
self.df = pd.read_csv(file_name)
self.df["v1"] = [1 if i == "spam" else 0 for i in self.df["v1"]]
self.X = np.array(self.df["v2"])
self.y = np.array(self.df["v1"])
def __call__(self,):
'''
Object call returns the dataframe
Args:
-
Returns:
data and labels as as <tuple>
Exception:
-
'''
return (self.X, self.y)
def balanced_sample(self, test_size = 0.25):
'''
Returns balanced sampling for training and testing the models
Args:
-
Returns:
train_data, train_labels, test_data, test_labels
Exception:
-
'''
normal_indices = np.argwhere(self.y == 0)
spam_indices = np.argwhere(self.y == 1)
train_spam, test_spam = train_test_split(spam_indices, test_size = test_size, shuffle = True)
train_normal, test_normal = train_test_split(normal_indices, test_size = test_size, shuffle = True)
train_indices = np.concatenate((train_normal, train_spam))
test_indices = np.concatenate((test_normal, test_spam))
np.random.shuffle(train_indices)
np.random.shuffle(test_indices)
return self.X[train_indices].squeeze(), self.y[train_indices].squeeze(), self.X[test_indices].squeeze(), self.y[test_indices].squeeze()