Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107211 > unrolled thread

Creating a hot vector (numpy)

Started byPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
First post2016-04-18 01:46 +0100
Last post2016-04-18 16:38 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Creating a hot vector (numpy) Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2016-04-18 01:46 +0100
    Re: Creating a hot vector (numpy) Reto Brunner <brunnre8@gmail.com> - 2016-04-18 04:05 +0000
      Re: Creating a hot vector (numpy) Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2016-04-18 16:38 +0100

#107211 — Creating a hot vector (numpy)

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2016-04-18 01:46 +0100
SubjectCreating a hot vector (numpy)
Message-ID<nf1aor$kiu$1@gioia.aioe.org>
Hi all.

I have seen this "trick" to create a hot vector.

In [45]: x
Out[45]: array([0, 1])

In [46]: y
Out[46]: array([1, 1, 1, 0, 0, 1, 0, 0], dtype=uint8)

In [47]: y[:,None]
Out[47]:
array([[1],
       [1],
       [1],
       [0],
       [0],
       [1],
       [0],
       [0]], dtype=uint8)

In [48]: x==y[:,None]
Out[48]:
array([[False,  True],
       [False,  True],
       [False,  True],
       [ True, False],
       [ True, False],
       [False,  True],
       [ True, False],
       [ True, False]], dtype=bool)

In [49]: (x==y[:,None]).astype(np.float32)
Out[49]:
array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 1.,  0.],
       [ 1.,  0.],
       [ 0.,  1.],
       [ 1.,  0.],
       [ 1.,  0.]], dtype=float32)

How does this (step 48) work?

Thanks

[toc] | [next] | [standalone]


#107230

FromReto Brunner <brunnre8@gmail.com>
Date2016-04-18 04:05 +0000
Message-ID<mailman.134.1460952337.6324.python-list@python.org>
In reply to#107211
Hi,
It is called broadcasting an array, have a look here:
http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html

Greetings,
Reto

On Mon, Apr 18, 2016, 02:54 Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
wrote:

> Hi all.
>
> I have seen this "trick" to create a hot vector.
>
> In [45]: x
> Out[45]: array([0, 1])
>
> In [46]: y
> Out[46]: array([1, 1, 1, 0, 0, 1, 0, 0], dtype=uint8)
>
> In [47]: y[:,None]
> Out[47]:
> array([[1],
>        [1],
>        [1],
>        [0],
>        [0],
>        [1],
>        [0],
>        [0]], dtype=uint8)
>
> In [48]: x==y[:,None]
> Out[48]:
> array([[False,  True],
>        [False,  True],
>        [False,  True],
>        [ True, False],
>        [ True, False],
>        [False,  True],
>        [ True, False],
>        [ True, False]], dtype=bool)
>
> In [49]: (x==y[:,None]).astype(np.float32)
> Out[49]:
> array([[ 0.,  1.],
>        [ 0.,  1.],
>        [ 0.,  1.],
>        [ 1.,  0.],
>        [ 1.,  0.],
>        [ 0.,  1.],
>        [ 1.,  0.],
>        [ 1.,  0.]], dtype=float32)
>
> How does this (step 48) work?
>
> Thanks
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#107273

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2016-04-18 16:38 +0100
Message-ID<nf2v10$14co$1@gioia.aioe.org>
In reply to#107230
Às 05:05 de 18-04-2016, Reto Brunner escreveu:
> Hi,
> It is called broadcasting an array, have a look here:
> http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html
> 
So, there are two broadcasts here.
OK.
Thanks.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web