Here is my module for statistics:

import numpy as np
import scipy.stats as st    

class Statistics():

    def sterm_and_leaf(self,data):
        for i in data:
            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



    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):
        var=[(i-mean)**2 for i in data]
        return round(sum(var)/float(len(var)-1),4)

    def pearson_correlation_coefficient(self,X,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

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

        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.

  • 2
    \$\begingroup\$ What's the expected format of data? Without this it's difficult to offer a proper review. \$\endgroup\$
    – IEatBagels
    Commented Aug 14, 2019 at 13:18
  • 2
    \$\begingroup\$ In response to IEatBagel's question, it appears that data typically is a list of numbers. @Белякова Анастасия please edit your post and add the test code for context. Code Review has a higher limit on code that can be embedded in a post. \$\endgroup\$ Commented Aug 14, 2019 at 15:57
  • \$\begingroup\$ Indeed, data here is list of numbers \$\endgroup\$ Commented Aug 15, 2019 at 14:46


Browse other questions tagged or ask your own question.