##cs231n notes:Python Numpy Tutorialimport numpy as np
#idctionary
d = {'cat':(6,7),'dog':(7,5),'dog2':(8,3),'dog3':(9,10)}
print(d['cat'][0])
c = sorted(d.items(),key=lambda item:item[1][0],reverse =True)
print(c)
print(c[0][1][0])
# #三种遍历方式# for k,v in d.items():# print(k,v)for i in d:
print(i) #字典的迭代,i表示key
print(i,d[i])
config = {}
config.setdefault('learning_rate', 1e-2)
config.setdefault('momentum', 0.9)
for k,v in config.items():
print(k,v)
for p in config:
print(p)
#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]
d = {}
for i in a:
d[i] = d.get(i,0)+1#如果i第一次放进来,即不存在,则d.get()=1
print(d.items())
d_ = sorted(d.items(),key=lambda item:item[1],reverse=False)
print(d_)
print(d_[0][0])
# 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()