Posted By

eiger_824 on 11/02/15


Tagged

sdf


Versions (?)

assignment


 / Published in: Python
 

asdfasd

  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4.  
  5. This is a temporary script file.
  6. """
  7.  
  8. #Assignment 1
  9. from __future__ import division
  10. import random
  11. from scipy import *
  12. from math import *
  13.  
  14. #1.1)
  15. import numpy as np
  16. poisson_lambda = 4
  17. s = np.random.poisson(poisson_lambda,50)
  18.  
  19. #1.2)
  20.  
  21. mean = 0
  22. for i in s:
  23. mean = mean + i
  24. mean = mean/len(s) #<--------- APROX. MEAN
  25.  
  26. mean2 = 0
  27. s_square = s**2
  28. for i in s_square:
  29. mean2 = mean2 + i
  30. mean2 = mean2/len(s_square)
  31. variance = mean2 - mean**2 #<------ APROX VARIANCE
  32.  
  33. #1.3)
  34. print 'Calculated mean: '.__add__(str(mean))
  35. print 'Calculated variance: '.__add__(str(variance))
  36. print 'Theoretical mean: '.__add__(str(poisson_lambda))
  37. print 'Theoretical variance: '.__add__(str(poisson_lambda))
  38. #Theoretically, lambda is the value for both the mean and variance of a Poisson
  39. # distribution. The fact that the number of observations is not very high
  40. #suggests that the approximation calculated in 1.4 won't be very accurate.
  41. #If the number of observations was greater, the aproximation would be better
  42.  
  43. #1.4)
  44. from collections import Counter
  45. cnt = Counter()
  46. for i in s:
  47. cnt[i] += 1
  48. cnt
  49. #1.5)
  50. import matplotlib.pyplot as plt
  51. d = dict(cnt) #from Counter() to dictionary
  52. plt.bar(range(len(d)),d.values())
  53. plt.title('Bar plot of frequencies of observed values')
  54. plt.xlabel('Observed values')
  55. plt.ylabel('Number of occurrences')
  56. plt.show()
  57.  
  58.  
  59. #1.6)
  60. import matplotlib.pyplot as plt2
  61. poisson_pmf = [x for x in range(14)] #initialization
  62. #I am going to create manually a poisson distribution of 14 elements
  63. for i in range(14):
  64. poisson_pmf[i] = (((poisson_lambda)**i)*(e**(-poisson_lambda)))/factorial(i)
  65. plt2.bar(range(len(poisson_pmf)),poisson_pmf)
  66. plt2.title('Bar plot of the poisson distribution between range [0,13]')
  67. plt2.xlabel('Observed values')
  68. plt2.ylabel('Magnitude')
  69. plt2.show()
  70.  
  71. #1.7)
  72. from scipy import stats
  73. a = stats.ttest_1samp(s,2.0)
  74. b = stats.ttest_1samp(s,3.7)
  75. c = stats.ttest_1samp(s,4.3)
  76.  
  77.  
  78. #Assignment 2
  79. #2.1)
  80. import numpy as np2
  81. X1 = np2.random.exponential(5.0,100)
  82. X2= np2.random.exponential(5.0,100)
  83. #2.2)
  84. Y = [x for x in range(100)]
  85. for i in range(100):
  86. if X1[i]*X2[i]<=30+random.normal(0,100):
  87. Y[i] = 'Blue'
  88. else:
  89. Y[i] = 'Orange'
  90.  
  91. #2.3)
  92. import matplotlib.pyplot as plt3
  93. plt3.scatter(X1,X2,c=Y)
  94. plt3.xlabel('X1 values')
  95. plt3.ylabel('X2 values')
  96. plt3.title('Scatter plot of X1 vs X2')
  97. plt3.show()
  98.  
  99. #2.4)
  100. X1X2 = zip(X1,X2)
  101.  
  102. #2.5)
  103. from sklearn import svm
  104. #a)
  105. clf = svm.SVC(kernel='linear')
  106. clf.fit(X1X2, Y)
  107. #b)
  108. clf2 = svm.SVC(kernel='rbf', gamma=0.7)
  109. clf2.fit(X1X2, Y)
  110.  
  111. #2.6)
  112. #For the linear kernel:
  113. linear_prediction = clf.predict(X1X2)
  114. #For the rbf kernel:
  115. rbf_prediction = clf2.predict(X1X2)
  116.  
  117. #2.7)
  118. #For the linear kernel
  119. import matplotlib.pyplot as plt4
  120. plt4.scatter(X1,X2,c=linear_prediction)
  121. plt4.xlabel('X1 values')
  122. plt4.ylabel('X2 values')
  123. plt4.title('Linearly predicted scatter plot of X1 vs X2')
  124. plt4.show()
  125. #For the rbf kernel:
  126. import matplotlib.pyplot as plt5
  127. plt5.scatter(X1,X2,c=rbf_prediction)
  128. plt5.xlabel('X1 values')
  129. plt5.ylabel('X2 values')
  130. plt5.title('RBF-predicted scatter plot of X1 vs X2')
  131. plt5.show()

Report this snippet  

You need to login to post a comment.