在嵌入过程中出现错误:无法将字符串转换为浮点数:’ng’

我在使用GloVe方法处理预训练的词向量。数据包含维基百科的数据。在嵌入数据时,我遇到了一个错误,提示无法将字符串转换为浮点数:’ng’。

我尝试检查了数据,但没有找到符号’ng’。

# load embedding as a dictdef load_embedding(filename):    # load embedding into memory, skip first line    file = open(filename,'r', errors = 'ignore')    # create a map of words to vectors    embedding = dict()    for line in file:        parts = line.split()        # key is string word, value is numpy array for vector        embedding[parts[0]] = np.array(parts[1:], dtype='float32')    file.close()    return embedding

这是错误报告。请指导我下一步该怎么做。

runfile('C:/Users/AKSHAY/Desktop/NLP/Pre-trained GloVe.py', wdir='C:/Users/AKSHAY/Desktop/NLP')C:\Users\AKSHAY\AppData\Local\conda\conda\envs\py355\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.  from ._conv import register_converters as _register_convertersUsing TensorFlow backend.Traceback (most recent call last):  File "<ipython-input-1-d91aa5ebf9f8>", line 1, in <module>    runfile('C:/Users/AKSHAY/Desktop/NLP/Pre-trained GloVe.py', wdir='C:/Users/AKSHAY/Desktop/NLP')  File "C:\Users\AKSHAY\AppData\Local\conda\conda\envs\py355\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile    execfile(filename, namespace)  File "C:\Users\AKSHAY\AppData\Local\conda\conda\envs\py355\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile    exec(compile(f.read(), filename, 'exec'), namespace)  File "C:/Users/AKSHAY/Desktop/NLP/Pre-trained GloVe.py", line 123, in <module>    raw_embedding = load_embedding('glove.6B.50d.txt')  File "C:/Users/AKSHAY/Desktop/NLP/Pre-trained GloVe.py", line 67, in load_embedding    embedding[parts[0]] = np.array(parts[1:], dtype='float32')ValueError: could not convert string to float: 'ng'

回答:

ValueError: 无法将字符串转换为浮点数:’ng’

为了解决上述问题,请在函数中添加 encoding=’utf8′,如下所示:

file = open(filename,'r', errors = 'ignore', encoding='utf8')

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注