Question about NumPy

In Numpy, when you call a function, the return type for that function is always an integer with a decimal, and no decimal place.
For example in,

list = [1,2,3,4,5]

np.mean would return 3. , and not 3.0. How would I change the np.mean call so that it does return 3.0?

I’d guess you may want to change the list to hold decimal values. But I’m the presence of the zero, by the definition of zero, would have no effect.

I just plucked the numbers into my Colab-notebook and it returned “3.0” so I can’t reproduce your problem.

1 Like

yea it is just for the certification test

really? Because in my Colab notebook, I have this code:

import numpy as np
list = [0, 1, 2, 3, 4, 5, 6, 7, 8]

def average(arr): #finalized
  vertavg0 = np.mean(arr[: , 0 ])
  vertavg1 = np.mean(arr[: , 1 ])
  vertavg2 = np.mean(arr[: , 2 ])
  vertavg = [vertavg0, vertavg1, vertavg2]
  print(np.mean(arr, axis=0)) # ****This print Statement*****
  horavg0 = np.mean(arr[0, :])
  horavg1 = np.mean(arr[1, :])
  horavg2 = np.mean(arr[2, :])
  horavg = [horavg0, horavg1, horavg2]
  linavg = np.mean(arr)
  return [vertavg, horavg, linavg]

def variance(arr): #finalized
  vertvar0 = np.var(arr[:, 0])
  vertvar1 = np.var(arr[:, 1])
  vertvar2 = np.var(arr[:, 2])
  vertvar = [vertvar0, vertvar1, vertvar2]
  horvar0 = np.var(arr[0, :])
  horvar1 = np.var(arr[1, :])
  horvar2 = np.var(arr[2, :])
  horvar = [horvar0, horvar1, horvar2]
  linvar = np.var(arr)
  return [vertvar, horvar, linvar]

def  std(arr): #finalized
  vertstd = [np.std(arr[:, 0]), np.std(arr[:, 1]), np.std(arr[:, 2])]
  horstd = [np.std(arr[0, :]), np.std(arr[1, :]), np.std(arr[2, :])]
  linstd = np.std(arr)
  return [vertstd, horstd, linstd]


def calculate(list):
  if len(list) < 9:
    raise ValueError("List must contain nine numbers.")
  top = np.asarray(list[:3])
  middle = np.asarray(list [3:6])
  bottom = np.asarray(list [6: 9])
  final = np.append([top],[middle],axis=0)
  final = np.append(final, [bottom], axis = 0)


And the print statement prints out this result:

[3. 4. 5.]

First: “list” is a Python keyword, don’t override it with some random values, because
Second: if you cast the thing to a list, it will be shown as a normal float (either by using the “list” keyword which you overrode OR by using the .tolist() from numpy

Also your first code-snipped does indeed show the zeroes, because it’s turned into a normal Python-float, whereas if you do it in a list, it’s kept as a numpy-object with different formatting.

Two additional tipps: First add some more text to your print statements for them to put out, so it’s easier to identify what they mean.
Second, either now or when you are finished, you can look more into numpy or look for a advanced solution of this, because Numpy offers a ton of commands that make this task a lot easier, like:

.reshape() to turn a list into a matrix of your liking
or arguments for .mean() to directly calculate it along different dimension, instead of having to call it 3 times


Thanks for all your help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.