Skip to content

Commit

Permalink
feat : LinearRegressor
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Dec 17, 2023
1 parent 5135c9b commit 012e150
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 8 deletions.
8 changes: 8 additions & 0 deletions docgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ fn main() {
let trait_name: &str = "TreeEnsembleClassifierTrait";
doc_trait(trait_path, doc_path, label);
doc_functions(trait_path, doc_path, trait_name, label);

// LINEAR REGRESSOR DOC
let trait_path = "src/operators/ml/linear/linear_regressor.cairo";
let doc_path = "docs/framework/operators/machine-learning/linear-regressor";
let label = "linear_regressor";
let trait_name: &str = "LinearRegressorTrait";
doc_trait(trait_path, doc_path, label);
doc_functions(trait_path, doc_path, trait_name, label);
}

fn doc_trait(trait_path: &str, doc_path: &str, label: &str) {
Expand Down
5 changes: 0 additions & 5 deletions docs/framework/numbers/complex-number/complex.to_polar.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@

//fn log2(self: T) -> T;

//fn log10(self: T) -> T;

# ComplexTrait::to_polar

```rust
Expand Down
1 change: 0 additions & 1 deletion docs/framework/numbers/fixed-point/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ use orion::numbers::fixed_point::core::FixedTrait;
| [`fp.sinh`](fp.sinh.md) | Returns the value of the hyperbolic sine of the fixed point number. |
| [`fp.tanh`](fp.tanh.md) | Returns the value of the hyperbolic tangent of the fixed point number. |
| [`fp.sign`](fp.sign.md) | Returns the element-wise indication of the sign of the input fixed point number. |
| [`fp.erf`](fp.erf.md) | The error function of the input fixed point number computed element-wise.|

### Arithmetic & Comparison operators

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Linear Regressor

`LinearRegressorTrait` provides a trait definition for linear regression problem.

```rust
use orion::operators::ml::LinearRegressorTrait;
```

### Data types

Orion supports currently only fixed point data types for `LinearRegressorTrait`.

| Data type | dtype |
| -------------------- | ------------------------------------------------------------- |
| Fixed point (signed) | `LinearRegressorTrait<FP8x23 \| FP16x16 \| FP64x64 \| FP32x32>` |


***

| function | description |
| --- | --- |
| [`linear_regressor.predict`](linear_regressor.predict.md) | Performs the generalized linear regression evaluation. |

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# LinearRegressorTrait::predict

```rust
fn predict(ref self: LinearRegressor<T>, X: Tensor<T>) -> Tensor<T>;
```

Linear Regressor. Performs the generalized linear regression evaluation.

## Args

* `self`: LinearRegressor<T> - A LinearRegressor object.
* `X`: Input 2D tensor.

## Returns

* Tensor<T> containing the generalized linear regression evaluation of the input X.

## Type Constraints

`LinearRegressor` and `X` must be fixed points

## Examples

```rust
use orion::operators::tensor::{Tensor, TensorTrait, FP16x16Tensor, FP16x16TensorAdd};
use orion::operators::ml::linear::linear_regressor::{
LinearRegressorTrait, POST_TRANSFORM, LinearRegressor
};
use orion::numbers::{FP16x16, FixedTrait};

fn example_linear_regressor() -> Tensor<FP16x16> {

let mut X: Tensor<FP16x16> = TensorTrait::new(
array![3, 2].span(),
array![
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 65536, sign: false },
FP16x16 { mag: 131072, sign: false },
FP16x16 { mag: 196608, sign: false },
FP16x16 { mag: 262144, sign: false },
FP16x16 { mag: 327680, sign: false },
]
.span()
);

let coefficients: Span<FP16x16> = array![
FP16x16 { mag: 19661, sign: false },
FP16x16 { mag: 50463, sign: true },

]
.span();

let intercepts: Span<FP16x16> = array![
FP16x16 { mag: 32768, sign: false },

]
.span();
let intercepts = Option::Some(intercepts);

let target : usize = 1;
let post_transform = POST_TRANSFORM::NONE;

let mut regressor: LinearRegressor<FP16x16> = LinearRegressor {
coefficients,
intercepts,
target,
post_transform
};

let scores = LinearRegressorTrait::predict(ref regressor, X);

scores
}

>>>
[[-0.27], [-1.21], [-2.15]]



fn example_linear_regressor_2() -> Tensor<FP16x16> {

let mut X: Tensor<FP16x16> = TensorTrait::new(
array![3, 2].span(),
array![
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 65536, sign: false },
FP16x16 { mag: 131072, sign: false },
FP16x16 { mag: 196608, sign: false },
FP16x16 { mag: 262144, sign: false },
FP16x16 { mag: 327680, sign: false },
]
.span()
);

let coefficients: Span<FP16x16> = array![
FP16x16 { mag: 19661, sign: false },
FP16x16 { mag: 50463, sign: true },
FP16x16 { mag: 19661, sign: false },
FP16x16 { mag: 50463, sign: true },

]
.span();

let intercepts: Span<FP16x16> = array![
FP16x16 { mag: 32768, sign: false },
FP16x16 { mag: 45875, sign: false },

]
.span();
let intercepts = Option::Some(intercepts);

let target = 2;
let post_transform = POST_TRANSFORM::NONE;

let mut regressor: LinearRegressor<FP16x16> = LinearRegressor {
coefficients,
intercepts,
target,
post_transform
};

let scores = LinearRegressorTrait::predict(ref regressor, X);

scores
}

>>>
[[-0.27, -0.07], [-1.21, -1.01], [-2.15, -1.95]]
```

3 changes: 3 additions & 0 deletions src/operators/ml.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod tree_ensemble;
mod linear;

use orion::operators::ml::tree_ensemble::core::{
TreeEnsemble, TreeEnsembleAttributes, TreeEnsembleImpl, NODE_MODES
};
use orion::operators::ml::tree_ensemble::tree_ensemble_classifier::{
TreeEnsembleClassifier, TreeEnsembleClassifierImpl, TreeEnsembleClassifierTrait, POST_TRANSFORM
};

use orion::operators::ml::linear::linear_regressor::{LinearRegressorTrait, LinearRegressorImpl, LinearRegressor};
1 change: 1 addition & 0 deletions src/operators/ml/linear.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod linear_regressor;
Loading

0 comments on commit 012e150

Please sign in to comment.