def getRate(aUrl): irUrl = mainUrl + aUrl i = 1 yes = 0 no = 0 yesarr = [] try: while True: #traveling pages pageSoup = BeautifulSoup(urlopen(irUrl+"&page="+str(i)), 'html.parser', from_encoding='cp932') pageTables = pageSoup.body.div.div.find_all('table') pageTable = pageTables[len(pageTables)-1].find_all('tr') # kinda dangerous part. check whether it's players' data table or not if len(pageTable[0].find_all('th'))!=17: break for j in range(1, len(pageTable),2): dataRow = pageTable[j] commentRow = pageTable[j+1] #check black list playerID = re.search('[0-9]+',dataRow.a['href']).group(0) if playerID in blackList: continue #replace fullwidth(Em-size) characters with halfwidth characters comment = unicodedata.normalize("NFKC", commentRow.td.string or "") checkVal = checkComment(comment) if checkVal[1]==-1: no = no+1 elif checkVal[1]>0: yes = yes+1 yesarr.append(checkVal) i = i+1 med = -1 if len(yesarr): yesarr = sorted(yesarr) temparr = [] resarr = [] for i in range(0, len(yesarr)): if i>0 and yesarr[i][0] > yesarr[i-1][0]: med = statistics.median_high(temparr) if len(resarr)==0 or med != resarr[-1][1]: resarr.append( (yesarr[i-1][0], med)) temparr.append(yesarr[i][1]) med = statistics.median_high(temparr) if len(resarr)==0 or med != resarr[-1][1]: resarr.append( (yesarr[-1][0], med)) return (yes, no, med, resarr) except: print("Failed to load the IR page: " + aUrl)
def get_overall_stats(results_dict): average = statistics.mean(results_dict) median = statistics.median(results_dict) median_low = statistics.median_low(results_dict) median_high = statistics.median_high(results_dict) mode = calc_mode(results_dict) return {'avg': average, 'med': median, 'medlo': median_low, 'medhi': median_high, 'mode': mode}
def stats_helper(list): """ https://docs.python.org/3/library/statistics.html#statistics.pvariance :param list: :return: """ mean = statistics.mean(list) mode = None try: mode = statistics.mode(list) except statistics.StatisticsError: # no unique mode pass return { 'mean': mean, 'variance': statistics.pvariance(list, mu=mean), 'standard_deviation': statistics.pstdev(list, mu=mean), 'median': statistics.median(list), 'median_low': statistics.median_low(list), 'median_high': statistics.median_high(list), 'median_grouped': statistics.median_grouped(list), 'mode': mode }
def __init__(self, values): values = [eval(i) for i in values] super().__init__(values) self.min = min(values) self.max = max(values) self.mean = Decimal(mean(values)).quantize(Decimal('.00000')) self.median_low = median_low(values) self.median = median(values) self.median_high = median_high(values)
def web_dataset_reader(url): ''' :param url: Input a web address. :return: This algorithm returns a dataseet and its descriptive statistics. Example: >>> url='http://robjhyndman.com/tsdldata/ecology1/hopedale.dat' >>> import web_dataset_reader_program as wbd >>> print(wbd.web_dataset_reader(url)) >>> . . . ''' from urllib import request as ur import web_skip_header_program as wh import statistics as st highvalue=0 count=0 with ur.urlopen(url) as webpage: first_data=wh.webpage_skip_head(webpage) first_data=first_data.split() dataset=first_data for data in webpage: # This reads the remaining lines of the webpage data=data.decode('utf-8') data=data.split() dataset+=data # For each iteration, the data transformed into # list append to dataset with first_data as its first list # #print(dataset) data_float=dataset for i in range(len(dataset)): data=float(dataset[i]) data_float[i]=float(dataset[i]) # Elements in the list 'dataset' are transformed to # float for additional operations such as min, max, range, sum count+= 1 # This counts the number of elements in the list highvalue =max(highvalue,data) # Zero is assigned to highvalue for its start value. # The data changes for each loop and compared with each element in the list lowvalue = min(data_float) totalvalue =sum(data_float) rangevalue = highvalue - lowvalue #print(count) observation=len(dataset) average=totalvalue/observation mean=st.mean(data_float) median=st.median(data_float) median_high=st.median_high(data_float) median_low=st.median_low(data_float) #mode=st.mode(data_float)# If there is more than one mode, it will return Stat Error stdev=st.pstdev(data_float) variance=st.pvariance(data_float) return print('The Dataset in List form is:\n',data_float,'\n', '=============================================\n',' DIAGNOSTIC ANALYTICS \n', '=============================================\n','The Descriptive Statistics of the Dataset are:', '\nTotal:\t\t{3}\nMaximum:\t{0}\nMinimum:\t{1}\nRange:\t\t{2}\nAverage:\t{5:.2f}\nMedian:\t\t{6}' '\nMedianHigh:\t{7}\nMedian Low:\t{8}\nVariance:\t{10:.2f}\nStd.Dev:\t{9:.2f}\nCounts:\t\t{4}' .format(highvalue,lowvalue,rangevalue,totalvalue,observation,mean,median,median_high, median_low,stdev,variance),'\n','=============================================')
def connected_components(matrix, author_to_int): #convert matrix to adj_list v = len(matrix) adj_list = convert_matrix_to_adj_list(matrix, v) #using dfs to find connected components metavisited, list_of_ints = [False for _ in range(v)], [] for vertex in range(v): if not metavisited[vertex]: ret = dfs(metavisited, vertex, adj_list) list_of_ints.append(ret[0]) metavisited = ret[1] largest = largest_connected(list_of_ints) max_cc_size, max_cc, sizes = largest[0], largest[1], sorted(largest[2]) #convert ints into authors inverse_author_dict = {v: k for k, v in author_to_int.items()} list_of_strings, max_cc_string = [], [] for cc in list_of_ints: string_cc = [inverse_author_dict[i] for i in cc] if cc == max_cc: max_cc_string = string_cc list_of_strings.append(string_cc) #statistics of cc sizes average = statistics.mean(sizes) median = statistics.median(sizes) median_low = statistics.median_low(sizes) median_high = statistics.median_high(sizes) mode = calc_mode(sizes) #density per connected component d_per_component = density_per_component(list_of_ints, matrix, author_to_int) #saving info into a text file with open('connected_components.txt', 'w') as f: f.write('Connected authors: ' + '\n') for cc in list_of_strings: f.write('\n' + str(len(cc)) + ' ' + str(cc) + '\n') f.write('Density : ' + str(d_per_component[str(cc)]) + '\n') f.write('\n') f.write('Connected components: ' + '\n') for cc in list_of_ints: f.write(str(len(cc)) + ' ' + str(cc) + '\n') f.write('\n') f.write('Number of connected components: ' + str(len(list_of_ints)) + '\n') f.write('Largest component size: ' + str(max_cc_size) + '\n') f.write('Largest connected component: ' + str(max_cc_string) + '\n') f.write('Component sizes: ' + str(sizes) + '\n') f.write('Average component size: ' + str(average) + '\n') f.write('Median: ' + str(median) + '\n') f.write('Low median: ' + str(median_low) + '\n') f.write('High median: ' + str(median_high) + '\n') f.write('Mode: ' + str(mode)) pickle.dump(adj_list, open("adj_list.p", "wb")) pickle.dump(list_of_ints, open("list_of_ints.p", "wb"))
def main(): print(stats.mean(range(6))) print(stats.median(range(6))) print(stats.median_low(range(6))) print(stats.median_high(range(6))) print(stats.median_grouped(range(6))) try: print(stats.mode(range(6))) except Exception as e: print(e) print(stats.mode(list(range(6)) + [3])) print(stats.pstdev(list(range(6)) + [3])) print(stats.stdev(list(range(6)) + [3])) print(stats.pvariance(list(range(6)) + [3])) print(stats.variance(list(range(6)) + [3]))
def __init__(self, values, stdDevs): new_values = [] for i in values: if i != '': try: if "." in i: new_values.append(float(i)) else: new_values.append(int(i)) except: pass #already picked up by error checks values = new_values super().__init__(values) self.stDevOutliers = [] standardDeviations = Decimal(stdDevs) if len(values) >= 8: self.pval = mstats.normaltest(array(values))[1] else: self.pval = 100 self.min = min(values) self.max = max(values) self.mean = Decimal(mean(values)).quantize(Decimal('.00000')) self.median_low = median_low(values) self.median = median(values) self.median_high = median_high(values) self.stdev = Decimal(stdev(values)).quantize(Decimal('.00')) self.normDist = 'No' if(self.pval > 0.055): self.normDist = 'Yes' elif self.pval == 100: self.normDist = 'N/A' if self.normDist == 'Yes': outlier_count = 0 for x, value in enumerate(values): if value < (self.mean - standardDeviations * self.stdev) or \ value > (self.mean + standardDeviations * self.stdev): if outlier_count > max_Outliers: self.stDevOutliers = ">%d outliers" % max_Outliers break self.stDevOutliers.append("Row: %d Value: %s" % (x, value)) outlier_count += 1
def __init__(self, values, stdDevs): standardDeviations = stdDevs new_values = [] for i in values: if i != '': try: new_values.append(float(i)) except: pass #already picked up in error checks values = new_values super().__init__(values) self.stDevOutliers = [] if len(values) >= 8: self.pval = mstats.normaltest(array(values))[1] else: self.pval = 100 if self.mode != 'N/A': self.mode = self.int_to_sci(self.mode) self.min = self.int_to_sci(min(values)) self.max = self.int_to_sci(max(values)) self.mean = self.int_to_sci(mean(values)) self.median_low = self.int_to_sci(median_low(values)) self.median = self.int_to_sci(median(values)) self.median_high = self.int_to_sci(median_high(values)) self.stdev = self.int_to_sci(stdev(values)) self.normDist = 'No' if(self.pval < 0.055): self.normDist = 'Yes' elif(self.pval == 100): self.normDist = 'N/A' if self.normDist == 'Yes': outlier_count = 0 for x, value in enumerate(values): if value < (float(self.mean) - standardDeviations * float(self.stdev)) or \ value > (float(self.mean) + standardDeviations * float(self.stdev)): if outlier_count > max_Outliers: self.stDevOutliers = ">%d outliers" % max_Outliers break self.stDevOutliers.append("Row: %d Value: %s" % (x, value)) outlier_count += 1
m2 = {} l1 = [] l2 = [] while n <= end: c += 1 h = intHash32(n) #Extract left most 12 bits x1 = (h >> 20) & 0xfff m1[x1] = 1 z1 = m - len(m1) #Linear counting formula u1 = int(m * math.log(float(m) / float(z1))) e1 = abs(100*float(u1 - c)/float(c)) l1.append(e1) print("%d %d %d %f" % (n, c, u1, e1)) #Extract right most 12 bits x2 = h & 0xfff m2[x2] = 1 z2 = m - len(m2) u2 = int(m * math.log(float(m) / float(z2))) e2 = abs(100*float(u2 - c)/float(c)) l2.append(e2) print("%d %d %d %f" % (n, c, u2, e2)) n += 1 print("Left 12 bits error: min=%f max=%f avg=%f median=%f median_low=%f median_high=%f" % (min(l1), max(l1), stat.mean(l1), stat.median(l1), stat.median_low(l1), stat.median_high(l1))) print("Right 12 bits error: min=%f max=%f avg=%f median=%f median_low=%f median_high=%f" % (min(l2), max(l2), stat.mean(l2), stat.median(l2), stat.median_low(l2), stat.median_high(l2)))
while(j > left and A[j - 1] > A[j]): swap(A,j,j - 1) j -= 1 return left + int(math.ceil((right - left) / 2)) def swap(A,i,j): A[i],A[j] = A[j],A[i] def partition(A,lo,hi,pivot): pivotVal = A[pivot] swap(A,hi,pivot) i = lo for j in range(lo,hi): if( A[j] <= pivotVal ): swap(A,i,j) i += 1 swap(A,hi,i) return i N = 1000 for i in range(1,N+1): A = [random.randint(0,100) for j in range(0,i)] myMed = median(A) theirMed = median_high(A) if(myMed != theirMed): print("ERROR! our median algorithm found %d while theirs found %d. Dump of A:\n%s" % (myMed, theirMed, str(A))) else: continue print("done! ran through %d test cases." % N)
print(num) a = [1, 2, 3, 4] num = statistics.median(a) print(num) a = [1, 2, 3, 4, 5] num = statistics.median(a) print(num) a = [1, 2, 3, 4] num = statistics.median_low(a) print(num) a = [1, 2, 3, 4, 5] num = statistics.median_high(a) print(num) a = [1, 2, 3, 4] num = statistics.median_grouped(a) print(num) a = [1, 2, 3, 4, 5, 4] num = statistics.mode(a) print(num) a = [1, 2, 3, 4, 5, 1] num = statistics.mode(a) print(num) a = [1, 2, 3, 4]
def calcular_dados(): """ Calcula as estatisticas de idade: 1. Mostrar a menor idade calculada 2. Mostrar a maior idade calculada 3. Mostrar a média de idade dos adultos 4. Mostrar a quantidade de nascimentos por década O que temos: [ 1970, 1981, 1998, 2002, 1990, 1970 ] O que queremos: { 1980: 10, 1990: 15, 2000: 5 } 1o passo: converter anos em decadas: 1985 / 10 = 198,5 int -> 198 x10 = 1980 2o passo: criar uma lista nova (set_decadas) com as decadas sem repetir 3o passo: contar as decadas na lista original (lista_decadas) usando a lista nova Para cada decada dentro da lista nova (set_decadas) contar qtas vezes ela aparece na lista original (lista_decadas) """ # Lista por Compreensão # lista = [ <expressão para valor> <loop> <expressão para loop> ] menor_idade = min([ objeto.idade for objeto in lista_entrevistados ]) maior_idade = max([ objeto.idade for objeto in lista_entrevistados ]) media_adultos = statistics.median_high( [ objeto.idade for objeto in lista_entrevistados if objeto.idade >= 18 ] ) lista_decadas = [ int(objeto.ano_informado/10)*10 for objeto in lista_entrevistados] set_decadas = set(lista_decadas) # Dicionário por Compreensão # dicionario = { <expressao para chave>:<expressao para valor> <loop> <expressao pro loop>} qtd_nascimentos = { decada:lista_decadas.count(decada) for decada in set_decadas } print("\nResultados:") print('-----------------------------------------') print("Quantidade de Entrevistas: {}".format(len(lista_entrevistados))) print("Menor Idade Informada: {}".format(menor_idade)) print("Maior Idade Informada: {}".format(maior_idade)) print("Média de Idade dos Adultos Informados: {}".format(media_adultos)) print("\nNascimentos por Década") print('-----------------------------------------') for decada, quantidade in qtd_nascimentos.items(): print("{}: {} nascimentos".format(decada, quantidade)) print('\n\n') resposta_ok = False while resposta_ok == False: try: resposta = input("Deseja mostrar os dados num arquivo? (s/n)") resposta = resposta[0].lower() if resposta == 's' or resposta == 'n': resposta_ok = True except: continue if resposta == 's': sh.gedit("dados.json")
def doCalc(G, counter, results): print("start calculations") fh = open("hamster.edgelist", 'rb') Ginit = nx.read_edgelist(fh, create_using=nx.DiGraph()) fh.close() pr = nx.pagerank(G, alpha=0.85) prtwo = nx.pagerank(Ginit, alpha=0.85) avg = float(1) / float(number_of_nodes(G)) isavg = 0 underavg = 0 aboveavg = 0 minv = 1 maxv = 0 work = 0 for key, value in pr.items(): #print(key, 'corresponds to', value) g = str(value) a = str(key) + ": " + g #print(a) work = work + value if (value < avg): underavg = underavg + 1 if (minv > value): minv = value elif (value == avg): isavg = isavg + 1 else: aboveavg = aboveavg + 1 if (maxv < value): maxv = value onemin = 0 onemax = 0 items = [] for k, l in pr.items(): items.append(l) if (l == minv): onemin = onemin + 1 elif (l == maxv): onemax = onemax + 1 m = str(onemin) n = str(onemax) b = str(work) c = str(isavg) d = str(underavg) e = str(aboveavg) h = str(minv) i = str(maxv) f = str(avg) o = str(statistics.median(items)) p = str(statistics.median_low(items)) q = str(statistics.median_high(items)) s = str(statistics.median_grouped(items)) #t = str(statistics.mode(items)) u = str(statistics.pstdev(items)) v = str(statistics.pvariance(items)) w = str(statistics.stdev(items)) x = str(statistics.variance(items)) #y= str(statistics.StatisticsError(items)) if (os.stat(os.getcwd() + "/results/testResults" + str(counter) + ".txt").st_size >= 5000000): results.close() counter = counter + 1 results = open( os.getcwd() + "/results/testResults" + str(counter) + ".txt", 'w') print(counter) results.write('\n') results.write('testdata \n') results.write(json.dumps(pr) + '\n') print("start new pagerank") one = pagerankCalc(G, pr, False) print("start init pagerank") two = pagerankCalc(Ginit, prtwo, False) results.write("average: " + f + '\n') results.write("total: " + b + '\n') results.write("isavg: " + c + '\n') results.write("underavg: " + d + '\n') results.write("aboveavg: " + e + '\n') results.write("minimum: " + h + '\n') results.write("maximum: " + i + '\n') results.write("nrminimum: " + m + '\n') results.write("nrmaximum: " + n + '\n') results.write("median: " + o + '\n') results.write("low median: " + p + '\n') results.write("high median: " + q + '\n') results.write("grouped median: " + s + '\n') #results.write("mode: "+t+'\n') results.write("pstdev: " + u + '\n') results.write("pvariance: " + v + '\n') results.write("stdev: " + w + '\n') results.write("variance: " + x + '\n') print("start pagerank calc") results.write("rank list: " + json.dumps(one) + '\n') results.write("rank list init: " + json.dumps(two) + '\n') #results.write("error: "+y+'\n') # too much data, only if actually test print("start eroor calc") results.write("rank list error : " + json.dumps(one) + '\n') results.write("rank list init error: " + json.dumps(two) + '\n') print("start error calc") error = calcrankError(two, one) pr = nx.pagerank(G, alpha=0.85) prtwo = nx.pagerank(Ginit, alpha=0.85) errorvalue = calcvalueError(two, one, prtwo, pr) results.write("rank based error: " + str(error) + '\n') results.write("value based error: " + str(errorvalue) + '\n') print("succes writing to test " + str(counter) + " result") return counter, results
atexit.register(lambda: p.terminate()) time.sleep(1) # Wait for the server to launch. """ def test_runserver(): import requests requests.get('http://localhost:8000/') if __name__ == '__main__': for cmd in ['runserver', 'gunserver']: t = timeit.Timer('test_runserver()', setup_code.format(cmd=cmd)) loop, repeat = 1000, 3 results = t.repeat(repeat, loop) print() print(cmd, 'loop of', loop, 'repeated', repeat, 'times') print('=============') print('Average: {mean:.2f}'.format(mean=statistics.mean(results))) print('Medians: {me:.2f} {lo:.2f} {hi:.2f}'.format( me=statistics.median(results), lo=statistics.median_low(results), hi=statistics.median_high(results), )) print('Min/Max: {min:.2f} {max:.2f}'.format( min=min(results), max=max(results), )) print()
for slice2 in range(0,200): ymean = input[slice2] popt, pcov = curve_fit(func, x, ymean,[1000, 3, 1]) k = func(0,*popt) if k > 800: densmean2[i2] = k i2 = i2 + 1 print 'length of densmap2', len(densmean2) plt.plot(zcoord,densmean,'k.') import statistics print mean(densmean2) print median(densmean2) print statistics.median_low(densmean2) print statistics.median_high(densmean2) #print statistics.mode(densmean2) # <codecell> ## DELETE THIS CELL, IT GIVES ONLY HOW MANY TIMES DOES EACH VALUE OF densmean APPEAR -> NOT USEFUL from collections import Counter c = Counter(densmean) #print densmean #list(c) c += Counter() #print c.most_common() # n least common elements #print c[-3.2638837653955745e-17] #sum(c.values()) # <codecell>
def findMedian(self): return (statistics.median_high(self.x_list), statistics.median_high(self.y_list))
import statistics lista1 = [3,5,3,12,13,3,7,8] lista2 = [3,3,3,5,7,8,12,13] print(statistics.median(lista1)) print(lista1) print(statistics.median_low(lista1)) print(statistics.median(lista2)) print(lista2) print(statistics.median_low(lista2)) print(statistics.median_high(lista1)) print(lista2[(statistics.median_high(lista2))]) l2 = ["bla", "aga"] l2[0] = 0 print(l2)
import pandas as pd import matplotlib.pyplot as plt dane = np.loadtxt("MDR_RR_TB_burden_estimates_2020-10-29.csv", delimiter=',', skiprows=1, usecols=12) # print(dane) print(np.max(dane)) print(dane.max()) print(np.median(dane)) dane1 = np.loadtxt("Wzrost.csv", delimiter=',', skiprows=0) # print(dane1) print("odchylenie standardowe:", st.stdev(dane1)) print("odchylenie standardowe 2", st.pstdev(dane1)) print("median high:", st.median_high(dane1)) print("median low:", st.median_low(dane1)) print("pstdev:", st.pstdev(dane1)) print("stdev:", st.stdev(dane1)) print("pvariance:", st.pvariance(dane1)) print("variance:", st.variance(dane1)) print("mode:", st.mode(dane1)) print(sc.ttest_1samp(dane, 0)) df = pd.read_csv("brain_size.csv", delimiter=";", skiprows=0) # print(df) print(np.mean(df['VIQ'])) print(df['Gender'].value_counts())
# using Python statistics functions import statistics import csv import array # simple statistics operations sample_data1 = [1, 3, 5, 7] sample_data2 = [2, 3, 5, 4, 3, 5, 3, 2, 5, 6, 4, 3] # TODO: Use the mean function - calculates an average value print(statistics.mean(sample_data1)) # TODO: Use the different median functions print(statistics.median(sample_data1)) print(statistics.median_high(sample_data1)) print(statistics.median_low(sample_data1)) print(statistics.median_grouped(sample_data1)) # TODO: The mode function indicates which data item occurs # most frequently print(statistics.mode(sample_data2)) # Read data from a CSV file and calculate statistics def readData(): with open("StockQuotes.csv") as dataFile: data = array.array('f', []) reader = csv.reader(dataFile) curLine = 0 for row in reader:
#Медиана n = len(x) if n % 2: median_ = sorted(x)[round(0.5 * (n - 1))] else: x_ord, index = sorted(x), round(0.5 * n) median_ = 0.5 * (x_ord[index - 1] + x_ord[index]) print(f'Расчет медианы: {median_}') median_2 = statistics.median(x) print(f'Расчет медианы с помощью statistics.median(): {median_2}') statistics.median_low(x[:-1]) print( f'Расчет медианы с помощью statistics.median_low: {statistics.median_low(x[:-1])}' ) statistics.median_high(x[:-1]) print( f'Расчет медианы с помощью statistics.median_high {statistics.median_high(x[:-1])}' ) median_2 = np.median(y) print(f'Расчет медианы с помощью np.median: {median_2}') #Мода u = [2, 3, 2, 8, 12] mode_ = max((u.count(item), item) for item in set(u))[1] print(f'Вычисление моды: {mode_}') mode_2 = statistics.mode(u) print(f'Вычисление моды с помощью statistics.mode(): {mode_2}') mode_3 = statistics.multimode(u) print(f'Вычисление моды с помощью statistics.multimode(): {mode_3}') mode_4 = scipy.stats.mode(u)
''' Created on 2016. 12. 8. @author: lsh ''' import statistics as sta data = [1, 2, 3, 4, 5, 6] print(sta.mean(data)) print(sta.median(data)) print(sta.median_low(data)) print(sta.median_high(data)) print(sta.pvariance(data)) print(sta.pstdev(data)) print(sta.variance(data)) print(sta.stdev(data))
def update_event(self, inp=-1): self.set_output_val(0, statistics.median_high(self.input(0)))
import statistics n = int(input()) a = list(map(int, input().split())) p = int(int(n * (n + 1) / 2) / 2 + 1) ans = a flg = False for i in range(2, n): for j in range(n - i): t = statistics.median_high(a[j:i + j]) ans.append(t) if (ans.count(t) >= p): flg = True break if (flg): break print(statistics.median_high(ans))
import statistics as s olst = [1, 2, 3, 5, 6, 9, 4, 2, 10] elst = [0, 1, 4, 5, 6, 6, 8, 6] print(s.mean(olst)) print(s.mean(elst)) print(s.median(olst)) print(s.median(elst)) print(s.median_low(olst)) print(s.median_high(elst)) print(s.mode(olst), s.mode(elst), sep=' And ') print(s.stdev(olst), s.stdev(elst)) print(s.variance(olst), s.variance(elst), sep=' And ') print(s.pvariance(olst), s.pvariance(elst), sep=' And ') print(s.pstdev(olst), s.pstdev(elst), sep=' And ')
import statistics score = [30, 40, 60, 70, 80, 90] print(statistics.mean(score)) print(statistics.harmonic_mean(score)) print(statistics.median(score)) print(statistics.median_low(score)) print(statistics.median_high(score)) import time print(time.time()) t = time.time() print(time.ctime(t)) import time now = time.localtime() print("%d년 %d월 %d일" % (now.tm_year, now.tm_mon, now.tm_mday)) print("%d:%d;%d" % (now.tm_hour, now.tm_min, now.tm_sec)) import time start = time.time() for a in range(1000): print(a) end = time.time() print(end - start) print("안녕하세요.") time.sleep(1)
median = statistics.median(l) print(median) # 3 #%% l_even = [10, 1, 3, 7, 1, 6] median = statistics.median(l_even) print(median) # 4.5 median_low = statistics.median_low(l_even) print(median_low) # 3 median_high = statistics.median_high(l_even) print(median_high) # 6 print( statistics.median_high(l) == statistics.median_low(l) == statistics.median( l)) # True #%% mode = statistics.mode(l) print(mode) # 1 l_mode_error = [1, 2, 3, 4, 5] # mode = statistics.mode(l_mode_error)
import random,statistics print(random) print(help(statistics)) #print(help(random)) print(random.randrange(1000000)) ra=random.choice(['apple','banana','greps']) print(ra) data=(2,4,5,3,5,7) print(data) print(statistics.mean(data)) print(statistics.median_high(data)) print(statistics.median(data))
h = intHash32(n) #Extract left most 12 bits x1 = (h >> 20) & 0xfff m1[x1] = 1 z1 = m - len(m1) #Linear counting formula u1 = int(m * math.log(float(m) / float(z1))) e1 = abs(100 * float(u1 - c) / float(c)) l1.append(e1) print("%d %d %d %f" % (n, c, u1, e1)) #Extract right most 12 bits x2 = h & 0xfff m2[x2] = 1 z2 = m - len(m2) u2 = int(m * math.log(float(m) / float(z2))) e2 = abs(100 * float(u2 - c) / float(c)) l2.append(e2) print("%d %d %d %f" % (n, c, u2, e2)) n += 1 print( "Left 12 bits error: min=%f max=%f avg=%f median=%f median_low=%f median_high=%f" % (min(l1), max(l1), stat.mean(l1), stat.median(l1), stat.median_low(l1), stat.median_high(l1))) print( "Right 12 bits error: min=%f max=%f avg=%f median=%f median_low=%f median_high=%f" % (min(l2), max(l2), stat.mean(l2), stat.median(l2), stat.median_low(l2), stat.median_high(l2)))
import statistics as s l1 = [4, 6, 2, 5, 7, 7] x = s.median_high(l1) print(f'Median_high is {x}') ''' Output: Median_high is 6 '''
def display_patches(n_patches, imageset, filestr=None, display=False, figsize=(8, 8), patch_dims=(16, 16), pcobject=None, center=False, labels=True, title=None, cmap='gray', acts=None, sample=False): ''' Function that takes in batch major data (must be a list) ''' fs = factors(n_patches) if len(fs) % 2 == 0: dims = [statistics.median_low(fs), statistics.median_high(fs)] else: dims = [statistics.median(fs), statistics.median(fs)] fig = plt.figure(figsize=(8, 8)) axs = [fig.add_subplot(dims[0], dims[1], i + 1) for i in range(n_patches)] if labels == True: cols = ['{}'.format(col + 1) for col in range(dims[1])] rows = ['{}'.format(row + 1) for row in range(dims[0])] for ax, col in zip(np.array(axs).reshape(dims)[0], cols): ax.set_title(col, fontweight="bold", size=30, pad=15) for ax, row in zip(np.array(axs).reshape(dims)[:, 0], rows): # ax.set_ylabel(row, rotation=0, size='large', labelpad=10) ax.set_ylabel(row, fontweight="bold", size=30, rotation=0, verticalalignment='center', labelpad=25) if title is not None: # fig.suptitle(title, fontsize=18, y=1.00) # fig.suptitle(title,fontweight ="bold",fontsize=20) fig.suptitle(title, fontweight="bold", fontsize=50) if type(imageset) != list: imageset = [imageset[i, :] for i in range(imageset.shape[0])] if acts is not None: sorted_idxs = np.argsort( np.linalg.norm(acts[:n_patches, :], ord=1, axis=1))[::-1] imageset = [imageset[:n_patches][idx] for idx in sorted_idxs] if sample == False: imagelist = zip(axs, imageset) else: imagelist = zip(axs, random.sample(imageset, n_patches)) for ax, image in imagelist: if center == True: vmax = np.max(np.abs([image.min(), image.max()])) vmin = -vmax else: vmax = None vmin = None if pcobject: image = np.reshape(pcobject.inverse_transform(image), patch_dims) else: image = np.reshape(image, patch_dims) ax.imshow(image, cmap=cmap, interpolation='nearest', vmin=vmin, vmax=vmax) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_ticks([]) fig.subplots_adjust(wspace=0, hspace=0) fig.set_figheight(dims[0]) fig.set_figwidth(dims[1]) fig.tight_layout(rect=[0, 0, 1, 0.98]) figure = plt.gcf() if filestr is not None: figure.savefig(filestr, dpi='figure') if display == False: plt.close() return (fig, axs)
percent = [ 2.606255012, 1.222935044, 1.283079391, 3.628708901, 6.856455493, 4.911788292, 2.886928629, 0.781876504, 0.962309543, 2.265437049, 6.816359262, 3.688853248, 3.468323978, 5.633520449, 4.530874098, 1.984763432, 0.922213312, 3.327987169, 4.190056135, 5.493183641, 1.864474739, 10.60545309, 2.425821973, 2.726543705, 8.740978348, 6.174819567 ] percent.sort() print(percent) #this ends with error #print(median(percent)) #print(median_low(percent)) #print(median_high(percent)) #this succeeds import statistics print(statistics.median(percent)) print(statistics.median_low(percent)) print(statistics.median_high(percent)) #this also succeeds ##from statistics import * ##print(median(percent)) ##print(median_low(percent)) ##print(median_high(percent))
662, 4, 26, 10, 13, 15, 21, 17, 123, 643, 1, 95, 20, 256, 2048, 1116, 5, 6, 1, 52, 6, 931, 83, 7, 9, 80, 99, 136, 18, 24, 14, 123, 5 ] print("Mean", statistics.mean(data)) print("Median", statistics.median(data)) try: print("Mode", statistics.mode(data)) except statistics.StatisticsError: print("No unique mode.") print("Standard deviation:", statistics.stdev(data)) print("Variance:", statistics.variance(data)) odd_data = [4, 7, 3, 9, 12] even_data = [4, 7, 3, 9, 12, 2] print(odd_data) print(even_data) print("Median odd data", statistics.median(odd_data)) print("Median even data", statistics.median(even_data)) print("Median low of even data", statistics.median_low(even_data)) print("Median high of even data", statistics.median_high(even_data)) print("Standard deviation:", statistics.stdev(odd_data)) print("Variance:", statistics.variance(odd_data)) # Simple plot to look for outliers import seaborn as sns import matplotlib.pyplot as plt sns.boxplot(x=even_data) plt.show() sns.boxplot(x=data) plt.show()
import statistics nums = [44, 62, 11, 22, 155, 22, 3333, 444] z = statistics.median_high(nums) print(z)
import statistics from sys import stdin message = stdin.readline().strip() N = len(message) rc = [] for i in range(1, N + 1): if N % i == 0: rc.append(i) N_rc = len(rc) if N_rc % 2 == 1: r = statistics.median(rc) c = statistics.median(rc) else: r = statistics.median_low(rc) c = statistics.median_high(rc) matrix = [[0 for _ in range(r)] for _ in range(c)] for i in range(c): for j in range(r): matrix[i][j] = message[(i * r) + j] for i in range(r): for j in range(c): print(matrix[j][i], end="")
y.append(float(val[4])) print(len(x),len(y)) peak = 49.9378768165 + 1399.2004138 #print(statistics.mean(y)) #print(statistics.stdev(y)) for val in y: if val >peak: z.append(val) print(len(z)) import matplotlib.pyplot as plt import numpy as np spread = z flier_high = [statistics.median_high(z)] flier_low = [statistics.median_low(z)] print(flier_high, flier_low) data = np.concatenate((spread, flier_high, flier_low), 0) plt.boxplot(data,1) plt.figure() plt.show() ''' for i in range(1,len(x)): x[i-1] = float(x[i])-float(x[i-1]) ll = []
def median_high(self): return stats.median_high(self.values())
__author__ = 'luowen' """ the statisic demo """ import statistics list1 = [1, 2, 3, 4, 5, 6] midNum = statistics.mean(list1) print(midNum) # get the average data of list1 medianNum = statistics.median(list1) print(medianNum) # get median data of list1 medianNumLow = statistics.median_low(list1) print(medianNumLow) # get median lower data of list1 medianNumHigh = statistics.median_high(list1) print(medianNumHigh) # get median hight data of list1 medianNumGroup = statistics.median_grouped(list1, 10) print(medianNumGroup) # get detail information from https://docs.python.org/3/library/statistics.html
def log_graph(G, identify_self=True, nodecolor="label", epoch=0, fig_size=(4, 3), dpi=300, label_node_feat=False, edge_vmax=None, args=None): """ Args: nodecolor: the color of node, can be determined by 'label', or 'feat'. For feat, it needs to be one-hot' """ cmap = plt.get_cmap("Set1") plt.switch_backend("agg") fig = plt.figure(figsize=fig_size, dpi=dpi) node_colors = [] # edge_colors = [min(max(w, 0.0), 1.0) for (u,v,w) in G.edges.data('weight', default=1)] edge_colors = [w for (u, v, w) in G.edges.data("weight", default=1)] # maximum value for node color vmax = 8 for i in G.nodes(): if nodecolor == "feat" and "feat" in G.nodes[i]: num_classes = G.nodes[i]["feat"].size()[0] if num_classes >= 10: cmap = plt.get_cmap("tab20") vmax = 19 elif num_classes >= 8: cmap = plt.get_cmap("tab10") vmax = 9 break feat_labels = {} for i in G.nodes(): if identify_self and "self" in G.nodes[i]: node_colors.append(0) elif nodecolor == "label" and "label" in G.nodes[i]: node_colors.append(G.nodes[i]["label"] + 1) elif nodecolor == "feat" and "feat" in G.nodes[i]: # print(G.nodes[i]['feat']) feat = G.nodes[i]["feat"].detach().numpy() # idx with pos val in 1D array feat_class = 0 for j in range(len(feat)): if feat[j] == 1: feat_class = j break node_colors.append(feat_class) feat_labels[i] = feat_class else: node_colors.append(1) if not label_node_feat: feat_labels = None plt.switch_backend("agg") fig = plt.figure(figsize=fig_size, dpi=dpi) #pos_layout = nx.kamada_kawai_layout(G, weight=None) pos_layout = nx.fruchterman_reingold_layout(G) if G.number_of_nodes() == 0 or G.number_of_edges() == 0: edge_vmax = 1 edge_vmin = 0 else: weights = [d for (u, v, d) in G.edges(data="weight", default=1)] if edge_vmax is None: edge_vmax = statistics.median_high( [d for (u, v, d) in G.edges(data="weight", default=1)]) min_color = min([d for (u, v, d) in G.edges(data="weight", default=1)]) # color range: gray to black edge_vmin = 2 * min_color - edge_vmax nx.draw( G, pos=pos_layout, arrows=True, with_labels=True, font_size=4, labels=feat_labels, node_color=node_colors, vmin=0, vmax=vmax, cmap=cmap, edge_color=edge_colors, edge_cmap=plt.get_cmap("Greys"), edge_vmin=edge_vmin, edge_vmax=edge_vmax, width=1.0, node_size=120, alpha=0.8, ) fig.axes[0].xaxis.set_visible(False) fig.canvas.draw()
print("Arithmetischer Mittelwert: ", statistics.mean(probe1)) # Mitterlwert = mean = Summe der Werte geteilt durch ihre Anzahl print("Harmonischer Mittelwert: ", statistics.harmonic_mean(probe1)) # harmonischer Mitterlwert = mean = Summe der Werte geteilt durch ihre Kehrwerte print() # Median # Mitte der Zahlenmenge print("Median: ", statistics.median(probe1)) probe2 = [5, 2, 4, 17, 3] print("Median: ", statistics.median(probe2)) print() # Unterer Median # Mitte der Zahlenmenge, low wertet nach unteren Wert ab print("Unterer Median: ", statistics.median_low(probe1)) print("Unterer Median: ", statistics.median_low(probe2)) print() # Oberer Median # Mitte der Zahlenmenge, high wertet nach oberen Wert auf print("Oberer Median: ", statistics.median_high(probe1)) print("Oberer Median: ", statistics.median_high(probe2)) print() # Tupel, Werte eines Dictionary probe3 = (5, 2, 4, 17) print("aus Tupel: ", statistics.mean(probe3)) probe4 = {'D': 5, 'NL': 2, 'CH': 4, 'F': 17} print("aus Dictionary: ", statistics.mean(probe4.values()))
#Arithmetic mean of data: print("data mean =", statistics.mean(data)) #Middle value (median) of data: print("data median =",statistics.median(data)) #Low median: is always a member of the data set. When the number of data points is odd the middle value is returned. When it is even, #the smaller of the two data points is returned: print("data low median =",statistics.median_low(data)) #Example with a dataset with even number of data points: print("even data set = ", (1,3,5,7)) print("even data set low median =",statistics.median_low([1,3,5,7])) #Median high: when the number of points in the data set is odd, this function will return the middle value. When the number of data points #is even, it will return the highest of the two middle points. print("data high median =",statistics.median_high(data)) #Example with a dataset with even number of data points: print("even data set high median =",statistics.median_high([1,3,5,7])) #MEASURES OF SPREAD: #Sample standard deviation print("data standard deviation =", statistics.stdev(data)) #Variance print("data variance =",statistics.variance(data)) #There is also another measurement of standard deviation, the population standard deviation: print("data population standard deviation = ",statistics.pstdev(data,mu=None)) #And the population variance of data. In this function there is also the argument mu, that corresponds to the mean of data. If mu is #not assigned or is 'None', than the men is also calculated by this function.
def __init__(self, pi, gpio, measurement_time=120): self.pi = pi self.gpio = gpio self.period = 1 / 910 * 1000000 self.tick_high = None self.duty_cycle = None self.duty_scale = 1000 self.list_duty_cycles = [] self.duty_cycle_min = None self.duty_cycle_max = None #http://abyz.me.uk/rpi/pigpio/python.html#set_mode self.pi.set_mode(gpio=self.gpio, mode=pigpio.INPUT) #http://abyz.me.uk/rpi/pigpio/python.html#callback self.cb = self.pi.callback(user_gpio=self.gpio, edge=pigpio.EITHER_EDGE, func=self.cbf) print('{}{}{}'.format('Starting measurements for: ', measurement_time, ' seconds.')) print('----------------------------------------------------------') time.sleep(measurement_time) #stop callback before sorting list to avoid getting added new elements unintended #http://abyz.me.uk/rpi/pigpio/python.html#callback self.cb.cancel() time.sleep(1) self.list_duty_cycles = sorted(self.list_duty_cycles) #some analyzis of the dc values sorted_set = list(sorted(set(self.list_duty_cycles))) print('{} {}'.format('Ascending sorted distinct duty cycle values:', sorted_set)) print('----------------------------------------------------------') differences_list = [ sorted_set[i + 1] - sorted_set[i] for i in range(len(sorted_set) - 1) ] rounded_differences_list = [ round(differences_list[i], 2) for i in range(len(differences_list) - 1) ] counted_sorted_list = collections.Counter(rounded_differences_list) print('{} {}'.format( 'Ascending counted, sorted and rounded distinct differences between duty cycle values:', counted_sorted_list)) print('----------------------------------------------------------') #Median and median_high/median_low are chosen, because the biggest #and smallest values are needed, and not an avarage of the smallest #and biggest values of the selection. #https://docs.python.org/3/library/statistics.html#statistics.median print('{} {}'.format('Smallest 250 values:', self.list_duty_cycles[:250])) self.duty_cycle_min = statistics.median_high( self.list_duty_cycles[:20]) print('----------------------------------------------------------') print('{} {}'.format('Biggest 250 values:', self.list_duty_cycles[-250:])) self.duty_cycle_max = statistics.median_low( self.list_duty_cycles[-20:]) print('----------------------------------------------------------') print('duty_cycle_min:', round(self.duty_cycle_min, 2)) print('duty_cycle_max:', round(self.duty_cycle_max, 2))
#CHAPTER 8 # 1. Call a different function from the statistics module # 2. Create a module named cubed with a function that takes a number as a parameter, # and returns the number cubed. Import and call the function from another module. import statistics import cubed numbers = [1, 2, 3, 4, 5, 6, 7, 8] print(statistics.median_high(numbers)) print(cubed.cubed(4))
import statistics as st x = [3,1.5,4.5,6.75,2.25,5.75,2.25] print("_________Question1__________") print(st.mean(x)) print(st.harmonic_mean(x)) print(st.median(x)) print(st.median_low(x)) print(st.median_high(x)) print(st.median_grouped(x)) print(st.mode(x)) print(st.pstdev(x)) print(st.pvariance(x)) print(st.stdev(x)) print(st.variance(x)) print("_________Question2__________") import random print(random.random()) print(random.randrange(10)) print(random.choice(["Ali","Khalid","Hussam"])) print(random.sample(range(1000),10)) print(random.choice("Orange Academy")) y = [1,5,8,9,2,4] random.shuffle(y) print(y)
def median(self): return statistics.median_high(self._salaries)
import statistics as s sales = [111, 222, 444, 222, 444, 222, 444, 222, 4455, 222] # print(s.mean(sales)) #avg print(max(sales)) #return max print(min(sales)) #return min print(sum(sales)) #return total print(len(sales)) #return count of items ## print(s.median(sales)) #return mid value (if even count : sum or mid 2 val/2) print(s.median_high(sales)) print(s.median_low(sales)) print(s.variance(sales)) #return var print(s.stdev(sales)) #return sqrt() for var print(s.median_high(sales)) print(s.mode(sales)) #return value which has highest freq/count ''' stats function: count/len min max sum mean/avv mode
def log_graph( writer, Gc, name, identify_self=True, nodecolor="label", epoch=0, fig_size=(4, 3), dpi=300, label_node_feat=False, edge_vmax=None, args=None, ): """ Args: nodecolor: the color of node, can be determined by 'label', or 'feat'. For feat, it needs to be one-hot' """ cmap = plt.get_cmap("Set1") plt.switch_backend("agg") fig = plt.figure(figsize=fig_size, dpi=dpi) node_colors = [] # edge_colors = [min(max(w, 0.0), 1.0) for (u,v,w) in Gc.edges.data('weight', default=1)] edge_colors = [w for (u, v, w) in Gc.edges.data("weight", default=1)] # maximum value for node color vmax = 8 for i in Gc.nodes(): if nodecolor == "feat" and "feat" in Gc.nodes[i]: num_classes = Gc.nodes[i]["feat"].size()[0] if num_classes >= 10: cmap = plt.get_cmap("tab20") vmax = 19 elif num_classes >= 8: cmap = plt.get_cmap("tab10") vmax = 9 break feat_labels = {} for i in Gc.nodes(): if identify_self and "self" in Gc.nodes[i]: node_colors.append(0) elif nodecolor == "label" and "label" in Gc.nodes[i]: node_colors.append(Gc.nodes[i]["label"] + 1) elif nodecolor == "feat" and "feat" in Gc.nodes[i]: # print(Gc.nodes[i]['feat']) feat = Gc.nodes[i]["feat"].detach().numpy() # idx with pos val in 1D array feat_class = 0 for j in range(len(feat)): if feat[j] == 1: feat_class = j break node_colors.append(feat_class) feat_labels[i] = feat_class else: node_colors.append(1) if not label_node_feat: feat_labels = None plt.switch_backend("agg") fig = plt.figure(figsize=fig_size, dpi=dpi) if Gc.number_of_nodes() == 0: raise Exception("empty graph") if Gc.number_of_edges() == 0: raise Exception("empty edge") # remove_nodes = [] # for u in Gc.nodes(): # if Gc pos_layout = nx.kamada_kawai_layout(Gc, weight=None) # pos_layout = nx.spring_layout(Gc, weight=None) weights = [d for (u, v, d) in Gc.edges(data="weight", default=1)] if edge_vmax is None: edge_vmax = statistics.median_high( [d for (u, v, d) in Gc.edges(data="weight", default=1)]) min_color = min([d for (u, v, d) in Gc.edges(data="weight", default=1)]) # color range: gray to black edge_vmin = 2 * min_color - edge_vmax nx.draw( Gc, pos=pos_layout, with_labels=False, font_size=4, labels=feat_labels, node_color=node_colors, vmin=0, vmax=vmax, cmap=cmap, edge_color=edge_colors, edge_cmap=plt.get_cmap("Greys"), edge_vmin=edge_vmin, edge_vmax=edge_vmax, width=1.0, node_size=50, alpha=0.8, ) fig.axes[0].xaxis.set_visible(False) fig.canvas.draw() if args is None: save_path = os.path.join("log/", name + ".pdf") else: save_path = os.path.join( "log", name + gen_explainer_prefix(args) + "_" + str(epoch) + ".pdf") print("log/" + name + gen_explainer_prefix(args) + "_" + str(epoch) + ".pdf") os.makedirs(os.path.dirname(save_path), exist_ok=True) plt.savefig(save_path, format="pdf") img = tensorboardX.utils.figure_to_image(fig) writer.add_image(name, img, epoch)
# = (2 * 2)/3 # = 1.333 # # Input : arr[] = {13.5, 14.5, 14.8, 15.2, 16.1} # Output : 14.7707 # ============================================================================= #harmonic mean print("Harmonic Mean") print(statistics.harmonic_mean([2.5, 3, 10])) print("Median") #median print(statistics.median([1, 3, 4, 5])) #median high print(statistics.median_high([1, 3, 5, 7])) #median high print(statistics.median_low([1, 3, 5, 7])) print("Statistics Grouped Median...") print(statistics.median_grouped([52, 52, 53, 54])) #mode print(statistics.mode(["red", "blue", "blue", "red", "green", "red", "red"])) print(statistics.mode([1, 1, 2, 2, 2])) data = [4, 5, 7, 1, 3, 6, 7] print(statistics.stdev(data)) print(statistics.variance(data))