NumPy로 평면 파일 불러오기

Python에서 데이터 가져오기 입문

Hugo Bowne-Anderson

Data Scientist at DataCamp

왜 NumPy인가?

  • NumPy 배열: 수치 데이터 저장 표준

 

ch_1_3.003.png

Python에서 데이터 가져오기 입문

왜 NumPy인가?

  • NumPy 배열: 수치 데이터 저장 표준
  • 다른 패키지의 필수 요소: 예) scikit-learn ch_1_3.004.png
  • loadtxt()
  • genfromtxt()
Python에서 데이터 가져오기 입문

NumPy로 평면 파일 불러오기

import numpy as np
filename = 'MNIST.txt'
data = np.loadtxt(filename, delimiter=',')
data
[[   0.    0.    0.    0.    0.]
 [  86.  250.  254.  254.  254.]
 [   0.    0.    0.    9.  254.]
 ..., 
 [   0.    0.    0.    0.    0.]
 [   0.    0.    0.    0.    0.]
 [   0.    0.    0.    0.    0.]]
Python에서 데이터 가져오기 입문

NumPy 가져오기 설정하기

import numpy as np
filename = 'MNIST_header.txt'
data = np.loadtxt(filename, delimiter=',', skiprows=1)
print(data)
[[   0.    0.    0.    0.    0.]
 [  86.  250.  254.  254.  254.]
 [   0.    0.    0.    9.  254.]
 ..., 
 [   0.    0.    0.    0.    0.]
 [   0.    0.    0.    0.    0.]
 [   0.    0.    0.    0.    0.]]
  • skiprows: 건너뛸 행 수(인덱스 아님)
Python에서 데이터 가져오기 입문

NumPy 가져오기 설정하기

import numpy as np
filename = 'MNIST_header.txt'
data = np.loadtxt(filename, delimiter=',', skiprows=1, usecols=[0, 2])
print(data)
[[   0.    0.]
 [  86.  254.]
 [   0.    0.]
 ..., 
 [   0.    0.]
 [   0.    0.]
 [   0.    0.]]
  • usecols: 유지할 열 인덱스 목록
Python에서 데이터 가져오기 입문

NumPy 가져오기 설정하기

data = np.loadtxt(filename, delimiter=',', dtype=str)
Python에서 데이터 가져오기 입문

혼합 데이터 타입

titanic.csv

                        Name      Sex  Cabin   Fare
     Braund, Mr. Owen Harris     male    NaN    7.3
  Cumings, Mrs. John Bradley   female    C85   71.3
      Heikkinen, Miss. Laina   female    NaN    8.0
Futrelle, Mrs. Jacques Heath   female   C123   53.1
    Allen, Mr. William Henry     male    NaN   8.05


1 출처: Kaggle
Python에서 데이터 가져오기 입문

혼합 데이터 타입

titanic.csv

                        Name      Sex  Cabin   Fare
     Braund, Mr. Owen Harris     male    NaN    7.3
  Cumings, Mrs. John Bradley   female    C85   71.3
      Heikkinen, Miss. Laina   female    NaN    8.0
Futrelle, Mrs. Jacques Heath   female   C123   53.1
    Allen, Mr. William Henry     male    NaN   8.05
               ^                                 ^
            strings                           floats
1 출처: Kaggle
Python에서 데이터 가져오기 입문

Vamos praticar!

Python에서 데이터 가져오기 입문

Preparing Video For Download...