Guest User

Untitled

a guest
Feb 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4.  
  5. # Question 1 (similar to class):
  6.  
  7. # An oceanographer is studying wintertime sea surface temperature at a site on
  8.  
  9. # the east coast. For the past six years, she obtains the following values:
  10.  
  11. #
  12.  
  13. # [8.61, 7.575, 9.72, 2.087, 5.421, 1.659]
  14.  
  15. #
  16.  
  17. # In the space below, write a program that
  18.  
  19. # calculates the mean, and prints it on the screen. Use the built-in sum() and
  20.  
  21. # len() functions. Avoid using np.mean(), except to check your work. Use
  22.  
  23. # descriptive variable names -- see Section 2.12 of Python for Everybody.
  24.  
  25.  
  26.  
  27. values = np.array([8.61, 7.575, 9.72, 2.087, 5.421, 1.659])
  28. print (values)
  29. print('sum')
  30. s=sum(values)
  31. print(s)
  32. print('length')
  33. l=len(values)
  34. print(l)
  35. print('mean')
  36. mean=(s/l)
  37. print(mean)
  38.  
  39. #checking answer
  40.  
  41. vmean=np.mean(values)
  42. print(vmean)
  43.  
  44.  
  45.  
  46.  
  47. # Question 2:
  48.  
  49. # Write a program the calculates and prints the (unbiased) variance of the same
  50.  
  51. # SST data set and prints it to the screen. Re-use variables that you created
  52.  
  53. # in your answer to Question 1. Avoid using np.var(), except to check your
  54.  
  55. # work. Note that the "ddof" option in np.var() needs to be modified to compute
  56.  
  57. # the unbiased variance.
  58.  
  59. print('variance')
  60. #subtracting mean from array
  61. var=(values-mean)
  62. print(var)
  63.  
  64. #squaring values
  65. sq=(var**2)
  66. print(sq)
  67.  
  68. #taking sum of the squares
  69. s2=sum(sq)
  70. print(sq)
  71.  
  72. #finding degrees of freedom
  73. print('degrees of freedom')
  74. l2=l-1
  75. print(l2)
  76.  
  77.  
  78. #finding variance
  79. print('variance')
  80. variance=(s2/l2)
  81. print(variance)
  82.  
  83. #checking variance answer
  84. varcheck=np.var(values,ddof=1)
  85. print(varcheck)
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. # Question 3:
  93.  
  94. # Write a program the calculates and prints the standard deviation of the same
  95.  
  96. # SST data set and prints it to the screen. Avoid using np.std(), except to check
  97.  
  98. # your work.
  99.  
  100. #standard deviation is the square root of the variance
  101. stdev=np.sqrt(variance)
  102. print(stdev)
  103.  
  104. #checking answer
  105. stdev2=np.std(values, ddof=1)
  106. print(stdev2)
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. # Question 4:
  114.  
  115. # Write a program the calculates and prints the standard error of the same
  116.  
  117. # SST data set and prints it to the screen.
  118.  
  119.  
  120. #Standard error is calculated by dividing the standard deviation by the square root of the sample size
  121. print('length')
  122. print(l)
  123. #calculating the square root of the sample size
  124. sql=np.sqrt(l)
  125. print(sql)
  126.  
  127. #calculating standard error
  128. print('standard error')
  129. se=(stdev/sql)
  130. print(se)
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. # Question 5:
  140.  
  141. # Write a program that prints the z-scores of the same SST data set.
  142.  
  143. #z-score is calculated by subracting the mean from the array anf then dividing by the standard deviation
  144. #subtracting mean from array
  145. var=(values-mean)
  146. print(var)
  147.  
  148. #standard deviation
  149. stdev=np.sqrt(variance)
  150. print(stdev)
  151.  
  152. #z-score var divided by stdev
  153. print('z-score')
  154. z=(var/stdev)
  155. print(z)
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. # Question 6:
  163.  
  164. # Write a program that takes the last value from SST data set created in
  165.  
  166. # Question 1, and rounds it to the nearest tenth. Hint: help(round)
  167.  
  168. # Make sure that all of your programs in questions 1-6 still work if
  169.  
  170. # new value is added to the end of the data set.
  171. help(round)
  172. r=float(round(1.695,2))
  173. print(r)
  174.  
  175. #checking to see if 1-6 still works after adding new value to the end
  176. values = np.array([8.61, 7.575, 9.72, 2.087, 5.421, 1.659, 1.7])
  177. print (values)
  178. print('sum')
  179. s=sum(values)
  180. print(s)
  181. print('length')
  182. l=len(values)
  183. print(l)
  184. print('mean')
  185. mean=(s/l)
  186. print(mean)
  187.  
  188. #checking answer
  189.  
  190. vmean=np.mean(values)
  191. print(vmean)
  192.  
  193. print('variance')
  194. #subtracting mean from array
  195. var=(values-mean)
  196. print(var)
  197.  
  198. #squaring values
  199. sq=(var**2)
  200. print(sq)
  201.  
  202. #taking sum of the squares
  203. s2=sum(sq)
  204. print(sq)
  205.  
  206. #finding degrees of freedom
  207. print('degrees of freedom')
  208. l2=l-1
  209. print(l2)
  210.  
  211.  
  212. #finding variance
  213. print('variance')
  214. variance=(s2/l2)
  215. print(variance)
  216.  
  217. #checking variance answer
  218. varcheck=np.var(values,ddof=1)
  219. print(varcheck)
  220.  
  221.  
  222. stdev=np.sqrt(variance)
  223. print(stdev)
  224.  
  225. #checking answer
  226. stdev2=np.std(values, ddof=1)
  227. print(stdev2)
  228.  
  229.  
  230. print('length')
  231. print(l)
  232. #calculating the square root of the sample size
  233. sql=np.sqrt(l)
  234. print(sql)
  235.  
  236. #calculating standard error
  237. print('standard error')
  238. se=(stdev/sql)
  239. print(se)
  240.  
  241. var=(values-mean)
  242. print(var)
  243.  
  244. #standard deviation
  245. stdev=np.sqrt(variance)
  246. print(stdev)
  247.  
  248. #z-score var divided by stdev
  249. print('z-score')
  250. z=(var/stdev)
  251. print(z)
  252.  
  253. #still works when new value is added to the end of the array
  254.  
  255.  
  256. # Question 7:
  257.  
  258. # (Excercise 5 in Chapter 2 of Python for Everybody)
  259.  
  260. # Write a program which prompts the user for a Celsius temperature, convert the
  261.  
  262. # temperature to Fahrenheit, and print out the converted temperature.
  263.  
  264.  
  265. c= input('Celsius temperature?')
  266. print(c)
  267. print(type(c))
  268. c2=float(c)
  269. print(type(c2))
  270.  
  271. #Celsius to Fahrenheit
  272. print('Fahrenheit')
  273. f=c2*(9/5)+32
  274. print(f)
  275.  
  276.  
  277.  
  278.  
  279.  
  280. # Question 8:
  281.  
  282. # Write a program that uses input to prompt a user for their first and last names
  283.  
  284. # and then welcomes them using their initials. For example:
  285.  
  286. #
  287.  
  288. # Enter your first name: Tom
  289.  
  290. # Enter your last name: Connolly
  291.  
  292. # Hello TC!
  293.  
  294. fn=input('enter your first name')
  295. ln=input('enter your last name')
  296. #print(fn[0])
  297. #print(ln[0])
  298. print('Hello '+fn[0]+ln[0]+'!')
Add Comment
Please, Sign In to add comment