Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Pandas dataframe to PyTorch tensor?

I want to train a simple neural network with PyTorch on a pandas dataframe df.

One of the columns is named "Target", and it is the target variable of the network. How can I use this dataframe as input to the PyTorch network?

I tried this, but it doesn't work:

import pandas as pd import torch.utils.data as data_utils  target = pd.DataFrame(df['Target']) train = data_utils.TensorDataset(df, target) train_loader = data_utils.DataLoader(train, batch_size=10, shuffle=True) 
like image 864
M. Fabio Avatar asked May 12 '18 15:05

M. Fabio


People also ask

Does PyTorch have pandas?

PyTorch provides many tools to make data loading easy and make your code more readable. In this tutorial, we will see how to load and preprocess Pandas DataFrame.


1 Answers

I'm referring to the question in the title as you haven't really specified anything else in the text, so just converting the DataFrame into a PyTorch tensor.

Without information about your data, I'm just taking float values as example targets here.

Convert Pandas dataframe to PyTorch tensor?

import pandas as pd import torch import random  # creating dummy targets (float values) targets_data = [random.random() for i in range(10)]  # creating DataFrame from targets_data targets_df = pd.DataFrame(data=targets_data) targets_df.columns = ['targets']  # creating tensor from targets_df  torch_tensor = torch.tensor(targets_df['targets'].values)  # printing out result print(torch_tensor) 

Output:

tensor([ 0.5827,  0.5881,  0.1543,  0.6815,  0.9400,  0.8683,  0.4289,          0.5940,  0.6438,  0.7514], dtype=torch.float64) 

Tested with Pytorch 0.4.0.

I hope this helps, if you have any further questions - just ask. :)

like image 176
MBT Avatar answered Oct 10 '22 14:10

MBT