728x90
반응형
In [4]:
Z = np.zeros(10,[('position',[('x',float,1),
('y',float,1)]),
('color', [('r',float,1),
('g',float,1),
('b',float,1)])])
Z
Out[4]:
In [6]:
Z = np.random.random((10,2))
# np.atleat_2d : View inputs as arrays with at least two dimensions.
X,Y = np.atleast_2d(Z[:,0],Z[:,1])
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print(D)
#or
import scipy
import scipy.spatial
Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
print(D)
In [7]:
Z = np.arange(10,dtype=np.float32)
Z = Z.astype(np.int32,copy=False)
# copy = False : the input array is returned instead of a copy.
print(Z)
In [13]:
from io import StringIO
# fake file
s = StringIO("""1, 2, 3, 4, 5\n
6, , , 7, 8\n
, , 9,10,11\n""")
Z = np.genfromtxt(s,delimiter=",",dtype=np.int)
print(Z)
# genfromtxt : Load data from a text file, with missing values handled as specified.
In [16]:
Z = np.arange(9).reshape(3,3)
# np.ndenumerate : Multidimensional index iterator. ( 다차원 index 반환)
for index, value in np.ndenumerate(Z):
print(index,value)
for index in np.ndindex(Z.shape):
print(index,Z[index])
# np.ndindex : An N-dimensional iterator object to index arrays. ( 다차원 index )
In [19]:
X,Y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
D = np.sqrt(X*X+Y*Y)
sigma,mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print(G)
In [26]:
n = 10
p = 3
Z = np.zeros((n,n))
np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
print(Z)
# np.put : Replaces specified elements of an array with given values.
# np.random.choice : Generates a random sample from a given 1-D array
In [30]:
X = np.random.rand(5,10)
Y = X - X.mean(axis=1,keepdims=True) # axis=1은 행선택 , keepdims로 차원 유지
print(Y)
In [45]:
Z = np.random.randint(0,10,(3,3))
print(Z)
print(Z[Z[:,1].argsort()])
## 행의 1번째 성분의 크기를 보고 순서대로 정렬한다. ( 3,7,1 -> 1, 3, 7)
In [41]:
Z = np.random.randint(0,3,(3,10))
print(Z)
print((~Z.any(axis=0)).any())
# null 인 column이 있는지 ( 전부 0)
728x90
반응형