728x90
반응형
In [9]:
Z = np.random.uniform(0,1,10)
z = 0.5
m = Z.flat[np.abs(Z-z).argmin()]
print(m)
# flat은 펴주는 것
In [31]:
A = np.arange(3).reshape(1,3)
B = np.arange(3).reshape(3,1)
it = np.nditer([A,B,None])
for x,y,z in it: z[...] = x+y
print(it.operands[2])
In [39]:
Z = np.ones(10)
a = np.random.randint(0,len(Z),20)
np.add.at(Z,a,1)
print(a)
print(Z)
In [42]:
X = [1,2,3,4,5,6]
I = [3,4,5,6,7,8]
F = np.bincount(I,X)
# I = Index라서 3번째부터 X의 원소가 들어감
print(F)
In [48]:
img = np.random.randint(0,2,(16,16,3)).astype(np.ubyte)
F = img[...,0]*(256*256) + img[...,1]*256 + img[...,2]
print(len(np.unique(F)))
In [50]:
A = np.random.randint(0,10,(3,4,3,4))
A.sum(axis=(-2,-1))
Out[50]:
In [79]:
D = np.random.uniform(0,1,100)
S = np.random.randint(0,10,100)
import pandas as pd
pd.Series(D).groupby(S).mean()
Out[79]:
In [55]:
A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5))
# 느림
np.diag(np.dot(A,B))
# 가장 빠른 버젼
np.einsum('ij,ji->i',A,B)
Out[55]:
In [75]:
import numpy as np
Z = np.array([1,2,3,4,5])
nz = 3
Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
# [::n] 처음부터 끝까지 n간격으로
Z0[::nz+1] = Z
print(Z0)
728x90
반응형