kindly help, I have a syntax error in the project below.
import numpy as np
def calculate(list):
if len(list)!=9:
raise ValueError("List should have nine elements.")
list=np.array(list).reshape((3,3))
'mean':[(np.mean(list,axis=1)).tolist(), (np.mean(list,axis=0)).tolist(), np.mean(list)],
'variance':[(np.var(list,axis=1)).tolist(), (np.var(list,axis=0)).tolist(), np.var(list)],
'standard deviation':[(np.std(list,axis=1)).tolist(), (np.std(list,axis=0)).tolist(), np.std(list)],
'max':[(np.max(list,axis=1)).tolist(), (np.max(list,axis=0)).tolist(), np.max(list)],
'min':[(np.min(list,axis=1)).tolist(), (np.min(list,axis=0)).tolist(), np.min(list)],
'sum':[(np.sum(list,axis=1)).tolist(), (np.sum(list,axis=0)).tolist(), np.sum(list)]
return{
'mean': mean,
'variance': variance,
'standard deviation': std_dev,
'max': max,
'min': min,
'sum': sum
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
Sh0es
August 2, 2021, 7:20pm
3
danstanvannevins:
'mean':[(np.mean(list,axis=1)).tolist(), (np.mean(list,axis=0)).tolist(), np.mean(list)],
'variance':[(np.var(list,axis=1)).tolist(), (np.var(list,axis=0)).tolist(), np.var(list)],
'standard deviation':[(np.std(list,axis=1)).tolist(), (np.std(list,axis=0)).tolist(), np.std(list)],
'max':[(np.max(list,axis=1)).tolist(), (np.max(list,axis=0)).tolist(), np.max(list)],
'min':[(np.min(list,axis=1)).tolist(), (np.min(list,axis=0)).tolist(), np.min(list)],
'sum':[(np.sum(list,axis=1)).tolist(), (np.sum(list,axis=0)).tolist(), np.sum(list)]
This looks like the definition of a dictionary object, but I don’t see a preceding declaration.
danstanvannevins:
return{
'mean': mean,
'variance': variance,
'standard deviation': std_dev,
'max': max,
'min': min,
'sum': sum
}
I suppose you meant to put those variable names in place of the dictionary keys above.
Sh0es
August 2, 2021, 7:33pm
4
Personally I would write this:
return {
'mean':[(np.mean(list,axis=1)).tolist(), (np.mean(list,axis=0)).tolist(), np.mean(list)],
'variance':[(np.var(list,axis=1)).tolist(), (np.var(list,axis=0)).tolist(), np.var(list)],
'standard deviation':[(np.std(list,axis=1)).tolist(), (np.std(list,axis=0)).tolist(), np.std(list)],
'max':[(np.max(list,axis=1)).tolist(), (np.max(list,axis=0)).tolist(), np.max(list)],
'min':[(np.min(list,axis=1)).tolist(), (np.min(list,axis=0)).tolist(), np.min(list)],
'sum':[(np.sum(list,axis=1)).tolist(), (np.sum(list,axis=0)).tolist(), np.sum(list)]
}
1 Like
Jagaya
August 3, 2021, 11:16am
5
“list” is a Python-keyword and should never be overwritten.
The error itself is prett simple, you didn’t create an object, you just assigned a list to a nameless string containing “mean” - that just cannot work.
Meaning you never declared a variable mean
yet you assign it within the dictionairy.
Sh0es gave you the solution for this.
system
Closed
February 1, 2022, 11:17pm
6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.