본문 바로가기

카테고리 없음

[python] numpy 행렬 전체 출력 하는법

결론은 np.set_printoptions(threshold=np.inf, linewidth=np.inf) 해주면 된다.

 

텐서플로우 예제코드 스터디 중이다.

from __future__ import absolute_import, division, print_function

import tensorflow as tf
import numpy as np

num_classes = 10
num_features = 784

learning_rate = 0.01
training_steps = 1000
batch_size = 256
display_step = 50

from tensorflow.keras.datasets import mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()

print(type(x_train))
x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
print(x_train)
#np.set_printoptions(threshold=784,linewidth=np.inf)
#print(x_train[0])

28*28이미지 이걸 로딩해야하는데

Mnist load가 됐는지 확인해보기 위해 array를 출력해봤는데 아래와 같이 출력 되었다.

x_train: 0밖에 없어서 에러난줄 알았다.

그런데 빈 행렬이 출력되는줄 알고 y는 어떤가 하고 봤는데 잘 출력되었다.

 

y_train은 로딩이 됐구나

 

뭐지 하고 생각하다보니 로드한 데이터의 코너부분들은 거의다 0이 겠구나 싶어서 행렬 전체를 출력하는 법을 찾아보았다.

 

맨위의 소스코드에서 주석처리한 np.set_printoptions(threshold=784,linewidth=np.inf)를 추가해주면 된다.

threshold=784개가 넘는 원소를 갖는경우 축약형으로 출력해주는 속성이고 linewidth는 한줄에 출력되는 원소 숫자이다.

이 둘의 속성을 변경해주면 아래와 같이 잘 출력이 된다. 근데 x_train은 수가 많아서 한개만 출력하였다. x_train[0]

5? 맞나?

 

아래는 해당함수 메뉴얼이다.

메뉴얼 자체는 위와 같다.(근데 v1.9)