NUMPY ARRAYS: CONCATENATING, FLATTENING AND ADDING DIMENSIONS
Flatten VS Ravel
FLATTEN
import numpy as np
A = np.array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11],
[12, 13],
[14, 15]],
[[16, 17],
[18, 19],
[20, 21],
[22, 23]]])
A.shape
(3, 4, 2)
Flatten_X = A.flatten()
Flatten_X
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
RAVEL
Ravel_X = A.ravel()
Ravel_X
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
Flatten vs Ravel
import numpy as np
a = np.random.randint(0,10, (2,3))
a
array([[8, 9, 0], [1, 5, 8]])
print(f'변경전 {a}')
b = a.flatten()
b[0] = 10
print(f'변경후 {a}')
print(f'b : {b}')
변경전 [[8 9 0] [1 5 8]] 변경후 [[8 9 0] [1 5 8]] b : [10 9 0 1 5 8]
print(f'변경전 {a}')
b = a.ravel()
b[0] = 10
print(f'변경후 {a}')
print(f'b : {b}')
변경전 [[8 9 0] [1 5 8]] 변경후 [[10 9 0] [ 1 5 8]] b : [10 9 0 1 5 8]
⇒ ravel 은 b를 바꿔주면 a도 같이 바뀐다. 공유하고 싶지 않을때는 flatten
numpy.reshape()
- numpy.reshape(a, newshape, order = 'C')
arr = np.array([[0, 1],
[2, 3],
[4, 5]])
np.reshape(arr, (2,3))
array([[0, 1, 2], [3, 4, 5]])
arr.reshape(2,3)
array([[0, 1, 2], [3, 4, 5]])
arr_data.reshape(-1,1)
array([[0], [1], [2], [3], [4], [5]])
arr_data.reshape(1,-1)
array([[0, 1, 2, 3, 4, 5]])
X = np.array(range(24))
X
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
X.reshape((3,1,8))
array([[[ 0, 1, 2, 3, 4, 5, 6, 7]],
[[ 8, 9, 10, 11, 12, 13, 14, 15]],
[[16, 17, 18, 19, 20, 21, 22, 23]]])
CONCATENATING ARRAYS - 배열 연결
- numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
x = np.array([11,22])
y = np.array([18,7,6])
z = np.array([1,3,5])
np.concatenate((x,y,z))
array([11, 22, 18, 7, 6, 1, 3, 5])
x = np.array(range(24))
x = x.reshape((3,4,2))
y = np.array(range(100,124))
y = y.reshape((3,4,2))
np.concatenate((x,y), axis = 0)
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[ 10, 11],
[ 12, 13],
[ 14, 15]],
[[ 16, 17],
[ 18, 19],
[ 20, 21],
[ 22, 23]],
[[100, 101],
[102, 103],
[104, 105],
[106, 107]],
[[108, 109],
[110, 111],
[112, 113],
[114, 115]],
[[116, 117],
[118, 119],
[120, 121],
[122, 123]]])
np.concatenate((x,y), axis = 1)
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[100, 101],
[102, 103],
[104, 105],
[106, 107]],
[[ 8, 9],
[ 10, 11],
[ 12, 13],
[ 14, 15],
[108, 109],
[110, 111],
[112, 113],
[114, 115]],
[[ 16, 17],
[ 18, 19],
[ 20, 21],
[ 22, 23],
[116, 117],
[118, 119],
[120, 121],
[122, 123]]])
ADDING NEW DIMENSIONS - 차원 추가
x = np.array([2,5,18,14,4])
y = x[:, np.newaxis]
print(y)
[[ 2]
[ 5]
[18]
[14]
[ 4]]
VECTOR STACKING - 벡터 쌓기
A = np.array([3, 4, 5])
B = np.array([1,9,0])
np.column_stack((A,B))
array([[3, 1],
[4, 9],
[5, 0]])
np.row_stack((A,B))
array([[3, 4, 5],
[1, 9, 0]])
BOOLEAN INDEXING - 참/거짓 인덱싱
x = np.array([2,5,18,14,18,18,4])
x == 18
array([False, False, True, False, True, True, False])
FANCY INDEXING
C = np.array([123,188,190,99,77,88,100])
A = np.array([4,7,2,8,6,9,5])
A<=5
array([ True, False, True, False, False, False, True])
C[[True, False, True, False, False, False, True]]
array([123, 190, 100])
WEIGHTED RANDOM CHOICE WITH NUMPY - 무작위 가중치 부여
from numpy.random import choice
professions = ["scientist",
"philosopher",
"engineer",
"priest",
"programmer"]
probabilities = [0.2, 0.05, 0.3, 0.15, 0.3]
choice(professions, p=probabilities)
'programmer’
programming_languages = ["Java", "C", "Python", "C++"]
weights = np.array([16, 15.2, 10, 6.1])
# normalization
weights /= sum(weights)
print(weights)
for i in range(10):
print(choice(programming_languages, p=weights))
[0.33826638 0.32135307 0.21141649 0.12896406]
Java
Java
C++
C
Java
Java
Python
C
Java
Python
'빅데이터 분석가 양성과정 > Python' 카테고리의 다른 글
Matplotlib ( 2 ) (0) | 2024.07.09 |
---|---|
Matplotlib ( 1 ) (0) | 2024.07.09 |
Numpy ( 2 ) (0) | 2024.07.09 |
Numpy ( 1 ) (0) | 2024.07.09 |
Web Crawling - 네이버 금융 환율정보 / 시 / 블로그 / 뉴스 (2) | 2024.07.09 |