Import Data From a CSV to Use with a Keras Model Using NumPy’s genfromtxt Method

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet

We’ll replace our set of sample data with data that we import from a CSV, by importing it as a numpy array using numpy's genfromtxt method. Then we’ll use that data to train the binary classification model.

Instructor: [00:01] We have a CSV that contains a hundred rows of inputs that we want to use to train our neural network. Each row has four numbers and a single output value of a zero or one. Zero represent the numbers are generally low which for us means less than 50, and one means the values are generally high which means over 50.

[00:26] After defining a neural network, import the CSV using NumPy's genfromtxt method. Genfromtxt takes the CSV to import as the first argument and an optional delimiter argument which is just a comma in this case. Then we can assign the output of that to data variable, which is now a NumPy array that contains all of the rows and columns that were in the CSV.

[00:54] We can split the data into the input values by taking all the rows except for the first, because that's the header row, and the first four column which are inputs. Then, we can extract the output class values by taking all the rows except for the header again, and only the fourth column this time which is the output value.

[01:19] If we print the X values at this point, we have a NumPy array with a hundred rows and four columns in each row. If we print the Y values, we have a NumPy array with a hundred values which are all the output classes. We can then use the X_train and Y_train values to fit the model, passing them into the X and Y arguments, specifying 100 epox and a validations split of 20 percent.

[01:51] When we run that, the network successfully trained on the init data.