# Logistic Regression¶

The Logistic Regression algorithm solves the classification problem and predicts class labels and probabilities of objects belonging to each class.

 Operation Computational methods Programming Interface Training dense_batch train(…) train_input train_result Inference dense_batch infer(…) infer_input infer_result

## Mathematical formulation¶

Refer to Developer Guide: Logistic Regression.

## Programming Interface¶

All types and functions are declared in the oneapi::dal::logistic_regression namespace and available via the inclusion of the oneapi/dal/algo/logistic_regression.hpp header file.

### Result Options¶

class result_option_id

Public Methods

constexpr result_option_id() = default
constexpr result_option_id(const result_option_id_base &base)

### Descriptor¶

template<typename Float = float, typename Method = method::by_default, typename Task = task::by_default, typename Optimizer = oneapi::dal::newton_cg::descriptor<Float>>
class descriptor
Template Parameters
• Float – The floating-point type that the algorithm uses for intermediate computations. Can be float or double.

• Method – Tag-type that specifies an implementation of algorithm. Can be method::dense_batch or method::sparse.

• Task – Tag-type that specifies type of the problem to solve. Can be task::classification.

• Optimizer – The descriptor of the optimizer used for minimization. Can be newton_cg::descriptor.

Constructors

descriptor(bool compute_intercept = true, double C = 1.0)

Creates a new instance of the class with the given compute_intercept and C.

descriptor(bool compute_intercept, double C, const optimizer_t &optimizer)

Creates a new instance of the class with the given compute_intercept, C and optimizer.

Properties

double inverse_regularization

Defines inverse regularization factor.

Getter & Setter
double get_inverse_regularization() const
auto & set_inverse_regularization(double C) const
std::int64_t class_count

Defines number of classes.

Getter & Setter
std::int64_t get_class_count() const
auto & set_class_count(std::int64_t class_count) const
const optimizer_t &optimizer
Getter & Setter
const optimizer_t & get_optimizer() const
auto & set_optimizer(const optimizer_t &opt)
bool compute_intercept

Defines should intercept be taken into consideration.

Getter & Setter
bool get_compute_intercept() const
auto & set_compute_intercept(bool compute_intercept) const
result_option_id result_options

Choose which results should be computed and returned.

Getter & Setter
result_option_id get_result_options() const
auto & set_result_options(const result_option_id &value)

#### Method Tags¶

struct dense_batch

Tag-type that denotes dense_batch computational method.

struct sparse

Tag-type that denotes sparse computational method.

using by_default = dense_batch

Alias tag-type for the method.

struct classification

Tag-type that parameterizes entities used for solving classification problem.

using by_default = classification

Alias tag-type for classification task.

### Model¶

class model
Template Parameters

Task – Tag-type that specifies type of the problem to solve.

Constructors

model()

Creates a new instance of the class with the default property values.

Properties

const table &packed_coefficients
Getter & Setter
const table & get_packed_coefficients() const
model & set_packed_coefficients(const table &t)

### Training train(...)¶

#### Input¶

class train_input
Template Parameters

Task – Tag-type that specifies type of the problem to solve. Can be task::classification.

Constructors

train_input(const table &data, const table &responses)

Creates a new instance of the class with the given data and responses property values.

Properties

const table &data

The training set X. Default value: table{}.

Getter & Setter
const table & get_data() const
auto & set_data(const table &data)
const table &responses

Vector of responses y for the training set X. Default value: table{}.

Getter & Setter
const table & get_responses() const
auto & set_responses(const table &responses)

#### Result¶

class train_result
Template Parameters

Task – Tag-type that specifies type of the problem to solve. Can be task::classification.

Constructors

train_result()

Creates a new instance of the class with the default property values.

Properties

std::int64_t iterations_count

Actual number of optimizer iterations.

Getter & Setter
std::int64_t get_iterations_count() const
auto & set_iterations_count(std::int64_t value)
const table &intercept

Table with Logistic Regression intercept.

Getter & Setter
const table & get_intercept() const
auto & set_intercept(const table &value)
const table &packed_coefficients

Table of Logistic Regression coefficients and intercept.

Getter & Setter
const table & get_packed_coefficients() const
auto & set_packed_coefficients(const table &value)

The trained Logistic Regression model. Default value: model<Task>{}.

Getter & Setter
const model< Task > & get_model() const
auto & set_model(const model< Task > &value)
const result_option_id &result_options

Result options that indicates availability of the properties.

Getter & Setter
const result_option_id & get_result_options() const
auto & set_result_options(const result_option_id &value)
const table &coefficients

Table of Logistic Regression coefficients.

Getter & Setter
const table & get_coefficients() const
auto & set_coefficients(const table &value)
std::int64_t inner_iterations_count

Number of optimizer subiterations.

Getter & Setter
std::int64_t get_inner_iterations_count() const
auto & set_inner_iterations_count(std::int64_t value)

#### Operation¶

template<typename Descriptor>
logistic_regression::train_result train(const Descriptor &desc, const logistic_regression::train_input &input)
Parameters
• desc – Logistic Regression algorithm descriptor logistic_regression::descriptor

• input – Input data for the training operation

Preconditions
input.data.has_data == true
input.responses.data.has_data == true
input.data.row_count == input.responses.row_count
input.responses.column_count == 1
desc.inverse_regularization > 0.0
desc.class_count == 2
Postconditions
result.coefficients.row_count = 1
result.coefficients.column_count = input.data.column_count
result.intercept.row_count = 1
result.intercept.column_count = 1
result.packed_coefficients.row_count = 1
result.packed_coefficients.column_count = input.data.column_count + 1

### Inference infer(...)¶

#### Input¶

class infer_input
Template Parameters

Task – Tag-type that specifies type of the problem to solve. Can be task::classification.

Constructors

infer_input(const table &data, const model<Task> &model)

Creates a new instance of the class with the given data and model property values.

Properties

The trained Logistic Regression model. Default value: model<Task>{}.

Getter & Setter
const model< Task > & get_model() const
auto & set_model(const model< Task > &m)
const table &data

The dataset for inference $$X'$$. Default value: table{}.

Getter & Setter
const table & get_data() const
auto & set_data(const table &data)

#### Result¶

class infer_result
Template Parameters

Task – Tag-type that specifies type of the problem to solve. Can be task::classification.

Constructors

infer_result()

Creates a new instance of the class with the default property values.

Properties

const table &responses

The predicted responses. Default value: table{}.

Getter & Setter
const table & get_responses() const
auto & set_responses(const table &value)
const table &probabilities

The predicted probabilities. Default value: table{}.

Getter & Setter
const table & get_probabilities() const
auto & set_probabilities(const table &value)

#### Operation¶

template<typename Descriptor>
logistic_regression::infer_result infer(const Descriptor &desc, const logistic_regression::infer_input &input)
Parameters
• desc – Logistic Regression algorithm descriptor logistic_regression::descriptor

• input – Input data for the inference operation

Preconditions
input.data.has_data == true
Postconditions
result.responses.column_count == 1
result.responses.row_count == input.data.row_count

## Examples¶

### oneAPI DPC++¶

Batch Processing: