TypeError: only size-1 arrays can be converted to Python scalars for encrypted data

TypeError: only size-1 arrays can be converted to Python scalars for encrypted data

我正在尝试加密此数据集 Wholesales customer,删除了前两列(频道和区域)。 加密脚本工作正常,因为我能够生成 public 密钥进行加密。但是,我认为问题在于嵌套的 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

此错误通常意味着您在需要数组的地方使用了标量值。

而不是

data_to_encrypt = to_encrypt(pub, int(converted_data))

你可以试试

data_to_encrypt = to_encrypt(pub, converted_data.astype(int))