Skip to content

Instantly share code, notes, and snippets.

@kimberlythegeek
Created September 17, 2020 02:22
Show Gist options
  • Select an option

  • Save kimberlythegeek/bd594dad9bb031330a455af711d97a69 to your computer and use it in GitHub Desktop.

Select an option

Save kimberlythegeek/bd594dad9bb031330a455af711d97a69 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 13 15:35:48 2020
@author: mcali
"""
#In this activity, we will use all of our different ways of comparing vectors.
import numpy as np
import matplotlib.pyplot as plt
import json
#Define some "signal vectors"
t=np.arange(0,1,.01)
cos=np.cos(3.14*t)
sin=np.sin(3.14*t)
expo=np.exp(t)
fig = plt.figure()
plt.ion()
plt.plot(t,cos, label='cos')
plt.plot(t,sin, label='sin')
plt.plot(t,expo, label='t')
plt.legend()
plt.show()
#If you're not using Spyder, you can un-comment this line to save the graphs.
fig.savefig('figure.png')
def average(vector):
# Calculate average value of a vector to the third decimal place
# return round(np.mean(vector), 3)
return round(np.sum(vector) / len(vector), 3)
def norm(vector):
# Return norm of a vector to the third decimal place
return round(np.linalg.norm(vector), 3)
def rms(vector):
# Return RMS of a vector to the third decimal place
return round((norm(vector) / len(vector)), 3)
def std(vector):
return round(np.std(vector), 3)
def angle_between(a, b):
# Calculate angle theta between vectors a and b in degrees to the first decimal place
theta = np.arccos(a @ b / (norm(a) * norm(b)))
return round(theta * (360/(2 * np.pi)), 1)
def correlations(a, b):
# Calculate correlations between vectors a and b to the third decimal place
a_tilde = a - sum(a)/len(a)
b_tilde = b - sum(b)/len(b)
return round((a_tilde @ b_tilde) / (norm(a_tilde) * norm(b_tilde)), 3)
print('\na) Average value')
print('\tcos: {}'.format(average(cos)))
print('\tsin: {}'.format(average(sin)))
print('\tt: {}'.format(average(t)))
print('\nb) Norm')
print('\tcos: {}'.format(norm(cos)))
print('\tsin: {}'.format(norm(sin)))
print('\tt: {}'.format(norm(t)))
print('\nc) RMS')
print('\tcos: {}'.format(rms(cos)))
print('\tsin: {}'.format(rms(sin)))
print('\tt: {}'.format(rms(t)))
print('\nd) Standard Deviation')
print('\tcos: {}'.format(std(cos)))
print('\tsin: {}'.format(std(sin)))
print('\tt: {}'.format(std(t)))
print('\ne) Angles between all pairs of vectors')
print('\tcos + sin: {}'.format(angle_between(cos,sin)))
print('\tsin + t: {}'.format(angle_between(sin,t)))
print('\tcos + t: {}'.format(angle_between(cos,t)))
print('\nf) Correlations between all pairs of vectors')
print('\tcos + sin: {}'.format(correlations(cos,sin)))
print('\tsin + t: {}'.format(correlations(sin,t)))
print('\tcos + t: {}'.format(correlations(cos,t)))
# Part 2
sonnet_18 = \
[2, 1, 5, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 2, 1,
1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 2, 1,
0, 0, 1, 1, 1, 3, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0,
0, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0, 2, 1, 0, 1, 1, 0, 1, 0, 1, 2, 1, 0, 1,
1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 0, 3, 1, 0, 0, 2, 0,
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 3, 0, 1,
1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 1, 2, 2, 0, 0,
0, 0, 2, 4, 0, 1, 1, 3, 0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0]
sonnet_83 = \
[3, 0, 3, 0, 0, 0, 0, 1, 1, 1, 3, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,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, 0, 1, 0, 1, 0, 1, 0, 2, 1, 0, 2, 1, 0,
0, 2, 0, 0, 0]
print('\n2)\n\ta) Sonnets 18 and 130 are both in reference to a lover, however 18 is\n\
\tpositive and 130 seems negative. Both 18 and 83 seems to be about\n\
\tthe end of something--change, death.')
print('\n\tb)\n\t\ti) The larger the norm, the more similar the sonnet to the\n\
\t\tone used to create the dictionary, and the smaller the norm, the\n\
\t\tless similar.')
print('\n\t\tii) Measure the similiarity by subtracting the norms of the vectors.')
print('\n\t\tiii)')
print('\n\t\t|| sonnet_18 - sonnet_83 || = {}\
'.format(round(norm(sonnet_18) - norm(sonnet_83), 3)))
print('\n\t\t|| sonnet_18 - sonnet_130 || = {}\
'.format(round(norm(sonnet_18) - norm(sonnet_130), 3)))
print('\n\t\t|| sonnet_83 - sonnet_130 || = {}\
'.format(round(norm(sonnet_83) - norm(sonnet_130), 3)))
# An example usage of JSON vs text file
# Assumes filename "sonnet_18vec.json"
#
# import json
# sonnet_18 = json.loads('sonnet18vec.json')
#
# Vector data is loaded as a python list.
# You don't have to modify the content
# of the file. [0,2,3] (example) is valid JSON.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment