PyTorch Tensor Tutorial (5): Import CSV files into a Tensor for Neural Networks
Please like, comment and share, if you enjoyed this video 🙂
0:25 Format the CSV file in Excel or OpenOffice
1:50 Import torch and pandas
2:05 Reading the CSV file with pandas
3:42 Convert CSV Data in a PyTorch Tensor
4:46 Check the shape of the Tensor
.csv? .xls? .xlsx? .txt? tables? databases? MySQL? pandas? read_csv?
Often the raw data that you need to prepare for analysis are in different formats. Usually they are available in various databases or also in Excel files.
A common output format from all these sources is the so-called CSV format. This format can be created from databases and Excel alike and is actually just a plain text file, but with the data arranged in a structured way (like in a database).
This tutorial shows how to convert the universal CSV format into a PyTorch tensor so that further operations are possible with it.
import pandas as pd
CSV file read and output with pandas
Terragon_Sample_Data = pd.read_csv(‚20181024_Terragon_Sample_Data.csv‘)
Convert values from the Pandas DataFrame into a PyTorch tensor
Terragon = torch.tensor(Terragon_Sample_Data.values)
hello everybody and welcome back to another tutorial of tarragon de where we want to see how to import um data from Excel or open Office for example and use the data inside uh yeah in the tensor and inside of pytorch so what we first want to do is to make list of different values here for example tarragon 1 taragon 2 tarragon 3 that can be whatever you like of course so you have three three inputs and we take here random numbers that are the actual values inside not so big so yes like that and so we make here for every for every kind of variable data some random input in Excel so that can be everything uh you have as Source data and you have to save that for example I already saved that here um so I just click save um with that file name and as a CSV document that is quite important because um we want to use pandas and for that we go here inside um pie charm and we start writing our code and first we want to import torch and import pandas at SPD for example that’s standard you don’t have to do that like that but we want to do it so then I paste here the code that is the code for reading the CSV file here and you can see here is the file name it’s the same as we had in our um open Office document and with the PD read CSV command PD for pandas it will read automatically The Columns and rows so you don’t have to deal with that so much and then we want to print and see what we have so here we have our data he is importing the headlines also so Terror On One turn on two Terror on three like here and we have here all the values we have um the First Column here is new that’s not um inside our data here but it is the yes the index of python here of pandas to yes index our input rows in Open Office it’s a bit it’s a different count so he starts the one with the headline and Row 2 is the first input and here in pytorch is um the first row of data it says the index 0. so it counts from zero and the next step is to convert the data here into a tensor into a pi torch tensor for that we’ll create tarragon as a yes tensor as a torch tensor and we will import here um tarragon sample data that we filled with the CSV data and we add um Point values that means he only takes the values here inside yes our array and then we will print again and we also want to print the shape like we saw in the pytorch tutorial and you see here the tensor looks like that it’s not the counting here is not shown so because it’s just adding the values to the tensor and it has the size or the shape 10 3. so we have three columns or three um different types of input and we have 10 lines of input or rows so yes that’s our training set or what you want to call it okay so that was it short and straightforward see you next time