0

I was asking myself if it is possible to turn the output of a class into a np.array within the class itself. I created the following class:

class stats:

    def __init__( self, x ):
        self.age = x[:,0]
        self.education = x[:,1]
        self.married = x[:,2]
        self.nodegree = x[:,3]
        self.RE75 = x[:,4]
        self.RE78 = x[:,5]

    def Vector( self ):
        age = [np.mean(self.age), st.stdev(self.age)]
        education = [np.mean(self.education), st.stdev(self.education)]
        married = [np.mean(self.married), st.stdev(self.married)]
        nodegree = [np.mean(self.nodegree), st.stdev(self.nodegree)]
        RE75 = [np.mean(self.RE75), st.stdev(self.RE75)]
        RE78 = [np.mean(self.RE78), st.stdev(self.RE78)]
        return [age, education, married, nodegree, RE75, RE78]

results1 is a numpy.ndarray of shape 156x6. I basically want to compute the mean as well as standard deviation for each column of results1 using a class. I use numpy to compute the mean and statistics for the std. When I am printing the output I get the following:

results1_stats = stats(results1)
print(results1_stats.Vector())

Output:

[[25.98076923076923, 7.299554695959556], [10.314102564102564, 2.0597666237347005], [0.1858974358974359, 0.39027677820527085], [0.7243589743589743, 0.448275807219502], [1490.7220884615383, 3296.5535502409775], [6136.320646794872, 8143.4659725229685]]

Apparently, the class is working as wanted (although there is probablly a more efficent way to code this up). The problem is, that I would lilke to get the output in a np.array of shape 6x2 (or transposed) directly from the class itself. However, since I just began using classes I don't know if that is even possible. Any help is appreciated :) Thank you!

Mcgroger
  • 67
  • 6
  • 2
    What exactly do you mean about wanting to get the output "directly from the class itself"? – martineau Dec 11 '19 at 11:00
  • @martineau - Instead of getting a list for mean and std of every column, I would like to get an array where each row (or column if transposed) includes the mean & std of every column from results1 – Mcgroger Dec 11 '19 at 11:03
  • 2
    @Mcgroger, in python lists are considered as arrays. Your vector function does return that list in the format you are expecting (6x2). Do you mean np.array instead of a list? perhaps, you can add expected output section in the question? – Rithin Chalumuri Dec 11 '19 at 11:13
  • @Rithin Chalumuri, Yes I meant np.array. Sorry for the confusion! I have updated the question. – Mcgroger Dec 11 '19 at 11:28

1 Answers1

0

You can construct an numpy array using np.array(your_list_sequence). Additionally, you can use list comprehension to convert list of lists to numpy array. More info here.

Try this:

def get_stats(results):
    return np.array([np.array([np.mean(results[:, column]), st.stdev(results[:, column])]) for column in range(6)])

your_new_np_array = get_status(results)

Although, if you want only stats array, having a function for this instead of class would be better and simpler. But, you can easily include that method in your class and get back your expected result.

Rithin Chalumuri
  • 1,739
  • 7
  • 19