Posted By

eiger_824 on 11/02/15



Versions (?)


 / Published in: Python


  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  5. This is a temporary script file.
  6. """
  8. #Assignment 1
  9. from __future__ import division
  10. import random
  11. from scipy import *
  12. from math import *
  14. #1.1)
  15. import numpy as np
  16. poisson_lambda = 4
  17. s = np.random.poisson(poisson_lambda,50)
  19. #1.2)
  21. mean = 0
  22. for i in s:
  23. mean = mean + i
  24. mean = mean/len(s) #<--------- APROX. MEAN
  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
  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
  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
  53. plt.title('Bar plot of frequencies of observed values')
  54. plt.xlabel('Observed values')
  55. plt.ylabel('Number of occurrences')
  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)
  66. plt2.title('Bar plot of the poisson distribution between range [0,13]')
  67. plt2.xlabel('Observed values')
  68. plt2.ylabel('Magnitude')
  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)
  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'
  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')
  99. #2.4)
  100. X1X2 = zip(X1,X2)
  102. #2.5)
  103. from sklearn import svm
  104. #a)
  105. clf = svm.SVC(kernel='linear')
  106., Y)
  107. #b)
  108. clf2 = svm.SVC(kernel='rbf', gamma=0.7)
  109., Y)
  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)
  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')
  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')

Report this snippet  


RSS Icon Subscribe to comments
Posted By: DavidBryant on February 28, 2018

Thanks for the code. Now I will type my essay fast with your help. I have been looking for this peace of code, without that I cannot finish my program.

Posted By: itskate on March 6, 2018

Extremely intriguing book to peruse, I have just perused two books of this Buy an Essay Online. He is better than average at composing, so far his books are among my top choices.

Posted By: richardy on April 11, 2018

Check the reviews of instantassignmenthelp before assignment help.

Posted By: miaryan on April 12, 2018

Hi itskate, can you help me with my essay

Posted By: LukeWright on April 26, 2018

Best one to share! Org

Posted By: ameliahadlock on May 22, 2018

Homework, or a homework task, is an arrangement of errands doled out to understudies by their instructors to be finished outside the class. Normal homework assignments may incorporate required perusing, a composition or writing venture, numerical activities to be finished, data to be explored before a test, or different aptitudes to be rehearsed. vehicle graphics dubai

Posted By: williamlucas on June 22, 2018

Thanks for sharing the code. It work like anything.

Assignment Help

Posted By: Michellejohnson1 on July 9, 2018

Feeling down throughout the week? Is the idea of writing a due project making you cringe? It’s nothing to feel ashamed of, we have all been there! Dump your project on my homework help programming homework help and forget about it. Maybe go to a barbeque party in the sun to lift yourself up. We all deserve a break sometimes. Comment on this post if you feel the same way.

Posted By: theresadelcas on September 5, 2018

My Assignment Services is a well-established and prominent name in providing high-quality assignment help online to students since almost a decade. You can trust our academic ghostwriters completely to get best quality write-ups including case studies, research proposals, dissertations and theses, and more. It is the best do my assignment provider.Students should always keep themselves ready to meet the university requirements in order to achieve good grades, moreover, they prefer attending lengthy classes instead of writing assignments. Students who are not comfortable with writing assignments can avail university assignment help by myob assignment help at My Assignment Services comparatively low prices.

Posted By: andrewsymond on October 16, 2018

You can rent Singapore expert writers to ensure top quality cheap essay writing service singapore. Singapore is known for a system of quality control before the assignment is delivered. We examine plagiarism on each assignment to ensure that they are 100% literary and error free.

Posted By: jamesstephens on October 17, 2018

The professional report writing help ireland writer at is well-versed with all the academic writing styles and format your assignment in any of the styles that students ask for such as MLA, APA, Harvard, Oxford, Chicago, Vancouver or any other.

Posted By: jeniferconnorsmk on October 17, 2018

Being a law student it is very difficult for me to invest a lot of time on other things, so when it comes to writing an assignment it becomes very challenging for me. Writing an assignment is a lot of work. We need to do a lot of research work and also write a lot of pages which is very time-consuming. So, I started searching for help and found the Business law assignment help australia. I was assisted by a professional with the assignment and the assignment that was delivered to me fetched me really good grades. I am highly satisfied with the assistance that was offered to me. Pay Someone To Do Your Assignment australia

You need to login to post a comment.