Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- # Question 1 (similar to class):
- # An oceanographer is studying wintertime sea surface temperature at a site on
- # the east coast. For the past six years, she obtains the following values:
- #
- # [8.61, 7.575, 9.72, 2.087, 5.421, 1.659]
- #
- # In the space below, write a program that
- # calculates the mean, and prints it on the screen. Use the built-in sum() and
- # len() functions. Avoid using np.mean(), except to check your work. Use
- # descriptive variable names -- see Section 2.12 of Python for Everybody.
- values = np.array([8.61, 7.575, 9.72, 2.087, 5.421, 1.659])
- print (values)
- print('sum')
- s=sum(values)
- print(s)
- print('length')
- l=len(values)
- print(l)
- print('mean')
- mean=(s/l)
- print(mean)
- #checking answer
- vmean=np.mean(values)
- print(vmean)
- # Question 2:
- # Write a program the calculates and prints the (unbiased) variance of the same
- # SST data set and prints it to the screen. Re-use variables that you created
- # in your answer to Question 1. Avoid using np.var(), except to check your
- # work. Note that the "ddof" option in np.var() needs to be modified to compute
- # the unbiased variance.
- print('variance')
- #subtracting mean from array
- var=(values-mean)
- print(var)
- #squaring values
- sq=(var**2)
- print(sq)
- #taking sum of the squares
- s2=sum(sq)
- print(sq)
- #finding degrees of freedom
- print('degrees of freedom')
- l2=l-1
- print(l2)
- #finding variance
- print('variance')
- variance=(s2/l2)
- print(variance)
- #checking variance answer
- varcheck=np.var(values,ddof=1)
- print(varcheck)
- # Question 3:
- # Write a program the calculates and prints the standard deviation of the same
- # SST data set and prints it to the screen. Avoid using np.std(), except to check
- # your work.
- #standard deviation is the square root of the variance
- stdev=np.sqrt(variance)
- print(stdev)
- #checking answer
- stdev2=np.std(values, ddof=1)
- print(stdev2)
- # Question 4:
- # Write a program the calculates and prints the standard error of the same
- # SST data set and prints it to the screen.
- #Standard error is calculated by dividing the standard deviation by the square root of the sample size
- print('length')
- print(l)
- #calculating the square root of the sample size
- sql=np.sqrt(l)
- print(sql)
- #calculating standard error
- print('standard error')
- se=(stdev/sql)
- print(se)
- # Question 5:
- # Write a program that prints the z-scores of the same SST data set.
- #z-score is calculated by subracting the mean from the array anf then dividing by the standard deviation
- #subtracting mean from array
- var=(values-mean)
- print(var)
- #standard deviation
- stdev=np.sqrt(variance)
- print(stdev)
- #z-score var divided by stdev
- print('z-score')
- z=(var/stdev)
- print(z)
- # Question 6:
- # Write a program that takes the last value from SST data set created in
- # Question 1, and rounds it to the nearest tenth. Hint: help(round)
- # Make sure that all of your programs in questions 1-6 still work if
- # new value is added to the end of the data set.
- help(round)
- r=float(round(1.695,2))
- print(r)
- #checking to see if 1-6 still works after adding new value to the end
- values = np.array([8.61, 7.575, 9.72, 2.087, 5.421, 1.659, 1.7])
- print (values)
- print('sum')
- s=sum(values)
- print(s)
- print('length')
- l=len(values)
- print(l)
- print('mean')
- mean=(s/l)
- print(mean)
- #checking answer
- vmean=np.mean(values)
- print(vmean)
- print('variance')
- #subtracting mean from array
- var=(values-mean)
- print(var)
- #squaring values
- sq=(var**2)
- print(sq)
- #taking sum of the squares
- s2=sum(sq)
- print(sq)
- #finding degrees of freedom
- print('degrees of freedom')
- l2=l-1
- print(l2)
- #finding variance
- print('variance')
- variance=(s2/l2)
- print(variance)
- #checking variance answer
- varcheck=np.var(values,ddof=1)
- print(varcheck)
- stdev=np.sqrt(variance)
- print(stdev)
- #checking answer
- stdev2=np.std(values, ddof=1)
- print(stdev2)
- print('length')
- print(l)
- #calculating the square root of the sample size
- sql=np.sqrt(l)
- print(sql)
- #calculating standard error
- print('standard error')
- se=(stdev/sql)
- print(se)
- var=(values-mean)
- print(var)
- #standard deviation
- stdev=np.sqrt(variance)
- print(stdev)
- #z-score var divided by stdev
- print('z-score')
- z=(var/stdev)
- print(z)
- #still works when new value is added to the end of the array
- # Question 7:
- # (Excercise 5 in Chapter 2 of Python for Everybody)
- # Write a program which prompts the user for a Celsius temperature, convert the
- # temperature to Fahrenheit, and print out the converted temperature.
- c= input('Celsius temperature?')
- print(c)
- print(type(c))
- c2=float(c)
- print(type(c2))
- #Celsius to Fahrenheit
- print('Fahrenheit')
- f=c2*(9/5)+32
- print(f)
- # Question 8:
- # Write a program that uses input to prompt a user for their first and last names
- # and then welcomes them using their initials. For example:
- #
- # Enter your first name: Tom
- # Enter your last name: Connolly
- # Hello TC!
- fn=input('enter your first name')
- ln=input('enter your last name')
- #print(fn[0])
- #print(ln[0])
- print('Hello '+fn[0]+ln[0]+'!')
Add Comment
Please, Sign In to add comment