-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
180 lines (133 loc) · 4.48 KB
/
datasets.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
from collections import Counter
import pandas as pd
from sklearn.datasets import fetch_covtype
from sklearn.preprocessing import LabelEncoder
__all__ = ['load_adult', 'load_cover_type', 'load_diabetes',
'load_mammography', 'load_oil', 'load_phoneme', 'load_satimage']
def load_adult(only_numeric=True, dropna=True):
"""Load the adult dataset.
Parameters
----------
only_numeric : bool
Whether to return a dataframe containing only the numeric
features. By default, only numerical features are returned.
dropna : bool
Whether to drop rows containing NA values.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
NUMERIC_COLUMNS = ['age', 'fnlwgt', 'education-num', 'capitalgain',
'capitalloss', 'hoursperweek']
df = pd.read_csv(os.path.join('data', 'adult.csv'), na_values=['?'])
if dropna:
df = df.dropna()
target = df['class']
data = df.drop(columns='class')
encoder = LabelEncoder()
target = encoder.fit_transform(target)
if only_numeric:
return data[NUMERIC_COLUMNS], target
return data, target
def load_cover_type():
"""Load the Forest cover type dataset.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
data, target = fetch_covtype(return_X_y=True)
# select only the class 3 and 4
mask = (target == 3) | (target == 4)
data = data[mask]
target = target[mask]
encoder = LabelEncoder()
target = encoder.fit_transform(target)
return data, target
def load_diabetes():
"""Load the Pima Indian Diabetes.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
df = pd.read_csv(os.path.join('data', 'diabetes.csv'))
target = df['class']
data = df.drop(columns='class')
encoder = LabelEncoder()
target = encoder.fit_transform(target)
return data, target
def load_mammography():
"""Load the mammography dataset.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
df = pd.read_csv(os.path.join('data', 'mammography.csv'))
target = df['class']
data = df.drop(columns='class')
encoder = LabelEncoder()
target = encoder.fit_transform(target)
return data, target
def load_oil():
"""Load the oil dataset.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
df = pd.read_csv(os.path.join('data', 'oil.csv'))
target = df['class']
data = df.drop(columns='class')
encoder = LabelEncoder()
target = encoder.fit_transform(target)
return data, target
def load_phoneme():
"""Load the phoneme dataset.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
df = pd.read_csv(os.path.join('data', 'phoneme.csv'))
target = df['Class']
data = df.drop(columns='Class')
encoder = LabelEncoder()
target = encoder.fit_transform(target)
return data, target
def load_satimage():
"""Load the satellite image datasets.
Returns
-------
data : dataframe, shape (n_samples, n_features)
A dataframe containing the data.
target : ndarray, shape (n_samples,)
The target label associated to the data.
"""
df = pd.read_csv(os.path.join('data', 'satimage.csv'))
target = df['class']
data = df.drop(columns='class')
encoder = LabelEncoder()
target = encoder.fit_transform(target)
# find the minority class
target_counter = Counter(target)
minority_class = min(target_counter, key=target_counter.get)
mask_minority_class = target == minority_class
target[mask_minority_class] = 0
target[~mask_minority_class] = 1
return data, target