TODO15 Create the CosineSimilarity by following the below instructions: Define the class CosineSimilarity using the class keyword. Define the __init__() method which takes in three arguments self, x, and z and saves them as class variables. These two arguments will be the vectors in which the cosine similarity will be computed. Define a method called compute_angle() that takes in no arguments other than self (remember self is a required argument for methods). Within the compute_angle() method compute the angle between two vectors and return the answer in degrees. Use the following equation to compute the angle between two vector. $$ \theta = \text{arccos}(\frac{\mathbf{x} \cdot \mathbf{z}}{||\mathbf{x}||_2 ||\mathbf{z}||_2}) $$ Hint 1: Recall from the quiz that $\cdot$ represents the dot product and $||\cdot||_2$ represents the L2 norm $$ ||\mathbf{x}||_2 = \sqrt{x_1^2 + x_2^2 + ... + x_n^2} = \sqrt{\mathbf{x} \cdot \mathbf{x}} $$ where a vector dotted with itself is the same as squaring the vector. Hint 2: Use np.sqrt() to compute the square root and np.arccos() to compute arccos. Hint 3: The output of np.arccos() is in radians! Be sure to use np.degrees() to convert your output into degrees. If you don't do this you will fail the test. Hint 4: Due to rounding errors your answer should be close to ~180 degrees. # TODO 15.1 x = np.array([1, 1]) z = np.array([-2, -2]) cosin_sim = CosineSimilarity(x, z) angle = cosin_sim.compute_angle() print(f"angle output: {angle}") todo_check([ (np.isclose(angle,180.0),'The value of the angle is not approximately 180 degrees! Make sure you converted from radians to degrees.') ])
TODO15 Create the CosineSimilarity by following the below instructions: Define the class CosineSimilarity using the class keyword. Define the __init__() method which takes in three arguments self, x, and z and saves them as class variables. These two arguments will be the vectors in which the cosine similarity will be computed. Define a method called compute_angle() that takes in no arguments other than self (remember self is a required argument for methods). Within the compute_angle() method compute the angle between two vectors and return the answer in degrees. Use the following equation to compute the angle between two vector. $$ \theta = \text{arccos}(\frac{\mathbf{x} \cdot \mathbf{z}}{||\mathbf{x}||_2 ||\mathbf{z}||_2}) $$ Hint 1: Recall from the quiz that $\cdot$ represents the dot product and $||\cdot||_2$ represents the L2 norm $$ ||\mathbf{x}||_2 = \sqrt{x_1^2 + x_2^2 + ... + x_n^2} = \sqrt{\mathbf{x} \cdot \mathbf{x}} $$ where a vector dotted with itself is the same as squaring the vector. Hint 2: Use np.sqrt() to compute the square root and np.arccos() to compute arccos. Hint 3: The output of np.arccos() is in radians! Be sure to use np.degrees() to convert your output into degrees. If you don't do this you will fail the test. Hint 4: Due to rounding errors your answer should be close to ~180 degrees. # TODO 15.1 x = np.array([1, 1]) z = np.array([-2, -2]) cosin_sim = CosineSimilarity(x, z) angle = cosin_sim.compute_angle() print(f"angle output: {angle}") todo_check([ (np.isclose(angle,180.0),'The value of the angle is not approximately 180 degrees! Make sure you converted from radians to degrees.') ])
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
TODO15
- Create the CosineSimilarity by following the below instructions:
- Define the class CosineSimilarity using the class keyword.
- Define the __init__() method which takes in three arguments self, x, and z and saves them as class variables. These two arguments will be the
vectors in which the cosine similarity will be computed. - Define a method called compute_angle() that takes in no arguments other than self (remember self is a required argument for methods).
- Within the compute_angle() method compute the angle between two vectors and return the answer in degrees. Use the following equation to compute the angle between two vector. $$ \theta = \text{arccos}(\frac{\mathbf{x} \cdot \mathbf{z}}{||\mathbf{x}||_2 ||\mathbf{z}||_2}) $$
- Hint 1: Recall from the quiz that $\cdot$ represents the dot product and $||\cdot||_2$ represents the L2 norm $$ ||\mathbf{x}||_2 = \sqrt{x_1^2 + x_2^2 + ... + x_n^2} = \sqrt{\mathbf{x} \cdot \mathbf{x}} $$ where a vector dotted with itself is the same as squaring the vector.
- Hint 2: Use np.sqrt() to compute the square root and np.arccos() to compute arccos.
- Hint 3: The output of np.arccos() is in radians! Be sure to use np.degrees() to convert your output into degrees. If you don't do this you will fail the test.
- Hint 4: Due to rounding errors your answer should be close to ~180 degrees.
# TODO 15.1
x = np.array([1, 1])
z = np.array([-2, -2])
cosin_sim = CosineSimilarity(x, z)
angle = cosin_sim.compute_angle()
print(f"angle output: {angle}")
todo_check([
(np.isclose(angle,180.0),'The value of the angle is not approximately 180 degrees! Make sure you converted from radians to degrees.')
])
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education