python - 在带参数的数据框中使用应用会返回错误
问题描述
我有以下代码
def mean_vectorizer(words, dimention, diction):
return np.array([np.mean([diction[w] for w in words if w in diction] or [np.zeros(dimention)], axis=0)])
data['vector'] = data.apply(lambda x: mean_vectorizer(x['words'], 100, diction), axis=1)
数据框中的列words
有一个单词列表。diction
是一个以单词为键,以固定大小为值的数组的字典。我尝试做的是创建一个新列,它将解析每一行中的列表,从字典中获取每个单词的数组,并用其余的平均值计算一个数组。
运行上面的代码会返回这个错误:
Exception: Data must be 1-dimensional
但是,如果我使用以下代码运行单行代码,它不会抱怨
words = data.iloc[0]['words']
np.array([np.mean([diction[w] for w in words if w in diction] or [np.zeros(dimention)], axis=0)])
解决方案
推荐阅读
- sql - SQL Server 初始备份恢复有效,其余无效
- python-3.x - 如何在 Windows 中通过 LDAP 将 Python 3.x cx_Oracle 连接到 Oracle DB?
- laravel - 在 apache 服务器上为 laravel 配置 https
- sql - 通过 vba 在 ms 访问中填充报告
- node.js - 如何在事件处理程序中实现异步/等待?
- java - java.lang.NoSuchFieldError: ACCEPT_CASE_INSENSITIVE_VALUES
- powershell - hashtable.ContainsKey 永远不会评估为真?
- php - 数字格式化phpspreadsheet,小数不被导出的xlsx视为数字
- mysql - PHPUnit - 批量运行测试时出现“超过锁定等待超时;尝试重新启动事务”错误
- magento2 - Magento2上的面包屑后如何从cms页面移动页面标题