- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这个真的难倒我了。我有一个计算单词权重的函数,我已经确认 a 和 b 局部变量都是 float 类型:
def word_weight(term):
a = term_freq(term)
print a, type(a)
b = idf(term)
print b, type(b)
return a*log(b,2)
运行 word_weight("the") 日志:
0.0208837518791 <type 'float'>
6.04987801572 <type 'float'>
Traceback (most recent call last):
File "summary.py", line 59, in <module>
print word_weight("the")
File "summary.py", line 43, in word_weight
return a*log(b,2)
TypeError: return arrays must be of ArrayType
为什么?
最佳答案
您正在使用 numpy.log
函数在这里,它的第二个参数不是 base
而是 out array:
>>> import numpy as np
>>> np.log(1.1, 2)
Traceback (most recent call last):
File "<ipython-input-5-4d17df635b06>", line 1, in <module>
np.log(1.1, 2)
TypeError: return arrays must be of ArrayType
您现在可以使用 numpy.math.log
或 Python 的 math.log
:
>>> np.math.log(1.1, 2)
0.13750352374993502
>>> import math
>>> math.log(1.1, 2) #This will return a float object not Numpy's scalar value
0.13750352374993502
或者,如果您只处理基数 2,那么正如@WarrenWeckesser 建议的那样,您可以使用 numpy.log2
:
关于python - 类型错误 : return arrays must be of ArrayType for a function that uses only floats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24911046/
我有一个带有 2 个 ArrayType 字段的 PySpark DataFrame: >>>df DataFrame[id: string, tokens: array, bigrams: arra
我正在尝试运行一些测试。即 assertDataFrameEquals(etalon, agg) 在 2 个数据帧上。但是得到如下错误 ScalaTestFailureLocation Expecte
我有包含 2 个 ArrayType 列的数据框。我想找出列之间的区别。 column1 将始终具有值,而 column2 可能具有空数组。我创建了以下 udf,但它不起作用 df.show() 给出
我正在尝试创建一个与 ArrayType 一起使用的全局 findAndRemove 函数,但遇到了问题。我收到一条错误消息: 无法将表达式的类型“0”转换为“@lvalue Named”类型 这是函
extension Array where Element: _ArrayType, Element.Generator.Element: Any { func transpose() ->
这个问题在这里已经有了答案: TypeError: Column is not iterable - How to iterate over ArrayType()? (2 个答案) 关闭 3 年前
我在 Pyspark 数据框中有一列,结构如下 Column1 [a,b,c,d,e] [c,b,d,f,g,h,i,p,l,m] 我想返回另一列,随机选择每行中的每个数组,数量在函数中指定。 所以像
我正在尝试按元素求和,并且我创建了这个虚拟 df。输出应该是 [10,4,4,1] from pyspark.sql.types import StructType,StructField, Stri
我有一个包含可以包含整数值的数组类型列的数据框。如果没有值,它将只包含一个值,它将是空值 重要 :注意该列不会为空,而是具有单个值的数组;空值 > val df: DataFrame = Seq((
在 HDF5 的 C++ 绑定(bind)中,我可以创建一个 1x4 H5::ArrayType 由带有类似表达式的 double 组成 H5::ArrayType array_type(H5::Pr
我有一个以 XML 形式出现的数据集,其中一个节点包含 JSON。 Spark 将其作为 StringType 读取,因此我尝试使用 from_json() 将 JSON 转换为 DataFrame。
我正在尝试使用 ArrayType() 列创建一个新的数据框,我尝试过定义模式和不定义模式,但无法获得所需的结果。我的代码在下面,带有架构 from pyspark.sql.types import
我写了一个UDF。它非常慢。我想用 pandas_udf 替换它以利用矢量化。 实际的 udf 有点复杂,但我创建了一个简化的玩具版本。 我的问题:是否可以将玩具示例中的 UDF 替换为可以利用矢量化
我目前有以下代码: def _join_intent_types(df): mappings = { 'PastNews': 'ContextualInformation', 'C
给定以下形式的 PySpark DataFrame: +----+--------+ |time|messages| +----+--------+ | t01| [m1]| | t03|[m1
我正在尝试为我的新 DataFrame 创建一个架构,并尝试了括号和关键字的各种组合,但无法弄清楚如何完成这项工作。我目前的尝试: from pyspark.sql.types import * sc
我有一个 PySpark 数据框,其中一列是字符串类型,而字符串是一个 2D 数组/列表,需要分解为行。但是,由于它不是结构/数组类型,因此无法直接使用explode。 这可以在下面的示例中看到: a
我有一个包含 3 列名为 id、feat1 和 feat2 的数据框。 feat1 和 feat2 是字符串数组的形式: Id, feat1,feat2 ------------------ 1, [
我有一个 HDF5 数据集,它是使用 H5::ArrayType 为 double[3] 编写的。 DataSpace 是一维的 (rank=1),具有 ndat 条目(每个类型都是 double[3
我正在尝试编写一个 PostgreSQL 函数并存储一个 ArrayType *state,每次我调用该函数时都需要更改它。每次调用函数时,我是否需要在 construct_array 中使用单独的
我是一名优秀的程序员,十分优秀!