How to extract Tp,Tn,Fp and Fn from PyCaret - confusion-matrix

Can i extract Tp,Tn,Fp and Fn as numbers from confusion matrix with PyCaret or they are only given as plot?

Related

Is there a way to get Euler angle orders in nalgebra or another Rust library?

I'm trying to convert raw quaternion values to Euler angles in order zyx. I'm trying to use nalgebra rotations to convert, but the in-built conversion only gives angles in order xyz. Is there a way to convert from quat to zyx or from xyz to zyx? Or an alternative library that can do the same conversion?
Here's the function I'm currently using for conversion:
pub fn raw_quaternion_to_euler(w: f32, x: f32, y: f32, z: f32) -> (f32, f32, f32) {
let unit_quat = UnitQuaternion::from_quaternion(nalgebra::Quaternion::from_vector([w, x, y, z]));
unit_quat.euler_angles()
}
I managed to work out a way of doing this using a different library from nalgebra called quaternion-core. It was really simple to use and did exactly what I was asking for in this question.

How to use Bezier curve interpolation?

I want to interpolate values between multiple points with Bezier curve interpolation using the splines crate.
My current implementation looks like this:
let mut output_buffer: Vec<f32> = vec![ 0.0; buffer.len() ];
let mut points: Vec<Key<f32, f32>> = Vec::new();
for val in pos_index.iter() {
let x = val.0;
let y = val.1;
points.push(Key::new(x, y, Interpolation::Bezier( 0.5 )));
}
let spline = Spline::from_vec(points);
for i in 0..output_buffer.len() {
let v = spline.clamped_sample(i as f32).unwrap_or(0.0);
output_buffer[i] = v;
}
output_buffer
But I do not known what value to put into Interpolation::Bezier( 'value' ).
The documentation does not help me neither and I cannot find any examples that use Bezier interpolation.
One important thing to note is that the points or keys are not evenly spaced out, so the distance between them is much higher on the first values that are closer to zero.
At line #170 of spline.rs, it shows V's values of Interpolation::Bezier(V) of cp0 (cp stands for control point) and cp1 are passed to the function V::cubic_bezier as the third and fourth parameters.
Here is the documentation of the function cubic_bezier.
fn cubic_bezier(t: T, a: Self, u: Self, v: Self, b: Self) -> Self
Cubic Bézier interpolation.
a is the first point; b is the second point; u is the output tangent of a to the curve and v is the input tangent of b to the curve.
We can see u and v stand for tangents and also, from the function signature, we can see their type is Self, the type that implements the Interpolate trait. The section Implementations on Foreign Types of Interpolate documentation lists the types that implements the trait. Here are some as examples:
impl Interpolate<f32> for Vector1<f32>
impl Interpolate<f32> for Vector2<f32>
impl Interpolate<f64> for Vector1<f64>
impl Interpolate<f64> for Vector2<f64>
Hope this is helpful.

implement a trait for Vec<T> for various numeric T's

I want to have a trait TopK as follows:
pub trait TopK{
pub fn top_k(&self, k : size) -> Result<Vec<usize>, io::Error>;
}
I would like to implement TopK on Vec<T> such that the behaviour would depend upon T. I have one implementation over all non-float numbers and another one over all float numbers.
Therefore I would like to have something as below:
impl<T> TopK for Vec<T> where T : num_traits::PrimInt + Copy {
//Implementation where T is any non-floating point number
}
impl<T> TopK for Vec<T> where T : num_traits::Float + Copy{
//Implementation where T is any floating point number
}
This is because floats are only partially ordered and so for floats, I have to make a new struct for all non-NaN floating point numbers and use them inside a binary-heap for TopK computation.
However, trying to do this produces conflicting definition error. What is the design pattern to be followed for this problem ?

How do I create a macro that transforms inputs into a tuple?

I want to avoid creating many numbered functions and duplicated code if possible. I'm writing a program that is parsing a config file containing lines like the following and I want to simplify my logic for parsing it with some helper functions.
I would like advice on the idiomatic Rust way of approaching this to avoid code duplication while keeping it readable. My best guess is if I could use a macro that could somehow convert the input into a block that results in a tuple but don't know how to write that while including the iteration and transformation steps.
Example Input
attribute-name
1 2
other-attribute
3 4 5
Current parsing implementation
/// Splits `s` into two values, using `pattern` to find the split. If not enough values
/// are present, `missing_err` is returned.
/// It then `transform`s each entry, returning the result as a tuple
fn split_str_into_2<'a, T, E>(
s: &'a str,
pattern: &str,
transform: &dyn Fn(&str) -> T,
missing_err: &E,
) -> Result<(T, T), E> where E: Copy {
let mut split = s.splitn(2, pattern);
Ok((
transform(split.next().ok_or_else(|| *missing_err)?),
transform(split.next().ok_or_else(|| *missing_err)?),
))
}
/// Same as above but parses into a tuple of 3
fn split_str_into_3<'a, T, E>( ...
Calling Code
let (width, height) = split_str_into_2(
input_line, " ", |entry| i32::from_str_radix(entry, 10), &MyError::new("Missing number entry"))?;
I do not know your exact use, but one possibility if you want to collect an iterator into a tuple would be Itertools::collect_tuple.
This, for now is implemented for tuples up to length 4. If you need more elements, you could try adapting the approach taken in itertools or filing an issue/PR on the project.

How do I write the transpose function example from Rust by Example?

I'm following the Rust by Example tutorial and am on the second part of the Tuples activity which is to add a transpose function using the reverse function as a template. This will accept a matrix as an argument and return a matrix in which two elements have been swapped. For example:
println!("Matrix:\n{}", matrix);
println!("Transpose:\n{}", transpose(matrix));
Expected results:
Input Matrix:
( 1.1 1.2 2.1 2.2 )
Transposed output:
( 1.1 2.1 1.2 2.2 )
I can't find the right code, here is what I'm trying:
// this is defined in the tutorial
#[derive(Debug)]
struct Matrix(f32, f32, f32, f32);
// this is my attempt that does not compile
fn transpose(maat: Matrix) -> (Matrix) {
let matrix = maat;
(matrix.0, matrix.2, matrix.1, matrix.3)
}
I don't want to give you the full solution because i would do you a disservice if you're learning Rust.
There's a key ingredient that you're missing at this point of the tutorial. Not your fault.
Matrix is a "tuple struct" (also sometimes called a newtype) and it's covered in a later section of Rust by example.
If you want to peek ahead, in the section on structs you'll learn the two pieces you're missing.
Piece one: struct Matrix(f32, f32, f32, f32); as defined in the tutorial can be destructured in a similar way as the simple tuple.
If you have a let matrix = Matrix(1.1, 1.2, 2.1, 2.2); you can do this to create names for its individual elements:
let Matrix(r1c1, r2c2, r2c1, r2c2) = matrix
What you did (matrix.0, matrix.1...) works too, though...
Piece two. When you want to create a new instance of Matrix, you do Matrix(1.1, 1.2, 2.1, 2.2). From your attempt at writing transpose you're trying to return a tuple, but a tuple struct like Matrix is a different, incompatible type (that's why it's also called a "newtype")
Using reverse as the example says and rewriting the reverse function to accept f32,
fn reverse(pair: (f32, f32)) -> (f32, f32) {
let (a, b) = pair;
(b, a)
}
fn transpose(mat: Matrix) -> Matrix {
let (a, b) = reverse((mat.1, mat.2));
Matrix(mat.0, a, b, mat.3)
}
You need to do three things here:
Tell the Display format how to structure the output so that it divides the Matrix into two rows of equal length.
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "( {} {} )\n( {} {} )", self.0, self.1, self.2, self.3)
}
}
Write the transpose function that will return the matrix in transposed order.
fn transpose(mat: Matrix) -> Matrix {
return Matrix(mat.0, mat.2, mat.1, mat.3);
}
call it inside println
println!("Transpose:\n{}", transpose(matrix));

Resources