numpy基本練習(三)

# translation of type
z = np.arange(10,dtype=np.int32)
print(z.dtype)
z = z.astype(np.float32)
print(z.dtype)

# print index and element
z = np.arange(9).reshape(3,3)
for index,value in np.ndenumerate(z):
    print(index,value)

# Arrange by a column of an array
z = np.random.randint(0,10,(3,3))
print(z)
print(z[z[:,1].argsort()])

# Count the number of occurrences of each value
z = np.array([33,44,44,3,3,89])
np.bincount(z)

# Sum the last two dimensions of four-dimensional data
z = np.random.randint(0,10,(4,4,4,4))
res = z.sum(axis=(-2,-1))
print(res)

# exchange Two lines in matrix
z = np.arange(25).reshape(5,5)
z[[0,1]] = z[[1,0]]

# find the most frequent element
z = np.random.randint(0,10,50)
print(np.bincount(z).argmax())

# topK
z = np.arange(10000)
np.random.shuffle(z)
n = 5
print(z[np.argpartition(-z,n)[:n]])

# if equal or not
a = np.array([4,4,4,9])
b = np.array([4,3,4,9])
print(np.all(a==b))
print(np.any(a==b))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章