只能将大小为1的数组错误转换为Python标量 [英] Error only size-1 arrays can be converted to Python scalars

查看:302
本文介绍了只能将大小为1的数组错误转换为Python标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

for a in data_X:
    for i in a:
        if not i.isdigit():
            x=hash(i)
            data_X[column,row]=x
        row=row+1
    row=0
    column=column+1


desired_array = [int(numeric_string) for numeric_string in data_X]

和我的Data_X是这些(仅是数组的一部分):

and I the Data_X are these (only a part of the Array):

[['42' '-6725209669690155188' '159449' ... '40' '4017763880221344027'
  '-136014339944321305']
 ...
 ['30' '-6725209669690155188' '154950' ... '60' '4017763880221344027'
  '-136014339944321305']]

将数字从char(例如'42')转换为int(42),我可以这样做: desired_array = [int(numeric_string)data_X中的numeric_string]
,但是我收到错误消息只有错误的size-1数组可以转换为Python标量,我不知道该怎么办我愿意。

and I wanted to transform the numbers from char ( e.g '42') to int (42) and I thougt I could doing this: desired_array = [int(numeric_string) for numeric_string in data_X] but I received the error "Error only size-1 arrays can be converted to Python scalars" and I don't know what can I do..

这些就是我想要的东西:from:

these is what I want to have: from :

[['42' '-6725209669690155188' '159449' ... '40' '4017763880221344027'
  '-136014339944321305']
 ...
 ['30' '-6725209669690155188' '154950' ... '60' '4017763880221344027'
  '-136014339944321305']]

至:

[[42 -6725209669690155188 159449 ... 40 4017763880221344027
  -136014339944321305]
 ...
 [30 -6725209669690155188 154950 ... 60 4017763880221344027
  -136014339944321305]]

有人可以告诉我问题出在哪里吗?

Can somebody tell me where is the Problem?

谢谢!

推荐答案

您正尝试使用列表理解来创建新列表,例如:

You're trying to use a list comprehension to create a new list, like this:

 desired_array = [int(numeric_string) for numeric_string in data_X]

由于 data_X 是2D数组,每个 numeric_string 是1D数组,只要您有多少列(至少7列)即可。 (事实上​​,您将其称为 numeric_string 并不能使其成为字符串。)您不能在 int 上调用

Since data_X is a 2D array, each numeric_string is a 1D array, as long as however many columns you have (at least 7). (The fact that you called it numeric_string doesn't make it a string.) You can't call int on that, for exactly the reason that the error message shows.

如果不清楚,您应该尝试打印出值:

If this isn't clear, you should try printing out the values:

for numeric_string in data_X:
    print(numeric_string)

...,应该很清楚,数字字符串不是数字字符串。

… and it should be pretty clear that numeric_string is not a numeric string.

可以使用嵌套循环来解决此问题。如果您不太理解,请首先使用显式循环语句编写它:

You could fix this with a nested loop. If you don't understand comprehensions that well, write it with explicit loop statements first:

desired_array = []
for row in data_X:
    desired_row = []
    for col in row:
        desired_row.append(int(col))
    desired_array.append(desired_row)

…然后,一旦确定您可以理解它,便可以将其转变为理解力:

… and then you can turn it into a comprehension once you're sure you understand it:

desired_array = [int(numeric_string) for numeric_string in row] for row in data_X]






但是,这仍然不能为您提供2D整数数组,而是为您提供了一个整数列表。它与相似,但是它又大又慢,因此您无法在其上调用numpy方法。 (或者,仍然至少可以将其传递给全局numpy函数。)


However, that still doesn't give you a 2D array of ints, it gives you a list of list of ints. It's similar, but it's bigger and slower, and you can't call numpy methods on it. (Althouuh you can still pass it to global numpy functions, at least.)

如果您想通过循环创建2D数组,则可以这样做。

If you wanted to create a 2D array by looping, you could do that.

但是与numpy一样,如果可能的话,您想要做的事情是使用矢量化操作而不是循环。它将变得既简单又快捷,没有任何实际缺点。

But as always with numpy, what you want to do, if at all possible, is used vectorized operations instead of loops. It'll be both a lot simpler and a lot faster, with no real downside.

您可能想要的是 astype

What you probably want is astype:

desired_array = data_X.astype(np.int64)

要比这更简单。而且,除非您想要一个包含Python int 值的 dtype = object 数组(例如,因为某些数字是太大而无法放入本机int64),这正是您想要的。

It's hard to get any simpler than that. And, unless you wanted an array of dtype=object holding Python int values (e.g., because some of your numbers are too big to fit in a native int64), it's exactly what you want.

这篇关于只能将大小为1的数组错误转换为Python标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆