import numpy as np
In [5]:
data = np.random.rand(2,3)
In [6]:
data
Out[6]:
array([[0.17855733, 0.28267648, 0.56366342],
       [0.86443114, 0.23546524, 0.46807879]])
In [7]:
import random
In [8]:
#Rnadom은 1 미만

dataList = [ [ random.random() for _ in range(3) ] for _ in range (2)]
In [9]:
dataList
Out[9]:
[[0.7013192258542376, 0.9613007418895336, 0.0629581450935679],
 [0.9316020590051985, 0.7235872833449181, 0.16369610888019936]]
In [10]:
data[0]
Out[10]:
array([0.17855733, 0.28267648, 0.56366342])
In [11]:
dataList[0]
Out[11]:
[0.7013192258542376, 0.9613007418895336, 0.0629581450935679]
In [12]:
data[0][1]
Out[12]:
0.28267647886733227
In [13]:
dataList[0][1]
Out[13]:
0.9613007418895336
In [14]:
data[0,1]
Out[14]:
0.28267647886733227
In [15]:
A = [ [ 10, 20], [30, 40 ]]
In [19]:
B = [[ 1, 2 ] , [3, 4]]
In [21]:
# 리스트는 덧셈하면 그냥 요소를 이어 붙여버린다.
A + B
Out[21]:
[[10, 20], [30, 40], [1, 2], [3, 4]]
In [22]:
AA = np.array(A)
BB = np.array(B)
In [24]:
# 행렬연산
AA + BB
Out[24]:
array([[11, 22],
       [33, 44]])
In [26]:
# 리스트가 길이가 2배가 됨
A * 2
Out[26]:
[[10, 20], [30, 40], [10, 20], [30, 40]]
In [27]:
AA * 2
Out[27]:
array([[20, 40],
       [60, 80]])
In [28]:
AA.shape
Out[28]:
(2, 2)
In [29]:
AA.dtype
Out[29]:
dtype('int32')
In [30]:
#벡터 연산
v1 = np.array([1,2,3])
v2 = np.array([4,5,6])
In [31]:
v1.shape
Out[31]:
(3,)
In [32]:
v2.shape
Out[32]:
(3,)
In [33]:
AA.ndim
Out[33]:
2
In [34]:
v1.ndim
Out[34]:
1
In [35]:
v1 + v2
Out[35]:
array([5, 7, 9])
In [36]:
v1 * v2
Out[36]:
array([ 4, 10, 18])
In [37]:
v1.dot(v2)
Out[37]:
32
In [38]:
# 형변환
data
Out[38]:
array([[0.17855733, 0.28267648, 0.56366342],
       [0.86443114, 0.23546524, 0.46807879]])
In [39]:
data.shape
Out[39]:
(2, 3)
In [41]:
data.reshape(3,2)
Out[41]:
array([[0.17855733, 0.28267648],
       [0.56366342, 0.86443114],
       [0.23546524, 0.46807879]])
In [42]:
AA
Out[42]:
array([[10, 20],
       [30, 40]])
In [43]:
BB
Out[43]:
array([[1, 2],
       [3, 4]])
In [44]:
# 행렬간의 곱셈
CC = np.dot(AA,BB)
In [45]:
CC
Out[45]:
array([[ 70, 100],
       [150, 220]])
In [46]:
CC = CC + 1
In [48]:
CC * 2
Out[48]:
array([[142, 202],
       [302, 442]])
In [50]:
# 행으로 브로드 캐스팅
CC + np.array([33, 44])
Out[50]:
array([[104, 145],
       [184, 265]])
In [51]:
# 전치행렬 

data.T
Out[51]:
array([[0.17855733, 0.86443114],
       [0.28267648, 0.23546524],
       [0.56366342, 0.46807879]])
In [52]:
data
Out[52]:
array([[0.17855733, 0.28267648, 0.56366342],
       [0.86443114, 0.23546524, 0.46807879]])
In [54]:
# slicing

list1 = [ n for n in range(10, 70, 10)]
list1
Out[54]:
[10, 20, 30, 40, 50, 60]
In [94]:
# reshape

AA = np.array(list1)
AA
Out[94]:
array([10, 20, 30, 40, 50, 60])
In [95]:
BB = AA.reshape(3,2)
In [96]:
BB
Out[96]:
array([[10, 20],
       [30, 40],
       [50, 60]])
In [59]:
BB[0][1]
Out[59]:
20
In [60]:
BB[: , 0]
Out[60]:
array([10, 30, 50])
In [61]:
BB[ 0 : -1 , 1:2]
Out[61]:
array([[20],
       [40]])
In [97]:
BB[:,:]
Out[97]:
array([[10, 20],
       [30, 40],
       [50, 60]])
In [98]:
CC = BB
In [99]:
CC[0][0] = 122
In [100]:
# 값이 바뀌었다..!
BB
Out[100]:
array([[122,  20],
       [ 30,  40],
       [ 50,  60]])
In [110]:
# 깊은 복사
CC = BB.copy()
BB
Out[110]:
array([[1112321, 1222222],
       [     30,      40],
       [     50,      60]])
In [111]:
CC[0][1] = 123
In [112]:
BB
Out[112]:
array([[1112321, 1222222],
       [     30,      40],
       [     50,      60]])
In [114]:
list2 = [ n for n in range(10, 90, 10)]
In [115]:
AA = np.array(list2).reshape(2,4) 
In [116]:
AA
Out[116]:
array([[10, 20, 30, 40],
       [50, 60, 70, 80]])
In [117]:
# 속도가 느리다!
it = np.nditer(AA, flags = ['multi_index'], op_flags = ['readwrite'])
In [118]:
while not it.finished :
    idx = it.multi_index
    print(AA[idx])
    it.iternext()
10
20
30
40
50
60
70
80
In [119]:
## 행 추가에 대해서는 추가되는 행렬의 Shape을 모 행렬과 일치 시켜줘야 한다.

DD = np.concatenate((AA, np.array([ 1,2,3,4 ]) ), axis = 0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-119-436779f6c7a7> in <module>
----> 1 DD = np.concatenate((AA, np.array([ 1,2,3,4 ]) ), axis = 0)

ValueError: all the input arrays must have same number of dimensions
In [121]:
BB = np.array([ 1,2,3,4 ])
In [124]:
BB = BB.reshape(1,4)
BB
Out[124]:
array([[1, 2, 3, 4]])
In [125]:
CC = np.concatenate((AA, BB), axis = 0 )
In [126]:
CC
Out[126]:
array([[10, 20, 30, 40],
       [50, 60, 70, 80],
       [ 1,  2,  3,  4]])
In [133]:
dd = np.array([100, 200, 300])
dd.shape
Out[133]:
(3,)
In [131]:
# (3, )의 쉐잎을 (3, 1) 로 바꿔야 열에 추가할 수가 있다.
dd = dd.reshape(3, 1)
EE = np.concatenate((CC, dd), axis = 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-131-8e610f1418dc> in <module>
----> 1 EE = np.concatenate((CC, dd), axis = 1)

ValueError: all the input arrays must have same number of dimensions
In [132]:
EE
Out[132]:
array([[ 10,  20,  30,  40, 100],
       [ 50,  60,  70,  80, 200],
       [  1,   2,   3,   4, 300]])
In [134]:
data = np.loadtxt("C:/Images/csv/12.csv", delimiter = ',', dtype = np.int32 )
In [135]:
rows = data[ :, 0:1]
In [136]:
rows[0 : 5]
Out[136]:
array([[0],
       [0],
       [0],
       [0],
       [0]])
In [137]:
np.max(rows)
Out[137]:
63
In [138]:
np.min(rows)
Out[138]:
0
In [142]:
np.zeros([2,3], dtype = np.uint8)
Out[142]:
array([[0, 0, 0],
       [0, 0, 0]], dtype=uint8)
In [143]:
np.ones([3,3])
Out[143]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
In [145]:
float_arr = AA.astype(np.float64)


+ Recent posts