2
\$\begingroup\$

Here is my module for statistics:

import numpy as np
import scipy.stats as st    

class Statistics():

    def sterm_and_leaf(self,data):
        graph={}
        for i in data:
            x=str(i)
            if len(x)>1:
                if int(x[0]) in graph.keys(): graph[int(x[0])].append(int(x[1:]))
                else : graph[int(x[0])]=[int(x[1:])]
            elif len(x)==1:
                if 0 in graph: graph[0].append(int(x))
                else : graph[0]=[int(x)]

        for v in graph.values():  v.sort()

        for k in sorted(graph.keys(),reverse=True):
            print(k,':',' '.join([str(i) for i in graph[k]]))

        return graph

    def hystogram(self,data):

        import matplotlib.pyplot as plt
        plt.hist(data)

        plt.xlabel("Value")
        plt.ylabel("Frequency")

        plt.show()


    def trimean(self,data):
        from numpy import percentile
        return (percentile(data,25)+2*percentile(data,50)+percentile(data,75))/4


    def trimmed_mean(self,data,percent):
        sorted_data = sorted(data) #uncomment to use plain python sorted
        n = len(sorted_data)
        outliers = n*percent//100 #may want some rounding logic if n is small
        trimmed_data = sorted_data[outliers: n-outliers]
        #from numpy import percentile
        #if percent<50: data_trimmed=[i for i in data if i>percentile(data,percent)and i<percentile(data,100-percent)]
        #else: data_trimmed=[i for i in data if i<percentile(data,percent)and i>percentile(data,100-percent)]
        return sum(trimmed_data)/float(len(trimmed_data))

    def variance(self,data):
        mean=sum(data)/float(len(data))
        var=[(i-mean)**2 for i in data]
        return round(sum(var)/float(len(var)-1),4)

    def pearson_correlation_coefficient(self,X,Y):
        mean_x=sum(X)/len(X)
        mean_y=sum(Y)/len(Y)
        x=[(i-mean_x) for i in X]
        y=[j-mean_y for j in Y]
        z=[x[i]*y[i] for i in range(len(x))]
        return round(sum(z)/((sum([k**2 for k in x])*sum([v**2for v in y]))**0.5),3)

    def confidence_interval(self,data,alpha):
        import numpy as np
        import scipy.stats as st
        g=st.t.interval(alpha, len(data) - 1, loc=np.mean(data), scale=st.sem(data))
        return([round(i) for i in g])

    def scatter_plot(self,x,y):
        import matplotlib.pyplot as plt
        plt.scatter(x,y)
        plt.show()

    def get_points_from_scatter(self,x,y,n):
        from pylab import ginput,plot,scatter

        scatter(x,y)
        z = ginput(n)

        return z

Please look for full version on Github Any hint on improving the code? Module was created for statistical analysis of data: - finding mean, median, mode, trimmed mean - finding range, variance, standard deviation, correlation coefficient - various visual representations of data: histogram, stern-and-leaf display, scatter plot.

\$\endgroup\$
  • 2
    \$\begingroup\$ Questions must include the code to be reviewed. Links to code hosted on third-party sites are permissible, but the most relevant excerpts must be embedded in the question itself. Please paste your code here instead of giving us a link to github \$\endgroup\$ – яүυк Jul 25 '17 at 18:28
  • \$\begingroup\$ I inserted some code. The matter is that module is big \$\endgroup\$ – Белякова Анастасия Jul 26 '17 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.