Skip to content

khiops.sklearn.dataset

Submodule of khiops.sklearn

Classes for handling diverse data tables

CLASS DESCRIPTION
Dataset

A representation of a dataset

DatasetTable

A generic dataset table

PandasTable

DatasetTable encapsulating a pandas dataframe

NumpyTable

DatasetTable encapsulating a NumPy array

SparseTable

DatasetTable encapsulating a SciPy sparse matrix

FUNCTION DESCRIPTION
check_dataset_spec

Checks that a dataset spec is valid

table_name_of_path

Returns the table name as the last fragment of the table data path

get_khiops_type

Translates a numpy dtype to a Khiops dictionary type

get_khiops_variable_name

Return the khiops variable name associated to a column id

read_internal_data_table

Reads into a DataFrame a data table file with the internal format settings

write_internal_data_table

Writes a DataFrame to data table file with the internal format settings

Classes

Dataset(X, y=None, categorical_target=True)

A representation of a dataset

PARAMETER DESCRIPTION
X

Either:

  • A single dataframe

  • A dict dataset specification

TYPE: DataFrame or dict

y

The target column.

TYPE: (Series, DataFrame or ndarray) DEFAULT: None

categorical_target

True if the vector y should be considered as a categorical variable. If False it is considered as numeric. Ignored if y is None.

TYPE: bool DEFAULT: True

METHOD DESCRIPTION
to_spec

Returns a dictionary specification of this dataset

copy

Creates a copy of the dataset

get_table

Returns a table by its name

create_khiops_dictionary_domain

Creates a Khiops dictionary domain representing this dataset

create_table_files_for_khiops

Prepares the tables of the dataset to be used by Khiops

ATTRIBUTE DESCRIPTION
table_type

type : The table type of this dataset's tables

is_multitable

bool : True if the dataset is multitable

Attributes

table_type property

type : The table type of this dataset's tables

Possible values:

is_multitable property

bool : True if the dataset is multitable

Methods:

to_spec()

Returns a dictionary specification of this dataset

copy()

Creates a copy of the dataset

Referenced pandas.DataFrame, numpy.ndarray and scipy.sparse.spmatrix objects in tables are copied as references.

get_table(table_name)

Returns a table by its name

PARAMETER DESCRIPTION
table_name

The name of the table to be retrieved.

RETURNS DESCRIPTION
DatasetTable

The table object for the specified name.

RAISES DESCRIPTION
KeyError

If there is no table with the specified name.

create_khiops_dictionary_domain()

Creates a Khiops dictionary domain representing this dataset

RETURNS DESCRIPTION
DictionaryDomain

The DictionaryDomain object representing this dataset

create_table_files_for_khiops(output_dir, sort=True)

Prepares the tables of the dataset to be used by Khiops

If this is a multi-table dataset it will create sorted copies the tables.

PARAMETER DESCRIPTION
output_dir

The directory where the sorted tables will be created.

TYPE: str

RETURNS DESCRIPTION
tuple

A tuple containing:

  • The path of the main table
  • A dictionary containing the relation [data-path -> file-path] for the secondary tables. The dictionary is empty for monotable datasets.

DatasetTable(name, key=None)

Bases: ABC

A generic dataset table

METHOD DESCRIPTION
check_key

Checks that the key columns exist

create_table_file_for_khiops

Creates a copy of the table at the specified directory

n_features

Returns the number of features of the table

create_khiops_dictionary

Creates a Khiops dictionary representing this table

Methods:

check_key()

Checks that the key columns exist

create_table_file_for_khiops(output_dir, sort=True) abstractmethod

Creates a copy of the table at the specified directory

n_features()

Returns the number of features of the table

The target column does not count.

create_khiops_dictionary()

Creates a Khiops dictionary representing this table

RETURNS DESCRIPTION
Dictionary

The Khiops Dictionary object describing this table's schema

PandasTable(name, dataframe, key=None)

Bases: DatasetTable

DatasetTable encapsulating a pandas dataframe

PARAMETER DESCRIPTION
name

Name for the table.

TYPE: str

dataframe

The data frame to be encapsulated. It must be non-empty.

TYPE: DataFrame

key

The names of the columns composing the key.

TYPE: list of str DEFAULT: None

NumpyTable(name, array, key=None)

Bases: DatasetTable

DatasetTable encapsulating a NumPy array

PARAMETER DESCRIPTION
name

Name for the table.

TYPE: str

array

The data frame to be encapsulated.

TYPE: numpy.ndarray of shape (n_samples, n_features_in) or Sequence

key

The names of the columns composing the key.

TYPE: array-like of int DEFAULT: None

SparseTable(name, matrix, key=None)

Bases: DatasetTable

DatasetTable encapsulating a SciPy sparse matrix

PARAMETER DESCRIPTION
name

Name for the table.

TYPE: str

matrix

The sparse matrix to be encapsulated.

TYPE: spmatrix

key

The names of the columns composing the key.

TYPE: list of str DEFAULT: None

METHOD DESCRIPTION
create_khiops_dictionary

Creates a Khiops dictionary representing this sparse table

Methods:

create_khiops_dictionary()

Creates a Khiops dictionary representing this sparse table

Adds metadata to each sparse variable

RETURNS DESCRIPTION
Dictionary

The Khiops [Dictionary][khiops.dictionary.Dictionary] object describing this table's schema

Functions:

check_dataset_spec(ds_spec)

Checks that a dataset spec is valid

PARAMETER DESCRIPTION
ds_spec

A specification of a multi-table dataset (see Multi-Table Learning Primer).

TYPE: dict

RAISES DESCRIPTION
TypeError

If there are objects of the spec with invalid type.

ValueError

If there are objects of the spec with invalid values.

table_name_of_path(table_path)

Returns the table name as the last fragment of the table data path

PARAMETER DESCRIPTION
table_path

Data path of the table, in the format "path/to/table".

RETURNS DESCRIPTION
str

The name of the table.

get_khiops_type(numpy_type, categorical_str_max_size=None)

Translates a numpy dtype to a Khiops dictionary type

PARAMETER DESCRIPTION
numpy_type

Numpy type of the column

TYPE: dtype

categorical_str_max_size

Maximum length of the entries of the column whose type is numpy_type.

TYPE: int DEFAULT: None

RETURNS DESCRIPTION
str

Khiops type name. Either "Categorical", "Text", "Numerical" or "Timestamp".

Note
The "Text" Khiops type is inferred if the NumPy type is "string"
and the maximum length of the entries of that type is greater than 100.

get_khiops_variable_name(column_id)

Return the khiops variable name associated to a column id

read_internal_data_table(file_path_or_stream, column_dtypes=None)

Reads into a DataFrame a data table file with the internal format settings

The table is read with the following settings:

  • Use tab as separator
  • Read the column names from the first line
  • Use '"' as quote character
  • Use csv.QUOTE_MINIMAL
  • double quoting enabled (quotes within quotes can be escaped with '""')
  • UTF-8 encoding
  • User-specified dtypes (optional)
PARAMETER DESCRIPTION
file_path_or_stream

The path of the internal data table file to be read or a readable file object.

TYPE: str or file object

column_dtypes

Dictionary linking column names with dtypes. See dtype parameter of the pandas.read_csv function. If not set, then the column types are detected automatically by pandas.

TYPE: dict DEFAULT: None

RETURNS DESCRIPTION
DataFrame

The dataframe representation of the data table.

write_internal_data_table(dataframe, file_path_or_stream)

Writes a DataFrame to data table file with the internal format settings

The table is written with the following settings:

  • Use tab as separator
  • Write the column names on the first line
  • Use '"' as quote character
  • Use csv.QUOTE_MINIMAL
  • double quoting enabled (quotes within quotes can be escaped with '""')
  • UTF-8 encoding
  • The index is not written

Khiops cannot handle multi-line records. Hence, the carriage returns / line feeds need to be removed from the records before data is handed over to Khiops.

PARAMETER DESCRIPTION
dataframe

The dataframe to write.

TYPE: DataFrame

file_path_or_stream

The path of the internal data table file to be written or a writable file object.

TYPE: str or file object