Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

python - Problem with converting a pandas dataframe column into a float

I am trying to build a celebrity look-alike program, using VggFace and IMDb celebrity faces database, I calculate the embedding of each face in the database and I stock it in a pandas data frame. However, when I try to load that embedding afterword to use it to calculate a cosine distance I can't make it work, apparently it is stocked as a list inside of a string (ie "[ 1.3 -1 .... ] )etc". So I have to questions:

  • How can I see how these vectors are really stored? when I use Excel to view the data it just seems okay.

  • How can I transform this string into a list of floats; I tried using ast but it didn't work.

My code is too long, so I don't know if I should upload the dataframe online so you guys can view it.

Here is a line using df.to_dict(): 518 : '[ 3.8515975 0.4580283 1.964929 ... -6.336113 1.31456 4.2759323]'

I am trying to iterate through the dataframe and multiply each line with an embedding representation of the user face using this code :

vect = calculVecteur('imagesUtilisateur/test.jpg',model)
vect = list(map(float, vect))
meta_data_imdb = pd.read_csv("resources/vectorisation/imdb_metadata_v.csv")
meta_data_imdb['distance'] = meta_data_imdb['vecteur'].apply(lambda x: calculerDistance(x,vect))

the calculVecteur() calculates the embedding of the users image using the Vgg model, then I calculate the cosine distance using the calculerDistance() method.

Using the debugger here is what I get :

https://imgur.com/a/B95VjdT

Thanks in advance for any help.

question from:https://stackoverflow.com/questions/65834947/problem-with-converting-a-pandas-dataframe-column-into-a-float

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the issue is type conversion try pd.to_numeric it works for me for example if you want to convert a list of columns to numeric;

cols = df.columns
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

You can do this for only one column as well by specifying it instead of a list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...