TypeError:只能将size-1数组转换为Python标量以获取加密数据

问题描述 投票:0回答:1

我正在尝试加密这个数据集Wholesales customer,已经删除了前两列(Channel和Region)。加密脚本工作正常,因为我能够生成加密的公钥。但是,我认为问题在于嵌套的for循环线。任何帮助,将不胜感激。

from Paillier_CRT.gmpy2mod import *
import numpy as np
import pandas as pd


priv, pub = generate_keypair(128)
n = pub.n
print("The public key is:", n)


def to_encrypt(public, data_matrix):
    data_encrypted = encrypt(public, data_matrix)
    return data_encrypted


def load_data():
    raw_data = pd.read_csv('wholesales1.csv')
    dtset = raw_data.drop(['Channel'], axis=1)
    new_dtset = dtset.drop(['Region'], axis=1)
    converted_data = new_dtset.values
    data_to_encrypt = []

    for row in converted_data:
        for elem in row:
            data_to_encrypt = to_encrypt(pub, int(converted_data))
            return data_to_encrypt


working_data = load_data()
print("The encrypted data is: ", working_data)

错误信息:

TypeError: only size-1 arrays can be converted to Python scalars
python arrays numpy scalar
1个回答
0
投票

此错误通常意味着您正在使用预期数组的标量值。

代替

data_to_encrypt = to_encrypt(pub, int(converted_data))

你可以试试

data_to_encrypt = to_encrypt(pub, converted_data.astype(int))
© www.soinside.com 2019 - 2024. All rights reserved.