-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_of_least_squares_ii.py
150 lines (89 loc) · 5.09 KB
/
copy_of_least_squares_ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""Copy of Least_squares_II.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/116hWgyE5KlmyRq0lnQKe9aMyVFG1SO3C
#**Lab 7 - Least squares II**
Enter your code in the spaces provided. Do not change any of the variable names or function names that are already provided for you. In places where we specify the name of the return value, make sure that your code produces the a value with the correct name.
"""
# Do not edit this cell.
LabID="Lab7"
try:
from graderHelp import ISGRADEPLOT
except ImportError:
ISGRADEPLOT = True
"""**Enter your name, section number, and BYU NetID**"""
# Enter your first and last names in between the quotation marks.
first_name="Madison"
last_name="Wozniak"
# Enter your Math 215 section number in between the quotation marks.
section_number="001"
# Enter your BYU NetID in between the quotation marks. NOT YOUR BYU ID NUMBER!
BYUNetID="mwozniak"
"""**Import the data set**
The simplest way to load the data into Colab is to first download it as a .csv file to your local computer by clicking the link
https://drive.google.com/uc?export=download&id=1iFTaBmnv1X66BO9cV_RP7OxvUr9dNJ-l
This will allow you to download the data as a .csv file. In the top left corner of this screen you should see a little file folder icon. Selecting it opens a new window to the left of the notebook with three tabs: "Upload", "Refresh", and "Mount Drive". Select "Upload". This should bring up a window that allows you to select the file "Lab7data" from your local machine, which will upload the file to your notebook. You will need to do this again if you decide to close your notebook and reopen it at a later time.
Once you've uploaded your file, convert it to a NumPy array called "signal_data" by executing the following cell.
"""
import numpy as np
import pandas as pd
df = pd.read_csv('Lab7data.csv')
signal_data=df.values
signal_data
"""**Problem 1**"""
T=signal_data[:,0] # Replace the value of 0 with the NumPy vector that contains all of the time values in the array signal_data (the first column).
T.shape
Y=signal_data[:,1]
Y.shape # Replace the value of 0 with the NumPy vector that contains all of the signal amplitude values in the array signal_data (the second column).
import matplotlib.pyplot as plt
plt.plot(T,Y)
# Create the plot here of the data in the vectors T and Y.
"""**Problem 2**"""
# This function returns the row [1,cos(t),sin(t), cos(2*t), sin(2*t) , ... , cos(n*t), sin(n*t)] of our matrix X.
def row_func(t,n):
L=[j(i*t) for i in range(1,n+1) for j in [np.cos,np.sin]]
return np.array(L)
"""**Problem 3**"""
# This function returns the matrix X, which we call the design matrix.
def design_matrix(n):
matrix = [row_func(T[j],n) for j in range(len(T))]
return np.array(matrix)
design_matrix(10).shape
design_matrix(2)
"""**Problem 4**"""
X2= design_matrix(2)
# Replace the value of 0 with the NumPy array that is returned from the function call design_matrix(2).
"""**Problem 5**"""
# Replace all of the 0 values with the NumPy matrices and vectors requested in Problem 5.
normal_coef2=np.matmul(np.transpose(X2),X2)
normal_vect2=np.matmul(np.transpose(X2),Y)
beta2=np.linalg.solve(normal_coef2,normal_vect2)
"""**Problem 6**"""
# This is our function which approximates the signal strength when n=2.
def f2(t):
answer = np.dot(beta2,row_func(t,2))
return answer
"""**Problem 7**"""
vf2 = np.vectorize(f2)
plt.plot(T,Y,'r.')
plt.plot(T,vf2(T),'b-')
plt.show()
"""**Problem 8**"""
MSE2 = (np.linalg.norm(np.matmul(X2,beta2)))/629
"""**Problem 9**"""
# Replace the 0 values with the values requested in Problem 9. Remember to copy the decimal values from your practice notebook, not the formulas you used to compute them.
MSE10=0.008013784777864817
pred10=0.06031513000830744
"""**Problem 10**"""
# Replace the 0 values with the values requested in Problem 10. Remember to copy the decimal values from your practice notebook, not the formulas you used to compute them.
MSE100=0.008762539346357083
pred100=0.04491557450549677
"""**Problem 11**"""
# Replace the 0 values with the values requested in Problem 11. Remember to copy the decimal values from your practice notebook, not the formulas you used to compute them.
MSE1000=0.02024558837125755
pred1000=-1.0395615804865699
"""**STOP! BEFORE YOU SUBMIT THIS LAB:** Go to the "Runtime" menu at the top of this page, and select "Restart and run all". If any of the cells produce error messages, you will either need to fix the error(s) or delete the code that is causing the error(s). Then use "Restart and run all" again to see if there are any new errors. Repeat this until no new error messages show up.
**You are not ready to submit until you are able to select "Restart and run all" without any new error messages showing up. Your code will not be able to be graded if there are any error messages.**
To submit your lab for grading you must first download it to your compute as .py file. In the "File" menu select "Download .py". The resulting file can then be uploaded to http://www.math.byu.edu:30000 for grading.
"""