-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add native log and entropy expression (#2952)
- Loading branch information
Showing
16 changed files
with
207 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::prelude::*; | ||
|
||
fn log<T: PolarsNumericType>(ca: &ChunkedArray<T>, base: f64) -> Float64Chunked { | ||
ca.cast_and_apply_in_place(|v: f64| v.log(base)) | ||
} | ||
|
||
impl Series { | ||
/// Compute the logarithm to a given base | ||
#[cfg_attr(docsrs, doc(cfg(feature = "log")))] | ||
pub fn log(&self, base: f64) -> Series { | ||
let s = self.to_physical_repr(); | ||
let s = s.as_ref(); | ||
|
||
use DataType::*; | ||
match s.dtype() { | ||
Int32 => log(s.i32().unwrap(), base).into_series(), | ||
Int64 => log(s.i64().unwrap(), base).into_series(), | ||
UInt32 => log(s.u32().unwrap(), base).into_series(), | ||
UInt64 => log(s.u64().unwrap(), base).into_series(), | ||
Float32 => s.f32().unwrap().apply(|v| v.log(base as f32)).into_series(), | ||
Float64 => s.f64().unwrap().apply(|v| v.log(base)).into_series(), | ||
_ => unimplemented!(), | ||
} | ||
} | ||
|
||
/// Compute the entropy as `-sum(pk * log(pk)`. | ||
/// where `pk` are discrete probabilities. | ||
#[cfg_attr(docsrs, doc(cfg(feature = "log")))] | ||
pub fn entropy(&self, base: f64) -> Option<f64> { | ||
let pk = self; | ||
let log_pk = pk.log(base); | ||
(pk * &log_pk).sum::<f64>().map(|v| -v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod list; | ||
#[cfg(feature = "rows")] | ||
mod pivot; | ||
|
||
use polars::prelude::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ features = [ | |
"ndarray", | ||
"series_from_anyvalue", | ||
"unique_counts", | ||
"log", | ||
] | ||
|
||
# [patch.crates-io] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,7 @@ Computations | |
Expr.pct_change | ||
Expr.skew | ||
Expr.kurtosis | ||
Expr.entropy | ||
Expr.sqrt | ||
Expr.sin | ||
Expr.cos | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters