Platte bestanden importeren met NumPy

Introductie tot data importeren in Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Waarom NumPy?

  • NumPy-arrays: standaard voor numerieke data

 

ch_1_3.003.png

Introductie tot data importeren in Python

Waarom NumPy?

  • NumPy-arrays: standaard voor numerieke data
  • Essentieel voor andere pakketten, bijv. scikit-learn ch_1_3.004.png
  • loadtxt()
  • genfromtxt()
Introductie tot data importeren in Python

Platte bestanden importeren met 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.]]
Introductie tot data importeren in Python

Je NumPy-import aanpassen

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: hoeveel rijen (niet indexen) je wilt overslaan
Introductie tot data importeren in Python

Je NumPy-import aanpassen

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: lijst met indexen van kolommen die je wilt behouden
Introductie tot data importeren in Python

Je NumPy-import aanpassen

data = np.loadtxt(filename, delimiter=',', dtype=str)
Introductie tot data importeren in Python

Gemengde datatypes

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 Bron: Kaggle
Introductie tot data importeren in Python

Gemengde datatypes

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 Bron: Kaggle
Introductie tot data importeren in Python

Laten we oefenen!

Introductie tot data importeren in Python

Preparing Video For Download...