##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()