a = [1,2,3]
b = [4,5,6]
print(a+b)# sad, [1, 2, 3, 4, 5, 6]
import numpy as np
a = [1,2,3]
b = [4,5,6]
print("list:", a+b)
a = np.array(a)
b = np.array(b)
print("np.array:", a+b)#good
c = [[[1,2,3], [4,5,6]]]
c = np.array(c)
print("ndim:{}, shape:{}".format(c.ndim, c.shape))
list: [1, 2, 3, 4, 5, 6]
np.array: [5 7 9]
ndim:3, shape:(1, 2, 3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-352-a3049ecc4fe2> in <module>()
----> 1 a = np.matrix([[[1,2,3,4]]])
C:\Users\frhyme\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in __new__(subtype, data, dtype, copy)
272 shape = arr.shape
273 if (ndim > 2):
--> 274 raise ValueError("matrix must be 2-dimensional")
275 elif ndim == 0:
276 shape = (1, 1)
ValueError: matrix must be 2-dimensional
a1 = np.array([[1, 2, 3], [4, 5, 6]])
print("a1+a1:\n", a1 + a1) # element-wise add
print("--" * 10)
print("a1-a1:\n", a1 - a1) # element-wise substraction
print("--" * 10)
print("a1*a1:\n", a1 * a1) # element-wise multi
print("--" * 10)
print("a1/a1:\n", a1 / a1) # element-wise div
print("--" * 10)
print("np.multiply(a1, a1)\n", np.multiply(a1, a1))
print("np.dot(a1, a1)\n", np.dot(a1, a1.T))
print("np.dot(a1, a1)\n", np.dot(a1.T, a1))
import time
a = list(reversed(range(0, 10000000)))
arr = np.array(a)
ar_t = time.time()
arr.sort()
print(arr[:10])
ar_t = time.time() - ar_t
lst_t = time.time()
a.sort()
print(a[:10])
lst_t = time.time() - lst_t
print("np.array:{}, list:{}".format(ar_t, lst_t))
print( round((lst_t - ar_t)/lst_t*100, 1),"%의 시간 감소" )
[0 1 2 3 4 5 6 7 8 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
np.array:0.21115589141845703, list:0.21898150444030762
3.6 %의 시간 감소
a = np.array([1,2,3,4])
print(a) # [1 2 3 4]
print(a.dtype) # int32
a = np.array([1, 2, 3, 4, "b"])
print(a) # ['1' '2' '3' '4' 'b']
print(a.dtype) # <U11, string이어야 하는데 뭔가 이상한 타입이 출력되었다.
print( type(a[0]) )# <class 'numpy.str_'>, 각 element는 numpy 내의 string으로 출력
l = [1,2,3,4,5]
print( np.array(l, np.int)), # [1 2 3 4 5]
print( np.array(l, np.float)) # [ 1. 2. 3. 4. 5.]
print( np.array(l, np.str)) # ['1' '2' '3' '4' '5']
import numpy as np
a = [[1, 2, 3], [4, 5, 6]]
a = np.array(a)
b = a # =의 경우 shallow copy
b[0][1] = 10
print(a)
print(b)
print("========")
a = [[1, 2, 3], [4, 5, 6]]
a = np.array(a)
b = a.copy() # .copy()를 사용해야 deep copy
b[0][1] = 10
print(a)
print(b)
[[ 1 10 3]
[ 4 5 6]]
[[ 1 10 3]
[ 4 5 6]]
========
[[1 2 3]
[4 5 6]]
[[ 1 10 3]
[ 4 5 6]]
a = np.array(list(reversed([[1,2,3], [4,5,6], [7,8,9]])))
b = np.array(([[1,2,3], [4,5,6], [7,8,9]]))
# return boolean array
print( a==b )
print( a[a>3])
print( a[a.nonzero()] )
[[False False False]
[ True True True]
[False False False]]
[7 8 9 4 5 6]
[7 8 9 4 5 6 1 2 3]
댓글남기기