【问题标题】:How to convert array(densevectors) into array?如何将数组(密集向量)转换为数组?
【发布时间】:2021-12-26 01:59:40
【问题描述】:

在下面的代码中,我尝试使用EnsembleVoteClassifier() 实现加权投票分类器。

from mlxtend.classifier import EnsembleVoteClassifier

import copy

eclf = EnsembleVoteClassifier(clfs=[s1, s2], weights=[1,1],refit=False)

其中 s1 和 s2 是 PySpark 管道模型。输入是图像数据集。 S1 是特征化器和 LogisticRegression 的流水线模型。 S2是特征化器和NaiveBayes分类器的流水线模型。

为了适应分类器,我需要图像和标签的特征。为了获得功能,我使用了变压器功能。并转换为 pandas 对象。

qa = featurizer.transform(train)

pandas_df3=qa.toPandas()

y_pred3 = np.array(pandas_df3['features'])

y_true3 = np.array(pandas_df3['label'])

eclf.fit(y_pred3,y_true3)

要使用学习投票分类器 eclf 在测试集上进行预测,我需要测试图像的特征。要获得特征,我使用了转换器函数。并转换为 pandas 对象。

qa1 = featurizer.transform(df1)

pandas_df4=qa1.toPandas()

y_pred4 = np.array(pandas_df4['features'])


# used predict () for prediction on test set
eclf.predict(y_pred4)
 # here y_pred4 is array([DenseVector([0.0205, 0.0206, 0.1201, 0.9808, 2.3941, 0.1696, 0.2612, 0.2964, 0.3071, 0.0961, 0.2314, 0.0729, 0.0445, 0.0, 0.0, 0.0585, 0.025, 0.3339, 0.2257, 0.0, 0.7522, 0.0, 0.1125, 0.6402, 0.2077, 0.0101, 0.4223, 0.1546, 0.195, 0.5894, 0.3867, 0.0298, 0.0, 0.3882, 0.086, 2.026, 0.3705, 0.1113, 0.452, 0.0056, 0.0, 0.6452, 0.4749, 0.3708, 0.1639,[DenseVector([.......... which is actually features of an image.
# but this gives me error 'PipelineModel' object has no attribute 'predict'
 # so I thought of converting it into array([],[]) form

另外,PySpark 中是否有类似EnsembleVoteclassifier 的投票分类器函数来实现加权集成分类器?

【问题讨论】:

    标签: python arrays pyspark numpy-ndarray


    【解决方案1】:

    您使用哪个包获取DenseVector

    是火花吗?如果是,根据documentationDenseVector 有一个toArray() 方法,它返回底层的np.ndarray

    【讨论】:

    • 是的,我正在使用 spark。当我使用 toArray() 它返回我 TypeError: Invalid argument, not a string or column
    • 您能否更新您的初始帖子并提供一个可复制的最小示例,以便我们进一步为您提供帮助?
    • 是的,我会更新
    • 好吧,如果错误是'PipelineModel' object has no attribute 'predict',那意味着PipelineModel类没有一个名为predict()的方法,所以你不能调用它。这很容易通过查看doc 进行验证。此时与y_pred4 的类型无关。你只是没有在好地方使用好课程。我不太了解 spark 包,因此无法提供进一步的帮助。
    猜你喜欢
    • 2022-01-21
    • 2018-12-15
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多