使用PCA降低维度:AttributeError: ‘numpy.ndarray’ object has no attribute ‘items’

我在尝试实现DZone上的一个示例项目(https://dzone.com/articles/cv-r-cvs-retrieval-system-based-on-job-description),但遇到了一个问题。在这种情况下,我设置了

dir_pca_we_EWE = 'pickle_model_pca.pkl'

并且正在执行以下代码:

def reduce_dimensions_WE(dir_we_EWE, dir_pca_we_EWE):    m1 = KeyedVectors.load_word2vec_format('./wiki.en/GoogleNews.bin', binary=True)    model1 = {}    # 标准化向量    for string in m1.wv.vocab:        model1[string] = m1.wv[string] / np.linalg.norm(m1.wv[string])    # 降低维度    pca = decomposition.PCA(n_components=200)    pca.fit(np.array(list(model1.values())))    model1 = pca.transform(np.array(list(model1.values())))    i = 0    for key, value in model1.items():        model1[key] = model1[i] / np.linalg.norm(model1[i])        i = i + 1    with open(dir_pca_we_EWE, 'wb') as handle:        pickle.dump(model1, handle, protocol=pickle.HIGHEST_PROTOCOL)return model1

这会产生以下错误:

Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 12, in reduce_dimensions_WEAttributeError: 'numpy.ndarray' object has no attribute 'items'

一如既往,任何帮助都将不胜感激!


回答:

你首先将model1 = {}初始化为一个空字典。在使用transform

model1 = pca.transform(np.array(list(model1.values())))

变量model1变成了一个numpy.ndarray,这是pca的transform方法的返回类型。在以下行中

for key, value in model1.items():    ...

你仍然将model1视为一个字典,但它已经不是了。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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