-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_of_lecture2worksheet.py
More file actions
123 lines (81 loc) · 3.02 KB
/
copy_of_lecture2worksheet.py
File metadata and controls
123 lines (81 loc) · 3.02 KB
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
# -*- coding: utf-8 -*-
"""Copy of Lecture2Worksheet.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1OCQsK6XZuccHskVgMLi2ozG-_sRDU_o1
#Week 2 Worksheet
###Please make a copy of this worksheet and work on it as we go through lecture.
*Author: Collin Duong* \
*Term: Fa2025*
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
letters = pd.Series(['A', 'B', 'C', "D", "E"])
numbers = pd.Series([1, 2, 3, 4, 5])
details = pd.Series(["big", "big", "small", "small", "small"])
df = pd.DataFrame({"letters": letters, "numbers": numbers, "details": details})
df
"""###Problem 1
Access the column and index attribtues from the dataframe and save them to the following two variables.
"""
column_answer = df.columns
index_answer = df.index
"""### Problem 2
* Use iloc to retreive the second row of the dataframe.
* Use iloc to retrieve the 1st row and 3rd column of the dataframe.
* Use iloc to retrieve the 3rd column of the dataframe.
"""
df.iloc[1]
df.iloc[0,2]
df.iloc[:,2]
"""###Problem 3
* Extract the numbers column from the dataframe using regular column extraction.
* Extract the numbers column from the dataframe using loc.
"""
df.numbers
df.loc[:,"numbers"]
"""###Problem 4
Extract all rows that have a number in the numbers column greater than 2.
"""
df.loc[df['numbers'] > 2]
#df.tail(3)
"""###Problem 5
Extract the row with the index "a" using loc. Also observe the following cell to see how to change the index in a DataFrame.
"""
# Do not modify this cell
df2 = df.copy()
df2.set_index("letters", inplace=True)
df2
df2.loc["A"]
"""### Problem 6
Compute the dot product between the following vectors using numpy.
$$\begin{bmatrix}1\\2\\3\end{bmatrix}, \begin{bmatrix}1\\2\\3\end{bmatrix}$$
Hint: You can create a vector with `np.array()`.
"""
v1 = np.array([1, 2, 3])
v2 = np.array([1, 2, 3])
dot_product = np.dot(v1, v2)
print(dot_product)
"""###Problem 7
Compute the cosine of the angle between the two vectors in the previous problem using numpy methods.
Hint: The formula is in the slides.
"""
np.dot(v1,v2)/(np.linalg.norm(v1)*np.linalg.norm(v2))
"""###Problem 8
Compute the projection (shadow) of the first vector described in problem six onto the second vector described in that problem. Use numpy methods.
Hint: This formula is also in the slides.
"""
np.dot(v2,v1)/np.dot(v1,v1) * v1
"""###Problem 9
You see the following function inputs and outputs. $$$$
$f(\begin{bmatrix}1\\0\\0\end{bmatrix})=\begin{bmatrix}1\\2\\3\end{bmatrix}\hspace{1cm}f(\begin{bmatrix}0\\1\\0\end{bmatrix})=\begin{bmatrix}7\\4\\2\end{bmatrix}\hspace{1cm}f(\begin{bmatrix}0\\0\\1\end{bmatrix})=\begin{bmatrix}3\\0\\1\end{bmatrix}$ $$$$
Use this information to find a matrix representation of f. Then compute $f(\begin{bmatrix}5\\6\\7\end{bmatrix})$ using numpy methods.
Hint: You can create a numpy matrix by doing the following:
`np.array(row1, row2, row3])`
"""
np.array([1, 0, 0])
np.array([1, 2, 3])
np.array([0, 1, 0])
np.array([7, 4, 2])