This recipe includes the following topics:
- Load a csv file using Python
- Convert iterable csv object into a list
- Convert list into Numpy array
# import modules
import csv
import numpy as np
filename = 'pima-indians-diabetes.data.csv'
# open file in read mode
f = open(filename, 'r')
# create csv reader that can iterate through rows
pimaCSVReader = csv.reader(f, delimiter=',')
# convert iterable csv object into a list
pimaList = list(pimaCSVReader)
# convert into numpy array
# convert string to float
pimaArr = np.array(pimaList).astype('float')
# display row, column size
print(pimaArr.shape)
# display first row
pimaArr[0]
(768, 9)
array([ 6. , 148. , 72. , 35. , 0. , 33.6 , 0.627,
50. , 1. ])