##cs231n notes:Python Numpy Tutorial#idctionary
d = {'cat':'cute','dog':'furry'}
#三种遍历方式for k,v in d.items():
print(k,v)
for i in d:
print(i,d[i])
cat cute
dog furry
cat cute
dog furry
#get(key[, default])¶# Return the value for key if key is in the dictionary, else default. # If default is not given, it defaults to None, so that this method never raises a KeyError.
d['monkey'] = 'wet'
print(d.get('fish')) # None
print(d) #get 并不会让字典里面的key-value增加
print(d.get('monkey', 'N/A')) # wetdel d['monkey']
print(d.get('monkey', 'N/A')) # N/A
print(d)
# sorted(iterable,key,reverse)# 其中iterable表示可以迭代的对象,例如可以是dict.items()、dict.keys()等,key是一个函数,用来选取参与比较的元素,# reverse则是用来指定排序是倒序还是顺序,reverse=true则是倒序,reverse=false时则是顺序,默认时reverse=false。# 根据value排序
a = [9,4,9,9,3,4,3,6,7,6,9]
d = {}
for i in a:
d[i] = d.get(i,1)+1#如果i第一次放进来,即不存在,则d.get()=1
sorted(d.items(),key=lambda item:item[1],reverse=False)
# distancesimport numpy as np
from scipy.spatial.distance import pdist,squareform
x = np.array([[0,1],[1,0],[2,0]])
print(x)
d = pdist(x,'euclidean') #计算(2范数)欧几里德距离#Computes the distance between m points using Euclidean distance (2-norm) as the distance metric between the points. The points are arranged as m n-dimensional row vectors in the matrix X.
print(d)
[[0 1]
[1 0]
[2 0]]
[ 1.41421356 2.23606798 1. ]
# plottingimport matplotlib.pyplot as plt
x = np.arange(0, 8, 0.1)
y = np.sin(x)
plt.plot(x,y)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine')
plt.legend(['sine'])
plt.show()