Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert .dat to a torch.tensor in python

I have a DAT file with the following structure:

;Text1
;Text2
;Text3

;    N    Vec1       Vec2    

     1  -0.09999   0.09999
     2  -0.09999  -0.09999 
     3  -0.09999  -0.09999 

and I want to store the values of Vec1 and Vec2 into two torch tensors.

I tried to use pandas:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import torch
import pandas as pd
df = pd.read_csv('file.DAT',skiprows=4, encoding='latin-1')
vec1 = torch.tensor(df.iloc[:,1].values,  dtype=torch.float32)
vec2 = torch.tensor(df.iloc[:,2].values,  dtype=torch.float32)

But the problem is I cannot reach the values inside the resulting frame "df". Can someone help? I just want to store Vec1 and Vec2 into two torch tensors.

>Solution :

You can use read_fwf :

df = pd.read_fwf("file.DAT", skiprows=3) #`skip_blank_lines=True` by default

vec1 = torch.tensor(df["Vec1"], dtype=torch.float32)
vec2 = torch.tensor(df["Vec2"], dtype=torch.float32)

Output :

print(vec1) #tensor([-0.1000, -0.1000, -0.1000])
print(vec2) #tensor([ 0.1000, -0.1000, -0.1000])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading