Python 将 numpy 数组转换为 0 或 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45648668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:11:49  来源:igfitidea点击:

convert numpy array to 0 or 1

pythonarraysnumpy

提问by Ryan Wang

A = np.array([[0.94366988, 0.86095311, 0.88896715, 0.93630641, 0.74075403, 0.52849619
                  , 0.03094677, 0.85707681, 0.88457925, 0.67279696, 0.26601085, 0.4823794
                  , 0.74741157, 0.78575729, 0.00978911, 0.9203284, 0.02453695, 0.84884703
                  , 0.2050248, 0.03703224, 0.92931392, 0.11930532, 0.01411064, 0.7832698
                  , 0.58188015, 0.66897565, 0.75119007, 0.01323558, 0.03402649, 0.99735115
                  , 0.21031727, 0.78123225, 0.6815842, 0.46647604, 0.66323375, 0.03424828
                  , 0.08031627, 0.76570656, 0.34760863, 0.06177743, 0.6987531, 0.4106426
                  , 0.6648871, 0.02776868, 0.93053125, 0.46395717, 0.23971605, 0.9771735
                  , 0.66202407, 0.10482388]])

Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5)

将 a 的条目转换为 0(如果激活 <= 0.5)或 1(如果激活 > 0.5)

for i in range(A.shape[1]):
if A[i]>0.5:
    Y_prediction[i] = 1
else:
    Y_prediction[i] = 0

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

ValueError:包含多个元素的数组的真值不明确。使用 a.any() 或 a.all()

And how to use vectorize this thx

以及如何使用矢量化这个 thx

回答by jezrael

I think you need vectorized function np.where:

我认为你需要矢量化函数np.where

B = np.where(A > 0.5, 1, 0)
print (B)
[[1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0
  1 0 0 1 0 1 0 1 0 0 1 1 0]]


B = np.where(A <= 0.5, 0, 1)
print (B)
[[1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0
  1 0 0 1 0 1 0 1 0 0 1 1 0]]

But better is holdenweb solutionif need convert to 0and 1only.

但如果只需要转换为并且只转换为Holdenweb 解决方案更好。01

np.whereis better if need convert to another scalars like 5and 10or aand b:

np.where如果需要转换为另一个标量,如5and10aand ,则更好b

C = np.where(A > 0.5, 5, 10)
print (C)
[[ 5  5  5  5  5  5 10  5  5  5 10 10  5  5 10  5 10  5 10 10  5 10 10  5
   5  5  5 10 10  5 10  5  5 10  5 10 10  5 10 10  5 10  5 10  5 10 10  5
   5 10]]

D = np.where(A > 0.5, 'a', 'b')
print (D)
[['a' 'a' 'a' 'a' 'a' 'a' 'b' 'a' 'a' 'a' 'b' 'b' 'a' 'a' 'b' 'a' 'b' 'a'
  'b' 'b' 'a' 'b' 'b' 'a' 'a' 'a' 'a' 'b' 'b' 'a' 'b' 'a' 'a' 'b' 'a' 'b'
  'b' 'a' 'b' 'b' 'a' 'b' 'a' 'b' 'a' 'b' 'b' 'a' 'a' 'b']]

Timings:

时间

np.random.seed(223)
A = np.random.rand(1,1000000)

#jez
In [64]: %timeit np.where(A > 0.5, 1, 0)
100 loops, best of 3: 7.58 ms per loop

#holdenweb
In [65]: %timeit (A > 0.5).astype(int)
100 loops, best of 3: 3.47 ms per loop

#stamaimer
In [66]: %timeit element_wise_round(A)
1 loop, best of 3: 318 ms per loop

回答by holdenweb

Standard numpybroadcasting can be used to compare each element with a scalar value, yielding a Boolean for each element. The ndarray.astypemethod then converts the Truevalues to 1 and the Falsevalues to zero.

标准numpy广播可用于将每个元素与标量值进行比较,从而为每个元素生成一个布尔值。ndarray.astype然后,该方法将True值转换为 1,并将False值转换为零。

In [16]: (A > 0.5).astype(int)
Out[16]:
array([[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0,
        0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0,
        1, 0, 0, 1, 1, 0]])

回答by Tkanno

np.random.seed(223)
A = np.random.rand(1000000)
A = [0 if i <=0.5 else 1 for i in A]

回答by Aurimas Petrevicius

The problem with your code is the dimension of A[i] in your code. A is initialised as matrix[1,n] (1 row and n columns - you have double [[]] brackets in np.array([[]])), so you reference of type A[i] actually means "the whole i'th row, or an array(n) in this case). I'm quite new to python and numpy, but to access individual cells and set each to 1 or 0 could be coded this way (of course, only if you are forced to use FOR loops here):

您的代码的问题是代码中 A[i] 的维度。A 被初始化为矩阵 [1,n](1 行和 n 列 - 你在 np.array([[]]) 中有双 [[]] 括号),所以你引用类型 A[i] 实际上意味着“整个第 i 行,或者在这种情况下是一个数组(n)。我对 python 和 numpy 很陌生,但是访问单个单元格并将每个单元格设置为 1 或 0 可以这样编码(当然,只有当你被迫在这里使用 FOR 循环):

 for i in range(A.shape[1]):
 if A[0,i]>0.5:
    Y_prediction[0,i] = 1
 else:
    Y_prediction[0,i] = 0

But,definitely, the best solution, as others described above, is to avoid for loop.

但是,毫无疑问,最好的解决方案是避免 for 循环。

回答by stamaimer

You can make the built-in roundfunction element wise use np.vectorize.

您可以round明智地使用内置函数元素np.vectorize

import numpy as np

element_wise_round = np.vectorize(round, otypes=[np.int])

print element_wise_round(A)

回答by Saugat Pudasaini

this may solve your issue

这可能会解决您的问题

a=1
b=0
predict=list()
for i in A:
    if i>0.5:
        predict.append(a)
    else:
        predict.append(b)
print(predict)

we first assign value a to 0 and b to 1 but you can directly use 1 and 0 on append function'A' contains the list in form of probability from which we assign the new list name predict and after comparing each items in list we put value into list by using append methordpredict list contain your ans

我们首先将值 a 分配给 0 并将 b 分配给 1,但您可以直接在附加函数“A”上使用 1 和 0使用附加方法将值放入列表中预测列表包含您的答案