getml.data
Contains functionalities for importing, handling, and retrieving data from the getML Engine.
All data relevant for the getML Suite has to be present in the getML Engine. Its Python API itself does not store any of the data used for training or prediction. Instead, it provides a handler class for the data frame objects in the getML Engine, the DataFrame
. Either using this overall handler for the underlying data set or the individual columns
it is composed of, one can both import and retrieve data from the Engine as well as performing operations on them. In addition to the data frame objects, the Engine also uses an abstract and lightweight version of the underlying data model, which is represented by the Placeholder
.
In general, working with data within the getML Suite is organized in three different steps.
- Importing the data into the getML Engine .
- Annotating the data by assigning
roles
to the individualcolumns
- Constructing the data model by deriving
Placeholder
from the data and joining them to represent the data schema.
Example
Creating a new data frame object in the getML Engine and importing data is done by one of the class methods from_csv
, from_db
, from_json
, or from_pandas
.
In this example we chose to directly load data from a public database in the internet. But, firstly, we have to connect the getML Engine to the database (see MySQL interface in the user guide for further details).
getml.database.connect_mysql(
host="db.relational-data.org",
dbname="financial",
port=3306,
user="guest",
password="relational",
time_formats=['%Y/%m/%d']
)
Using the established connection, we can tell the Engine to construct a new data frame object called df_loan
, fill it with the data of loan
table contained in the MySQL database, and return a DataFrame
handler associated with it.
loan = getml.DataFrame.from_db('loan', 'df_loan')
print(loan)
| loan_id | account_id | amount | duration | date | payments | status |
| unused float | unused float | unused float | unused float | unused string | unused string | unused string |
-------------------------------------------------------------------------------------------------------------
| 4959 | 2 | 80952 | 24 | 1994-01-05 | 3373.00 | A |
| 4961 | 19 | 30276 | 12 | 1996-04-29 | 2523.00 | B |
| 4962 | 25 | 30276 | 12 | 1997-12-08 | 2523.00 | A |
| 4967 | 37 | 318480 | 60 | 1998-10-14 | 5308.00 | D |
| 4968 | 38 | 110736 | 48 | 1998-04-19 | 2307.00 | C |
set_role
method (see Annotating data for details). (For demonstration purposes, we assign payments
the target role
. In reality, you would want to forecast the defaulting behaviour, which is encoded in the status
column. See the loans notebook.)
loan.set_role(["duration", "amount"], getml.data.roles.numerical)
loan.set_role(["loan_id", "account_id"], getml.data.roles.join_key)
loan.set_role("date", getml.data.roles.time_stamp)
loan.set_role(["payments"], getml.data.roles.target)
print(loan)
| date | loan_id | account_id | payments | duration | amount | status |
| time stamp | join key | join key | target | numerical | numerical | unused string |
-----------------------------------------------------------------------------------------------------------
| 1994-01-05T00:00:00.000000Z | 4959 | 2 | 3373 | 24 | 80952 | A |
| 1996-04-29T00:00:00.000000Z | 4961 | 19 | 2523 | 12 | 30276 | B |
| 1997-12-08T00:00:00.000000Z | 4962 | 25 | 2523 | 12 | 30276 | A |
| 1998-10-14T00:00:00.000000Z | 4967 | 37 | 5308 | 60 | 318480 | D |
| 1998-04-19T00:00:00.000000Z | 4968 | 38 | 2307 | 48 | 110736 | C |
Placeholder
from each DataFrame
and establishing relations between them using the join
method. # But, first, we need a second data set to build a data model.
trans = getml.DataFrame.from_db(
'trans', 'df_trans',
roles = {getml.data.roles.numerical: ["amount", "balance"],
getml.data.roles.categorical: ["type", "bank", "k_symbol",
"account", "operation"],
getml.data.roles.join_key: ["account_id"],
getml.data.roles.time_stamp: ["date"]
}
)
ph_loan = loan.to_placeholder()
ph_trans = trans.to_placeholder()
ph_loan.join(ph_trans, on="account_id",
time_stamps="date")
The data model contained in ph_loan
can now be used to construct a Pipeline
.
arange
arange(
start: Union[Real, float] = 0.0,
stop: Optional[Union[Real, float]] = None,
step: Union[Real, float] = 1.0,
)
Returns evenly spaced variables, within a given interval.
PARAMETER | DESCRIPTION |
---|---|
start | The beginning of the interval. Defaults to 0. |
stop | The end of the interval. |
step | The step taken. Defaults to 1. |
Source code in getml/data/columns/columns.py
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 |
|
rowid
rowid() -> FloatColumnView
Get the row numbers of the table.
RETURNS | DESCRIPTION |
---|---|
FloatColumnView | (numerical) column containing the row id, starting with 0 |
Source code in getml/data/columns/columns.py
162 163 164 165 166 167 168 169 |
|
list_data_frames
Lists all available data frames of the project.
RETURNS | DESCRIPTION |
---|---|
dict | Dict containing lists of strings representing the names of the data frames objects
|
Example
d, _ = getml.datasets.make_numerical()
getml.data.list_data_frames()
d.save()
getml.data.list_data_frames()
Source code in getml/data/helpers.py
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 |
|
delete
delete(name: str)
If a data frame named 'name' exists, it is deleted.
PARAMETER | DESCRIPTION |
---|---|
name | Name of the data frame. TYPE: |
Source code in getml/data/helpers2.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
exists
exists(name: str)
Returns true if a data frame named 'name' exists.
PARAMETER | DESCRIPTION |
---|---|
name | Name of the data frame. TYPE: |
Source code in getml/data/helpers2.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
load_data_frame
Retrieves a DataFrame
handler of data in the getML Engine.
A data frame object can be loaded regardless if it is held in memory or not. It only has to be present in the current project and thus listed in the output of list_data_frames
.
PARAMETER | DESCRIPTION |
---|---|
name | Name of the data frame. TYPE: |
RETURNS | DESCRIPTION |
---|---|
DataFrame | Handle the underlying data frame in the getML Engine. |
Example
d, _ = getml.datasets.make_numerical(population_name = 'test')
d2 = getml.data.load_data_frame('test')
Source code in getml/data/helpers2.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
make_target_columns
Returns a view containing binary target columns.
getML expects binary target columns for classification problems. This helper function allows you to split up a column into such binary target columns.
PARAMETER | DESCRIPTION |
---|---|
base | The original view or data frame. |
colname | The column you would like to split. A column named TYPE: |
RETURNS | DESCRIPTION |
---|---|
View | A view containing binary target columns. |
Source code in getml/data/helpers2.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
to_placeholder
to_placeholder(
*args: Union[
DataFrame, View, List[Union[DataFrame, View]]
],
**kwargs: Union[
DataFrame, View, List[Union[DataFrame, View]]
]
) -> List[Placeholder]
Factory function for extracting placeholders from a DataFrame
or View
.
PARAMETER | DESCRIPTION |
---|---|
args | The data frames or views you would like to convert to placeholders. TYPE: |
kwargs | The data frames or views you would like to convert to placeholders. TYPE: |
RETURNS | DESCRIPTION |
---|---|
List[Placeholder] | A list of placeholders. |
Example
Suppose we wanted to create a DataModel
:
dm = getml.data.DataModel(
population_train.to_placeholder("population")
)
# Add placeholders for the peripheral tables.
dm.add(meta.to_placeholder("meta"))
dm.add(order.to_placeholder("order"))
dm.add(trans.to_placeholder("trans"))
But this is a bit repetitive. So instead, we can do the following:
dm = getml.data.DataModel(
population_train.to_placeholder("population")
)
# Add placeholders for the peripheral tables.
dm.add(getml.data.to_placeholder(
meta=meta, order=order, trans=trans))
Source code in getml/data/helpers2.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
|
load_container
Loads a container and all associated data frames from disk.
PARAMETER | DESCRIPTION |
---|---|
container_id | The id of the container you would like to load. TYPE: |
RETURNS | DESCRIPTION |
---|---|
Container | The container with the given id. |
Source code in getml/data/load_container.py
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 |
|
concat
Creates a new data frame by concatenating a list of existing ones.
PARAMETER | DESCRIPTION |
---|---|
name | Name of the new column. TYPE: |
data_frames | The data frames to concatenate. Must be non-empty. However, it can contain only one data frame. Column names and roles must match. Columns will be appended by name, not order. |
Examples:
new_df = data.concat("NEW_DF_NAME", [df1, df2])
Source code in getml/data/concat.py
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 |
|
random
random(seed: int = 5849) -> FloatColumnView
Create random column.
The numbers will be uniformly distributed from 0.0 to 1.0. This can be used to randomly split a population table into a training and a test set
PARAMETER | DESCRIPTION |
---|---|
seed | Seed used for the random number generator. TYPE: |
RETURNS | DESCRIPTION |
---|---|
FloatColumnView | FloatColumn containing random numbers |
Example
population = getml.DataFrame('population')
population.add(numpy.zeros(100), 'column_01')
idx = random(seed=42)
population_train = population[idx > 0.7]
population_test = population[idx <= 0.7]
Source code in getml/data/columns/random.py
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 |
|
OnType module-attribute
Types that can be passed to the 'on' argument of the 'join' method.