Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Hi people, so far can get the values, just want to get done the final layout as requested. The dictionary doesnt show the key values plus the lists are shown in vertical and i want to set them as the challenge requires (each axes and flattened outputs horizontal and separated by a comma). Thanks very much

Your code so far
import numpy as np

a = [int(x) for x in input('Intro 9 int separated by a space: ').split()]
if len(a) < 9 or len(a) > 9:
print (‘List must contain nine numbers’)
else: # can be removed
#print(a) #
arr = numpy.asarray(a) #turn list into array/matrix
#print(arr) # can be removed
arr = arr.reshape((3, 3)) # np.arange(9).reshape((3, 3)) for generating generic array from 0 to 8
#print(arr)
m1 = print(np.mean(arr, axis=0))
m2 = print(np.mean(arr, axis=1))
m3 = print(np.mean(arr.flatten()))
thisdict = dict[(
(‘mean’, m1, m2, m3),
(‘variance’, [print(np.var(arr, axis=0)), print(np.var(arr, axis=1)), print(np.var(arr.flatten()))]),
(‘standard deviation’, [print(numpy. std(arr, axis=0)), print(numpy. std(arr, axis=1)), print(numpy. std(arr.flatten()))]),
(‘max’, [print(np.max(arr, axis=0)), print(np.max(arr, axis=1)), print(np.max(arr.flatten()))]),
(‘min’, [print(np.min(arr, axis=0)), print(np.min(arr, axis=1)), print(np.min(arr.flatten()))]),
(‘sum’, [print(np.sum(arr, axis=0)), print(np.sum(arr, axis=1)), print(np.sum(arr.flatten()))]),
)]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Link to the challenge:

Can you link to the replit?

Problem looks like it’s with all the print statements:

'variance', [print(np.var(arr, axis=0)), print(np.var(arr, axis=1)), print(np.var(arr.flatten()))]),

Try taking those out.

I don’t think the tests will pass with this input, as well, you just need to call the function and pass the 9 digit array as an argument

a = [int(x) for x in input('[Intro 9 int separated by a space: ').split()]

Here’s how you enter test data from main.py:

#print(mean_var_std.calculate([0,1,2,3,4,5,6,7,8]))
# print(mean_var_std.calculate([2,6,2,8,4,0,1,5,7]))

The boilerplate has a function that passes an array as an argument, but you’ve removed that.


def calculate(list):

    #Your code goes here


    return calculations

Thanks for te quick answer pkdvalis

im newby and dont know what you mean with linking to the replit…

I have been looking into it and worked almost exactly as requested

import numpy as np

def calculate(a):  
    if len(a) == 9:
        arr = np.asarray(a) #turn list into array/matrix
        arr = arr.reshape((3, 3)) # np.arange(9).reshape((3, 3)) for generating generic array from 0 to 8        
        meaxis1 = np.mean(arr[ :,0]), np.mean(arr[ :,1]), np.mean(arr[ :,2])
        meaxis2 = np.mean(arr[0]), np.mean(arr[1]), np.mean(arr[2])
        meflat = np.mean([arr])
        varaxis1 = np.var(arr[ :,0]), np.var(arr[ :,1]), np.var(arr[ :,2])
        varaxis2 = np.var(arr[0]), np.var(arr[1]), np.var(arr[2])
        varflat = np.var([arr])
        stdaxis1 = np.std(arr[ :,0]), np.std(arr[ :,1]), np.std(arr[ :,2])
        stdaxis2 = np.std(arr[0]), np.std(arr[1]), np.std(arr[2])
        stdflat = np.std([arr])
        maxaxis1 = np.max(arr[ :,0]), np.max(arr[ :,1]), np.max(arr[ :,2])
        maxaxis2 = np.max(arr[0]), np.max(arr[1]), np.max(arr[2])
        maxflat = np.max([arr])
        minaxis1 = np.min(arr[ :,0]), np.min(arr[ :,1]), np.min(arr[ :,2])
        minaxis2 = np.min(arr[0]), np.min(arr[1]), np.min(arr[2])
        minflat = np.min([arr])
        sumaxis1 = np.sum(arr[ :,0]), np.sum(arr[ :,1]), np.sum(arr[ :,2])
        sumaxis2 = np.sum(arr[0]), np.sum(arr[1]), np.sum(arr[2])
        sumflat = np.sum([arr])
        dict = {
            "mean": [[meaxis1], [meaxis2], meflat],
            "variance": [[varaxis1], [varaxis2], varflat],
            "standard deviation": [[stdaxis1], [stdaxis2], stdflat],
            "max": [[maxaxis1], [maxaxis2], maxflat],
            "min": [[minaxis1], [minaxis2], minflat],
            "sum": [[sumaxis1], [sumaxis2], sumflat]
        }
        return dict
    else:              
        print ('List must contain nine numbers')

a = [int(x) for x in input('[Intro 9 int separated by a space: ').split()]
calculate(a)

Just the format is slightly different

  • requested output:
    ‘mean’: [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0]
  • my output:
    ‘mean’: [[(3.0, 4.0, 5.0)], [(1.0, 4.0, 7.0)], 4.0]

There are some extra brackets i wanna get rid of and sure there are many other things to express better, please let me know!

1 Like

The link is Mean-Variance-Standard Deviation Calculator - Replit
As well is working on Jupiter notebook but not on replit…

1 Like

I also work on a notebook (Google Colab) so much easier to test and write!

It looks like some of the variables are tuples () instead of Lists []

Print out meaxis1 and see what it is. When you assign meaxis1 into the dict, make it into a list()

“The values in the returned dictionary should be lists and not Numpy arrays.”

1 Like

Correct, they are assigned as tuples by default, just used the list() and match exactly now, thanks!

1 Like

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