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
Published 6 years ago
Updated 5 years ago

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.

egghead
egghead
~ 14 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today