Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy sum of squares for matrix

Tags:

python

numpy

I do have a matrix with observations in rows (measurements at differnt pH) with data points as columns (concentration over time). So one row consists of differnt data points for one pH.

I do want to fit an ODE to the data. So i defined a cost function and would like to calculate the sum of squares for all observatoins. Taking the sum of sqares for this matrix should work like:

res = y - yhat                        # calculate residuals
ssq = np.diag(np.dot(res.T,res))      # sum over the diagonal

is that correct ?

like image 474
Moritz Avatar asked Jul 17 '14 14:07

Moritz


People also ask

How to square a matrix using NumPy?

The most pythonic way would be to use ** 2 which also square my array. import numpy as np my_array = np.array ( [1, 2, 3, 4]) squared_array = my_array ** 2 print (f"My array is equal to {my_array}") print (f"Squared array is equal to {squared_array}") These are 3 different ways to square a matrix using Numpy.

How to use NumPy sum of squares in Python?

In this section, we will learn about the python numpy sum of squares. Numpy. square () function helps the user to calculate the square value of each element in the array. This function is used to sum all elements, the sum of each row, and the sum of each column of a given array. In this section, we will learn about the python NumPy sum 3d array.

How to add all the terms of a NumPy matrix?

In this program, we will add all the terms of a numpy matrix using the sum () function in the numpy library. We will first create a random numpy matrix and then, we will obtain the sum of all the elements. Step 1: Import numpy. Step 2: Create a random m×n matrix using the random () function.

How do you find the sum of a matrix in Python?

matrix.sum(axis=None, dtype=None, out=None) [source] ¶. Returns the sum of the matrix elements, along the given axis. Refer to numpy.sum for full documentation. See also. numpy.sum. Notes. This is the same as ndarray.sum, except that where an ndarray would be returned, a matrix object is returned instead. Examples.


2 Answers

If you would take the sum of the last array it would be correct. But it's also unnecessarily complex (because the off-diagonal elements are also calculated with np.dot) Faster is:

ssq = np.sum(res**2)

If you want the ssd for each experiment, you can do:

ssq = np.sum(res**2, axis=1)
like image 173
Dave Avatar answered Oct 14 '22 10:10

Dave


Performance comparison follow-up, that fastest method I've found is to do the math directly:

res[:, 0]**2 + res[:, 1]**2 + res[:, 2]**2

enter image description here

import numpy as np
import perfplot

perfplot.live(
    setup=lambda n: np.random.randn(n, 3),
    kernels=[
        lambda a: a[:, 0]**2 + a[:, 1]**2 + a[:, 2]**2,
        lambda a: np.sum(a**2, axis=1),
        lambda a: np.sum(np.square(a), axis=1),
    ],
    labels=["a[:, 0]**2 + a[:, 1]**2 + a[:, 2]**2",
            "np.sum(a**2, axis=1)",
            "np.sum(np.square(a), axis=1)"],
    n_range=[2 ** k for k in range(25)],
    xlabel="len(a)",
)

Shout out to https://github.com/nschloe/perfplot

like image 32
Ryan Goss Avatar answered Oct 14 '22 10:10

Ryan Goss