기본 콘텐츠로 건너뛰기

데이터 열로 추가하여 쌓기

import numpy as np
x = []
a = np.array([1,2,3])
Out[5]: array([1, 2, 3])
a.shape
Out[6]: (3,)
np.concatenate((x,a))
Out[7]: array([ 1.,  2.,  3.])
x = np.concatenate((x,a))
x = np.concatenate((x,a))
Out[10]: array([ 1.,  2.,  3.,  1.,  2.,  3.])
x = []
x = np.concatenate((x,a), axis=1)

=> 오류발생
 -. x가 차원이 안맞아서 안됨. 

np.expand_dims(a,axis=1)
Out[14]: 
array([[1],
       [2],
       [3]])
b = np.expand_dims(a,axis=1)
b.shape
Out[16]: (3, 1)

=> (3,1)짜리 차원으로 확장한 후 열 방향으로 쌓을 수 있다.

=> 아래는 empty함수나 zeros를 사용하여 공간을 만드는 작업

x = []
x = np.empty((3,0))
x
Out[19]: array([], shape=(3, 0), dtype=float64)
x = np.zeros((3,0))
x
Out[21]: array([], shape=(3, 0), dtype=float64)
x = np.zeros((3,0))
a
Out[23]: array([1, 2, 3])
b
Out[24]:
array([[1],
       [2],
       [3]])

np.concatenate((x,b), axis=1)
Out[26]:
array([[ 1.],
       [ 2.],
       [ 3.]])
x
Out[27]: array([], shape=(3, 0), dtype=float64)
x = np.concatenate((x,b), axis=1)
x = np.concatenate((x,b), axis=1)
x = np.concatenate((x,b), axis=1)
x
Out[31]:
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.],
       [ 3.,  3.,  3.]])
x = []

=> 차례대로 들어가는 것을 볼 수 있다.

다른 방법으로 ( 이 방법을 더 추천)

 -. append로 다 쌓은 후에 for문 밖에서 stack 함수를 사용, vstack과 hstack이 같으나, axis로 컨트롤할 수 있음

a
Out[33]: array([1, 2, 3])
b
Out[34]:
array([[1],
       [2],
       [3]])
x.append(a)
x.append(a)
x
Out[37]: [array([1, 2, 3]), array([1, 2, 3])]
np.stack(x, axis=1)
Out[38]:
array([[1, 1],
       [2, 2],
       [3, 3]])


댓글

이 블로그의 인기 게시물

HTML 테스트

Lab 시리즈 화장품 구매(파란색, 가장크게) <span style="color: blue; font-size: x-large;">Lab 시리즈 화장품 구매(파란색, 가장크게)</span><br /> Lab 시리즈 화장품 구매(검은색, 보통) <span style="color: blue; font-size: x-large;"><br /></span> Lab 시리즈 화장품 구매(검은색, 보통)<br /> Lab 시리즈 화장품 구매(검은색, 보통, Airal) <span style="font-family: Arial, Helvetica, sans-serif;">Lab 시리즈 화장품 구매(검은색, 보통, Airal)</span><br /> Lab 시리즈 화장품 구매(검은색, 보통, Airal, Bold) <span style="font-family: Arial, Helvetica, sans-serif;"><b>Lab 시리즈 화장품 구매(검은색, 보통, Airal, Bold)</b></span> 하나의 문단을 의미하는 <p>태그 문장의 일부를 의미하는 <span>태그 강제줄바꿈을 의미하는 <br>태그 비교적 넓은 지역을 묶어 지정 </div>

keras를 이용하여 cifar10 실행해보기 (Functional API step by step)

Keras 라이브러리를 이용하여 cifar 10 데이터를 기본적인 CNN 구조를 이용하여 수행 사용하는 레이어들을 추가한다. from keras.layers import Input, Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D 모델도 추가한다. from keras.models import Model 구조는 기본적으로 아래와 같다. 이는 keras에서 제공하는 cifar10 예제를 참고하였다. conv2D -> conv2D -> maxpooling -> dropout -> conv2D -> conv2D -> maxpooling -> dropout -> flatten -> Dense -> dropout ->Dense(마지막은 분류하고자 하는 개수, 여기서는 10) 블럭을 생성하기 전에 Input layer에 형태를 넣어줘야한다. #Input inputs =Input((32,32,3)) # block1 x = Conv2D(32, (3,3), activation='relu', padding='same', name='block1_conv1')(inputs)  x = Conv2D(32, (3,3), activation='relu', padding='valid', name='block1_conv2')(x)  x = MaxPooling2D((2, 2), name='block1_pool')(x) x = Dropout(0.25)(x)  # block2 x = Conv2D(64, (3,3), activation='relu', padding='same', name='block2_conv1')...