np.average takes an optional weight parameter. If it is not supplied they are equivalent. Take a look at the source code: Mean, Average
np.mean:
try:
mean = a.mean
except AttributeError:
return _wrapit(a, 'mean', axis, dtype, out)
return mean(axis, dtype, out)
np.average:
...
if weights is None :
avg = a.mean(axis)
scl = avg.dtype.type(a.size/avg.size)
else:
#code that does weighted mean here
if returned: #returned is another optional argument
scl = np.multiply(avg, 0) + scl
return avg, scl
else:
return avg
...
Answer from Hammer on Stack OverflowTypeerror on "np.mean" function, any ways on how to fix?
Alternative to np.mean() with better performance?
find mean of 2 numpy arrays without using the 0 values
np.average takes an optional weight parameter. If it is not supplied they are equivalent. Take a look at the source code: Mean, Average
np.mean:
try:
mean = a.mean
except AttributeError:
return _wrapit(a, 'mean', axis, dtype, out)
return mean(axis, dtype, out)
np.average:
...
if weights is None :
avg = a.mean(axis)
scl = avg.dtype.type(a.size/avg.size)
else:
#code that does weighted mean here
if returned: #returned is another optional argument
scl = np.multiply(avg, 0) + scl
return avg, scl
else:
return avg
...
np.mean always computes an arithmetic mean, and has some additional options for input and output (e.g. what datatypes to use, where to place the result).
np.average can compute a weighted average if the weights parameter is supplied.
So I'm currently working on a linear regression algorithm and whilst writing the code for it I encountered a typeerror on the "np.mean" function. (made in jupyter nb, so if this is related with updates or something, let me know.)
Here's the reproducible error code:
# Mean X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
# Total number of values
m = len(X)
# Using the formula to calculate b1 and b0
numer = 0
denom = 0
for i in range(m):
numer +- (x[i] - mean_x) * (Y[i] - mean_y)
denom +- (X[i] - mean_x) ** 2
b1 = numer/denom
b0 = mean_y - (b1 * mean_x)
# Print coefficients
print(b1, b0)
Here's the error screenshot:
https://imgur.com/P97plv3
P.S, I'm a complete newbie, so if I'm looking over something completely obvious, feel free to criticize me.