forked from aMLLibrary/aMLLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression_inputs.py
More file actions
136 lines (104 loc) · 4.34 KB
/
Copy pathregression_inputs.py
File metadata and controls
136 lines (104 loc) · 4.34 KB
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
"""
Copyright 2019 Marco Lattuada
Copyright 2022 Nahuel Coliva
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
class RegressionInputs:
"""
Data structure storing inputs information for a regression problem
It wraps a pandas dataframe which actually includes all the data, including all the dataset (i.e., training, hyperparameter selection, validation) and all the columns (i.e., both original and derived by preprocessing steps).
The dataframe is "filtered" by means of x_columns and input_split which determine which are the columns and rows to be considered.
Moreover, it contains the y column and all the scalers used to generate scaled column.
Attributes
----------
data: dataframe
The whole dataframe
input_split: dict of str: set(int)
For each considered set (i.e., training, hyperparameter selection, validation) the indices of the rows which belong to that set
x_columns: list of strings
The labels of the columns of the data frame to be used to train the model
y_column: string
The label of the y column
scalers: dict str->sklearn.preprocessing.StandardScaler
The scaler which has been used to scale the input
scaled_columns: list of strings
The list of columns which have been scaled
Methods
-------
_get_data()
Extacts a portion of the data frame
get_xy_data()
Generates the two pandas data frame with x_columns and y
copy()
Returns a copy of this object
__copy__()
Hidden method that actually performs the copy
"""
def __init__(self, data, inputs_split, x_cols, y_column):
"""
Parameters
data: dataframe
The whole dataframe
inputs_split: map of str to list of integers
How the input is split. Key is the type of set (e.g., training, cv1, validation), value is the list of rows belonging to that set
x_cols: list of strings
The labels of the columns of the data frame to be used to train the model
y_column: string
The label of the y column
"""
self.data = data
self.inputs_split = inputs_split
self.x_columns = x_cols
self.scalers = {}
self.y_column = y_column
self.scaled_columns = []
def __copy__(self):
new_copy = RegressionInputs(self.data.copy(), self.inputs_split.copy(), self.x_columns.copy(), self.y_column)
new_copy.scalers = self.scalers.copy()
new_copy.scaled_columns = self.scaled_columns.copy()
return new_copy
def copy(self):
return self.__copy__()
def __str__(self):
ret = "x_columns: " + str(self.x_columns) + " - y_column: " + self.y_column + "\n"
for name, values in self.inputs_split.items():
ret = ret + name + ": " + str(values) + "\n"
ret = ret + "Dimensions: " + str(self.data.shape)
return ret
def _get_data(self, rows, columns):
"""
Extract a portion of the data frame as a matrix
Parameters
----------
rows: list of integers
The list of rows to be extracted
columns: list of str
The list of columns to be extracted
Returns
matrix
The specified subset of the data frame
"""
return self.data.loc[rows, columns]
def get_xy_data(self, rows):
"""
Generate the x and y pandas dataframes containing only the necessary information
Parameters
----------
rows: list of integer
The list of rows to be considered
Returns
-------
df,df
The data frame containing the x_columns column and the data frame containing the y column
"""
xdata = self._get_data(rows, self.x_columns)
ydata = self._get_data(rows, self.y_column)
return xdata, ydata