I have a code that runs on numpy. I already use anaconda and open vscode from there
import numpy as np
def calculate(list):
if len(list) != 9:
raise ValueError("Input list must contain exactly 9 elements.")
return {
'mean': [axis1_mean, axis2_mean, flattened_mean],
'variance': [axis1_v, axis2_v, flattened_v],
'standard deviation': [axis1_std, axis2_std, flattened_std],
'max': [axis1_max, axis2_max, flattened_max],
'min': [axis1_min, axis2_min, flattened_min],
'sum': [axis1_sum, axis2_sum, flattened_sum]
}
array_2d = np.array(list).reshape(3,3)
axis1_mean = np.mean(array_2d, axis=0).tolist()
axis2_mean = np.mean(array_2d, axis=1).tolist()
flattened_mean = np.mean(array_2d)
#variance
axis1_v = np.var(array_2d, axis=0).tolist()
axis2_v = np.var(array_2d, axis=1).tolist()
flattened_v = np.var(array_2d)
#standard deviation
axis1_std = np.std(array_2d, axis=0).tolist()
axis2_std = np.std(array_2d, axis=1).tolist()
flattened_std= np.std(array_2d)
#max
axis1_max = np.max(array_2d, axis=0).tolist()
axis2_max = np.max(array_2d, axis=1).tolist()
flattened_max = np.max(array_2d)
#min
axis1_min = np.min(array_2d, axis=0).tolist()
axis2_min = np.min(array_2d, axis=1).tolist()
flattened_min = np.min(array_2d)
#sum
axis1_sum = np.sum(array_2d, axis=0).tolist()
axis2_sum = np.sum(array_2d, axis=1).tolist()
flattened_sum = np.sum(array_2d)
list = [9,1,5,3,3,3,2,9,0]
result = calculate(list)
print(result)
I found a few issues. First, the variables is not accessed if I indent it into the calculation (list). Second, when I run this code, it gives me this error line:
array_2d = np.array(list).reshape(3,3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 1 into shape (3,3)
When I use gitpod
to solve this, everything is fine and perfect. But not with the VS Code.
How do I fix this?
Especially the second question.
Hartmann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
first, the variable are not accessed if I indent it into calculate(list).
The code from array_2d = ...
to flattened_sum = ...
should indeed be inside the function definition. However, if you just indented that part of the code, it would never be executed, because it would come after the return
statement inside the function. The effect of a return statement is not only to return the specified value, but also to exit the function.
Also, you cannot return values you have not calculated yet, so clearly you want that return statement at the very end of the function body.
second, when I run this code, it gives me this error line array_2d = np.array(list).reshape(3,3) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: cannot reshape array of size 1 into shape (3,3)
When you run the code as given, at the point where the interpreter reaches the line array_2d = np.array(list).reshape(3,3)
, you have not defined your own list yet. So what is list
at that point? It is a built-in Python function! Hence, np.array(list)
is interpreted as a NumPy array that contains that function as its one and only item. This explains the error message.
Assigning another object to a Python keyword is usually a bad idea. In your case, your program would not be able to use the list()
function anymore, and error messages may become confusing, as you have seen. So to avoid overwriting the built-in function, replace all instances of list
in your code with a different name, one that is not a Python keyword.