Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import operator
  3. import math
  4. import urllib2
  5. import sys
  6. import scipy
  7. import numpy
  8. import matplotlib
  9. import pandas
  10. import sklearn
  11. import matplotlib.pyplot as plt
  12. from decimal import Decimal
  13. from pandas.plotting import scatter_matrix
  14. from functools import reduce
  15. from scipy.stats import kurtosis as kurtosis_scipy, skew as skewness_scipy
  16. from scipy.stats import moment
  17. from statistics import mode
  18.  
  19. #########
  20. url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
  21. names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
  22. dataset = pandas.read_csv(url, names=names)
  23.  
  24. A = []
  25. B = []
  26. C = []
  27. D = []
  28. Description = []
  29.  
  30. data = urllib2.urlopen(url)
  31. for line in data:
  32. x = line.split(',')
  33. if(len(x) == 5):
  34. A.append(float(x[0]))
  35. B.append(float(x[1]))
  36. C.append(float(x[2]))
  37. D.append(float(x[3]))
  38. Description.append((x[4]))
  39.  
  40. def bubble_sort(X):
  41. length = len(X)
  42. for i in range(length):
  43. for j in range(length-1):
  44. if X[j] > X[j+1]:
  45. X[j], X[j+1] = X[j+1], X[j]
  46.  
  47. return X
  48.  
  49. ######### Miary srednie klasyczne
  50.  
  51. #srednia arytmetyczna
  52. def mean(X):
  53. return sum(X)/ len(X)
  54.  
  55. #srednia harmoniczna
  56. def harmonic_mean(X):
  57. sum = 0.0
  58. for i in X:
  59. sum += 1.0 / i
  60. return len(X) / sum
  61.  
  62. print("Srednia harmoniczna: " + str(harmonic_mean(A)))
  63.  
  64. #srednia geometryczna
  65. def geomean(X):
  66. power = 1.0 / len(X)
  67. return reduce(operator.mul, X) ** power
  68.  
  69. print("Srednia: " + str(mean(A)))
  70. print("Srednia geometryczna: " + str(geomean(A)))
  71.  
  72. ######### Miary srednie pozycyjne
  73.  
  74. #mediana
  75. def median(X):
  76. sortedX = bubble_sort(X)
  77. #print(X)
  78. mid = len(sortedX) // 2 # podloga jako ze chcemy liczbe calkowita
  79. if (len(sortedX) % 2 == 0):
  80. # parzysta
  81. return (sortedX[mid-1] + sortedX[mid]) / 2.0
  82. else:
  83. # nieparzysta
  84. return sortedX[mid]
  85.  
  86. X = [10.3, 4.1, 12, 15.5, 20.2, 5.5, 15.5, 4.1]
  87. print("Mediana: " + str(median(X)))
  88.  
  89. #modalna (dominanta)
  90. def dominant(X):
  91. value = None
  92. count = 0
  93. for i in X:
  94. if count == 0:
  95. value = i
  96. count += 1
  97. elif i == value:
  98. count += 1
  99. else:
  100. count -= 1
  101. return (value)
  102.  
  103. print("Dominanta: " + str(dominant(A)))
  104.  
  105. #kwartyle
  106. def quartiles(X):
  107.  
  108. sortedX = bubble_sort(X)
  109. #print(bubble_sort(sortedX))
  110. mid = len(sortedX) // 2 # podloga jako ze chcemy liczbe calkowita
  111. #print(mid)
  112. #print(sortedX[:mid])
  113. #print(sortedX[mid:])
  114. if (len(sortedX) % 2 == 0):
  115. # parzysta
  116. Q1 = median(sortedX[:mid])
  117. Q3 = median(sortedX[mid:])
  118. else:
  119. # nieparzysta
  120. Q1 = median(sortedX[:mid])
  121. Q3 = median(sortedX[mid+1:])
  122.  
  123. return (Q1, Q3)
  124.  
  125. print("Kwartyl Q1: " + str(quartiles(A)[0])+" oraz kwartyl Q3: " + str(quartiles(A)[1]))
  126. #decyle
  127.  
  128. #percyle
  129.  
  130. ######### Miary rozproszenia
  131.  
  132. # rozstep
  133. # odchylenie standardowe
  134. def stdev(X):
  135. m = mean(X)
  136. return math.sqrt(sum((x-m)**2 for x in X) / len(X))
  137.  
  138. # wariancje
  139. # rozstep
  140. # odchylenie standardowe
  141. # wariancja
  142. # wspolczynnik zmiennosci
  143. # gestosc prawdopodobienstwa
  144. def normal(x):
  145. return exp((-x*x/2) - math.sqrt(math.pi*2))
  146.  
  147. ######### Miary# wspolczynnik zmiennosci
  148.  
  149. # moment centralny
  150. def CentralMoment(X, level):
  151. centralMoment = 0
  152. for innerX in X:
  153. centralMoment += ((innerX - mean(X))**level)
  154. return (Decimal(1/(Decimal(len(X))))*Decimal(centralMoment))
  155.  
  156. # Wspolczynnik asymetrii
  157. def asymmetryCoefficient(X):
  158. return (CentralMoment(X) / Decimal((stdev(X) ** 3)))
  159.  
  160. # Wspolczynnik skosnosci
  161. def skewness(X):
  162. toReturn = 0
  163. for innerX in X:
  164. toReturn += ((innerX - mean(X))**3)/len(X)
  165. toReturn = toReturn / (stdev**3)
  166. return toReturn
  167.  
  168. ######### Miary koncentracji
  169.  
  170. # Kurtoza
  171. def kurtosis(X):
  172. return (CentralMoment(X, 4) / Decimal(stdev(X)**4) - 3)
  173. print('Kurtoza: ' + str(kurtosis(A)))
  174.  
  175. # Wspolczynnik Giniego
  176. def gini(list_of_values):
  177. sorted_list = sorted(list_of_values)
  178. height, area = 0, 0
  179. for value in sorted_list:
  180. height += value
  181. area += height - value / 2.
  182. fair_area = height * len(list_of_values) / 2.
  183. return (fair_area - area) / fair_area
  184.  
  185. ######### Normalizacja zmiennej losowej. Dopasowanie rozkladu i analiza danych w jego kontekscie.
  186.  
  187. ######### Formulowanie i weryfikacja hipotez statystycznych.
  188.  
  189. ######### Obliczanie histogramu.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement