123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 #!/usr/bin/env python# coding: utf-8 import numpy as np np_array = np.array([3, 33, 333]) #create rank1 array print(type(np_array))print(np_array)print(np_array.shape)print(np_array[0], np_array[1], np_array[2])print(np_array)another = np.array([[11,12,13], [21,22,23]]) print(another)print("shape of another {}".format(another.shape)) another[0][2]np.zeros((3,3)) np.full((3,3), 9.0) np.eye(3,3) np.ones((1,2)) np.ones((1,2)).shape #rank 2 matrix np.random.random((2,2)) # Rank 2 array of shape (3, 4)an_array = np.array([[11,12,13,14], [21,22,23,24], [31,32,33,34]])print(an_array) a_slice = an_array[:2, 1:3] # copy by referrencea_slice[0,0] = 123print(a_slice) a_slice = np.array(an_array[:2, 1:3]) #call by valuea_slice[0,0] = 321print(a_slice) row_rank1 = an_array[1, :]print(row_rank1, row_rank1.shape) row_rank2 = an_array[1:2, :] #this gives 2d arrayprint(row_rank2, row_rank2.shape) an_array = np.array([[11,12,13], [21,22,23], [31,32,33],[41,42,43] ]) an_array col_indices = np.array([0, 1, 2, 0])print(col_indices) row_indices = np.arange(4)print(row_indices) #Examine the pairings of row_indices and col_indices.for row, col in zip( row_indices, col_indices): print(row, col) # Select one element from each rowan_array[row_indices, col_indices] an_array[row_indices, col_indices] += 100000an_array an_array = np.array([[11,12], [21,22], [31, 32]]) an_array # boolean values will be returned as a listfilter = (an_array > 15)filter an_array[filter] #better wayan_array[an_array > 15] an_array[an_array > 25] = 25 an_array ## Data type of an element in array is matter!!ex1 = np.array([11, 12])ex1.dtype ex2 = np.array([11.0, 12.0])ex2.dtype ex3 = np.array([11,12], dtype=np.int64)ex3.dtype ex4 = np.array([11.1, 12.7], dtype = np.int64)ex4.dtype ex5 = np.array([ 11, 12 ], dtype = np.int64)ex5.dtypeex5 ## ndarray operation x = np.array([[ 111,112 ], [121,122 ]], dtype = np.int)y = np.array([[ 211.1,212.1 ], [221.1,222.1 ]], dtype = np.int)print(x)print()print(y) x + y np.add(x,y) np.sqrt(x) np.exp(x) cs
import numpy as np
np_array = np.array([3, 33, 333]) #create rank1 array
print(type(np_array))
print(np_array)
In [18]:
print(np_array.shape)
In [19]:
print(np_array[0], np_array[1], np_array[2])
In [20]:
print(np_array)
In [24]:
another = np.array([[11,12,13],
[21,22,23]])
print(another)
print("shape of another {}".format(another.shape))
In [26]:
another[0][2]
Out[26]:
In [31]:
np.zeros((3,3))
Out[31]:
In [32]:
np.full((3,3), 9.0)
Out[32]:
In [33]:
np.eye(3,3)
Out[33]:
In [35]:
np.ones((1,2))
Out[35]:
In [36]:
np.ones((1,2)).shape #rank 2 matrix
Out[36]:
In [37]:
np.random.random((2,2))
Out[37]:
In [3]:
# Rank 2 array of shape (3, 4)
an_array = np.array([[11,12,13,14], [21,22,23,24], [31,32,33,34]])
print(an_array)
In [44]:
a_slice = an_array[:2, 1:3] # copy by referrence
a_slice[0,0] = 123
print(a_slice)
In [47]:
a_slice = np.array(an_array[:2, 1:3]) #call by value
a_slice[0,0] = 321
print(a_slice)
In [8]:
row_rank1 = an_array[1, :]
print(row_rank1, row_rank1.shape)
In [9]:
row_rank2 = an_array[1:2, :] #this gives 2d array
print(row_rank2, row_rank2.shape)
In [11]:
an_array = np.array([[11,12,13], [21,22,23], [31,32,33],[41,42,43] ])
an_array
In [13]:
col_indices = np.array([0, 1, 2, 0])
print(col_indices)
row_indices = np.arange(4)
print(row_indices)
In [14]:
#Examine the pairings of row_indices and col_indices.
for row, col in zip( row_indices, col_indices):
print(row, col)
In [15]:
# Select one element from each row
an_array[row_indices, col_indices]
Out[15]:
In [16]:
an_array[row_indices, col_indices] += 100000
an_array
In [24]:
an_array = np.array([[11,12], [21,22], [31, 32]])
In [19]:
an_array
Out[19]:
In [20]:
# boolean values will be returned as a list
filter = (an_array > 15)
filter
Out[20]:
In [21]:
an_array[filter]
Out[21]:
In [22]:
#better way
an_array[an_array > 15]
Out[22]:
In [25]:
an_array[an_array > 25] = 25
an_array
Out[25]:
In [26]:
## Data type of an element in array is matter!!
ex1 = np.array([11, 12])
ex1.dtype
Out[26]:
In [27]:
ex2 = np.array([11.0, 12.0])
ex2.dtype
Out[27]:
In [28]:
ex3 = np.array([11,12], dtype=np.int64)
ex3.dtype
Out[28]:
In [29]:
ex4 = np.array([11.1, 12.7], dtype = np.int64)
ex4.dtype
Out[29]:
In [31]:
ex5 = np.array([ 11, 12 ], dtype = np.int64)
ex5.dtype
ex5
Out[31]:
In [32]:
## ndarray operation
x = np.array([[ 111,112 ], [121,122 ]], dtype = np.int)
y = np.array([[ 211.1,212.1 ], [221.1,222.1 ]], dtype = np.int)
print(x)
print()
print(y)
In [33]:
x + y
Out[33]:
In [34]:
np.add(x,y)
Out[34]:
In [35]:
np.sqrt(x)
Out[35]:
In [36]:
np.exp(x)
Out[36]:
In [ ]:
'Python Library > Numpy' 카테고리의 다른 글
Day 4. Image Processing with a numpy -02 (0) | 2019.06.12 |
---|---|
Day 4. Image Processing with a numpy - 01 (0) | 2019.06.12 |
Day 4. Numpy02 (0) | 2019.06.12 |