Read data coming from "np.linalg.lstsq" function
Matteo-0 opened this issue · 6 comments
Hello,
I am using numpy in unity with C# because I need to use "np.linalg.lstsq".
Now when I use this function I do not know how to read the data returned from that function in unity.
I did like the following:
(C, residules, rank, singval) = np.linalg.lstsq(A,b);
Where variables are defined as following:
var C = np.array(new double[] {1,1,1,1});
var residules = np.array(new double[] {});
var singval = np.array(new double[] {});
private int rank;
The error that I get is the following:
Cannot implicitly convert type 'Numpy.NDarray' to 'Numpy.NDarray'. An explicit conversion exists (are you missing a cast?)
What do I have to do?
can you give me a simple python listing how to use that function? I'll convert it to a c# unittest and make sure it works.
You mean something like what is in this link?
https://numpy.org/devdocs/reference/generated/numpy.linalg.lstsq.html
I can directly use that, thanks
Really, thanks for your help, I really need that, Thank you so much.
Good news, it works as expected
[TestMethod]
public void IssueByMatteo_0()
{
//>>> x = np.array([0, 1, 2, 3])
//>>> y = np.array([-1, 0.2, 0.9, 2.1])
//>>> A = np.vstack([x, np.ones(len(x))]).T
//>>> A
//array([[0., 1.],
// [1., 1.],
// [2., 1.],
// [3., 1.]])
//>>> np.linalg.lstsq(A, y, rcond = None)
//(array([1. , -0.95]), array([0.05]), 2, array([4.10003045, 1.09075677]))
var x= np.array(new[] { 0, 1, 2, 3 });
var y= np.array(new[] { -1, 0.2, 0.9, 2.1 });
var A = np.vstack(x, np.ones(x.len)).T;
Assert.AreEqual("array([[0., 1.],\n [1., 1.],\n [2., 1.],\n [3., 1.]])", A.repr);
var tuple=np.linalg.lstsq(A, y, null);
Assert.AreEqual("array([ 1. , -0.95])", tuple.Item1.repr);
Assert.AreEqual("array([0.05])", tuple.Item2.repr);
Assert.AreEqual(2, tuple.Item3);
Assert.AreEqual("array([4.10003045, 1.09075677])", tuple.Item4.repr);
}
So In my code I have to substitute this line:
(C, residules, rank, singval) = np.linalg.lstsq(A,b);
with:
var tuple=np.linalg.lstsq(A, b, null);
And then to see the values I can type:
print(tuple.Item1.repr);
I tried doing like what I just described and it worked!
You are amazing, thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!