简体   繁体   中英

How to calculate Sum of Squares of a list in python?

I have a list like this;

A = [111,222,333,444]

I want to calculate the Sum of Squares of this list using python

so far I tried this;

value = ((sum([i**2 for i in A]))-(sum(A)**2)/len(A))

but I am not sure if this is correct. Is there a way to do this in numpy?

Yes, you can compute the sum of squares of a list with numpy:

>>> import numpy as np
>>> A = np.array([111, 222, 333, 444])
>>> np.sum(A**2)
369630

This is equivalent to the expression sum([i**2 for i in A]) in your code.

It looks like you have some additional operations after that. I'm not exactly sure what quantity you're trying to compute, but there are idiomatic ways to compute things like variance, MSE, etc. in numpy as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM