我嵌套了 json 如下
{
"product" : "name",
"protocol" : "scp",
"read_logs" : {
"log_type" : "failure",
"log_url" : "htttp:url"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下代码创建 Python 类对象。
import json
class Config (object):
"""
Argument: JSON Object from the configuration file.
"""
def __init__(self, attrs):
if 'log_type' in attrs:
self.log_type = attrs['log_type']
self.log_url = attrs['log_url']
else:
self.product = attrs["product"]
self.protocol = attrs["protocol"]
def __str__(self):
return "%s;%s" %(self.product, self.log_type)
def get_product(self):
return self.product
def get_logurl(self):
return self.log_url
class ConfigLoader (object):
'''
Create a confiuration loaded …Run Code Online (Sandbox Code Playgroud) 我正在构建文本分类器并使用 Spark countVectorizer 来创建特征向量。
现在要将此向量与 BIDGL 库一起使用,我需要将特征向量中的所有 0 转换为 1。
这是我的特征向量,它是一个稀疏向量:
vectorizer_df.select('features').show(2)
+--------------------+
| features|
+--------------------+
|(1000,[4,6,11,13,...|
|(1000,[0,1,2,3,4,...|
+--------------------+
only showing top 2 rows
Run Code Online (Sandbox Code Playgroud)
我正在尝试更新该值,如下所示。首先将稀疏向量转换为稠密向量
from pyspark.mllib.linalg import Vectors, VectorUDT
from pyspark.sql.types import ArrayType, FloatType
from pyspark.sql.functions import udf
update_vector = udf(lambda vector: Vectors.dense(vector), VectorUDT())
df = vectorizer_df.withColumn('features',update_vector(vectorizer_df.features))
df.select('features').show(2)
+--------------------+
| features|
+--------------------+
|[0.0,0.0,0.0,0.0,...|
|[5571.0,4688.0,24...|
+--------------------+
only showing top 2 rows
Run Code Online (Sandbox Code Playgroud)
一旦我有了稠密向量,我就尝试给所有元素加 1
def add1(x):
return x+1
def array_for(x):
return np.array([add1(xi) for xi in x])
add_udf_one = udf(lambda z: array_for(z), …Run Code Online (Sandbox Code Playgroud)