[SciPy-User] Scipy and information theory

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Scipy and information theory

Iyán Méndez Veiga
Hi,

I discovered recently some interesting functions in scipy that are useful in
the context of information theory. In particular, xlogy, entr and rel_entr.
However, for the calculations I want to do I am interested in using log2
instead of natural logarithm. I was expecting to be able to choose the base
with this functions but it is not possible.

Do you think it would be a nice feature to add to scipy? Maybe not for a
general base, but at least the bases that are available in numpy (log2,
log10).

The workaround I am using with xlogy(x,y) (and similar functions) is
multiplying x array by log2(e).

Best wishes,
Iyán


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Scipy and information theory

ralfgommers
Hi Iyán,


On Thu, Nov 7, 2019 at 12:21 PM Iyán Méndez Veiga <[hidden email]> wrote:
Hi,

I discovered recently some interesting functions in scipy that are useful in
the context of information theory. In particular, xlogy, entr and rel_entr.
However, for the calculations I want to do I am interested in using log2
instead of natural logarithm. I was expecting to be able to choose the base
with this functions but it is not possible.

Do you think it would be a nice feature to add to scipy? Maybe not for a
general base, but at least the bases that are available in numpy (log2,
log10).

This does sound like a useful feature to add to these functions. I'm not sure how easy it is to add a keyword to those functions though, since the ufunc machinery may be limited in flexibility. Maybe someone else can say straight away. If you want to start looking into implementing this, here are some notes of where to start: http://scipy.github.io/devdocs/dev/core-dev/index.html#scipy-special

Cheers,
Ralf


The workaround I am using with xlogy(x,y) (and similar functions) is
multiplying x array by log2(e).

Best wishes,
Iyán


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Scipy and information theory

Robert Lucente - Pipeline.Com
> interested in using log2 instead of natural logarithm. I was expecting to
be able to choose the base with this functions but it is not possible
Why not use Python's built in fcn?

https://docs.python.org/3/library/math.html#math.log

math.log(x[, base])

    With one argument, return the natural logarithm of x (to base e).

    With two arguments, return the logarithm of x to the given base,
calculated as log(x)/log(base).



-----Original Message-----
From: SciPy-User <scipy-user-bounces+rlucente=[hidden email]> On
Behalf Of [hidden email]
Sent: Friday, November 8, 2019 12:00 PM
To: [hidden email]
Subject: SciPy-User Digest, Vol 195, Issue 2

Send SciPy-User mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.python.org/mailman/listinfo/scipy-user
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific than
"Re: Contents of SciPy-User digest..."


Today's Topics:

   1. Scipy and information theory (Iy?n M?ndez Veiga)
   2. Re: Scipy and information theory (Ralf Gommers)


----------------------------------------------------------------------

Message: 1
Date: Thu, 07 Nov 2019 13:11:55 +0100
From: Iy?n M?ndez Veiga <[hidden email]>
To: [hidden email]
Subject: [SciPy-User] Scipy and information theory
Message-ID: <20976252.W2eyRVhfPJ@theotp40>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

I discovered recently some interesting functions in scipy that are useful in
the context of information theory. In particular, xlogy, entr and rel_entr.
However, for the calculations I want to do I am interested in using log2
instead of natural logarithm. I was expecting to be able to choose the base
with this functions but it is not possible.

Do you think it would be a nice feature to add to scipy? Maybe not for a
general base, but at least the bases that are available in numpy (log2,
log10).

The workaround I am using with xlogy(x,y) (and similar functions) is
multiplying x array by log2(e).

Best wishes,
Iy?n




------------------------------

Message: 2
Date: Fri, 8 Nov 2019 10:54:32 -0500
From: Ralf Gommers <[hidden email]>
To: SciPy Users List <[hidden email]>
Subject: Re: [SciPy-User] Scipy and information theory
Message-ID:
        <CABL7CQh=[hidden email]>
Content-Type: text/plain; charset="utf-8"

Hi Iy?n,


On Thu, Nov 7, 2019 at 12:21 PM Iy?n M?ndez Veiga <[hidden email]> wrote:

> Hi,
>
> I discovered recently some interesting functions in scipy that are
> useful in the context of information theory. In particular, xlogy,
> entr and rel_entr.
> However, for the calculations I want to do I am interested in using
> log2 instead of natural logarithm. I was expecting to be able to
> choose the base with this functions but it is not possible.
>
> Do you think it would be a nice feature to add to scipy? Maybe not for
> a general base, but at least the bases that are available in numpy
> (log2, log10).
>

This does sound like a useful feature to add to these functions. I'm not
sure how easy it is to add a keyword to those functions though, since the
ufunc machinery may be limited in flexibility. Maybe someone else can say
straight away. If you want to start looking into implementing this, here are
some notes of where to start:
http://scipy.github.io/devdocs/dev/core-dev/index.html#scipy-special

Cheers,
Ralf


> The workaround I am using with xlogy(x,y) (and similar functions) is
> multiplying x array by log2(e).
>
> Best wishes,
> Iy?n
>
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/scipy-user/attachments/20191108/f06b5c12/a
ttachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user


------------------------------

End of SciPy-User Digest, Vol 195, Issue 2
******************************************

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Scipy and information theory

Robert Lucente - Pipeline.Com
In reply to this post by ralfgommers
> interested in using log2 instead of natural logarithm. I was expecting
> to be able to choose the base with this functions but it is not
> possible

Why not use Python's built in fcn?

https://docs.python.org/3/library/math.html#math.log2

math.log2(x)

    Return the base-2 logarithm of x. This is usually more accurate than
log(x, 2).

    New in version 3.3.

-----Original Message-----
From: Robert Lucente - Pipeline.Com <[hidden email]>
Sent: Friday, November 8, 2019 12:11 PM
To: '[hidden email]' <[hidden email]>
Cc: Robert Gmail Backup 2 Lucente ([hidden email])
<[hidden email]>
Subject: Re: Scipy and information theory

> interested in using log2 instead of natural logarithm. I was expecting
> to be able to choose the base with this functions but it is not
> possible
Why not use Python's built in fcn?

https://docs.python.org/3/library/math.html#math.log

math.log(x[, base])

    With one argument, return the natural logarithm of x (to base e).

    With two arguments, return the logarithm of x to the given base,
calculated as log(x)/log(base).



-----Original Message-----
From: SciPy-User <scipy-user-bounces+rlucente=[hidden email]> On
Behalf Of [hidden email]
Sent: Friday, November 8, 2019 12:00 PM
To: [hidden email]
Subject: SciPy-User Digest, Vol 195, Issue 2

Send SciPy-User mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.python.org/mailman/listinfo/scipy-user
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific than
"Re: Contents of SciPy-User digest..."


Today's Topics:

   1. Scipy and information theory (Iy?n M?ndez Veiga)
   2. Re: Scipy and information theory (Ralf Gommers)


----------------------------------------------------------------------

Message: 1
Date: Thu, 07 Nov 2019 13:11:55 +0100
From: Iy?n M?ndez Veiga <[hidden email]>
To: [hidden email]
Subject: [SciPy-User] Scipy and information theory
Message-ID: <20976252.W2eyRVhfPJ@theotp40>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

I discovered recently some interesting functions in scipy that are useful in
the context of information theory. In particular, xlogy, entr and rel_entr.
However, for the calculations I want to do I am interested in using log2
instead of natural logarithm. I was expecting to be able to choose the base
with this functions but it is not possible.

Do you think it would be a nice feature to add to scipy? Maybe not for a
general base, but at least the bases that are available in numpy (log2,
log10).

The workaround I am using with xlogy(x,y) (and similar functions) is
multiplying x array by log2(e).

Best wishes,
Iy?n




------------------------------

Message: 2
Date: Fri, 8 Nov 2019 10:54:32 -0500
From: Ralf Gommers <[hidden email]>
To: SciPy Users List <[hidden email]>
Subject: Re: [SciPy-User] Scipy and information theory
Message-ID:
        <CABL7CQh=[hidden email]>
Content-Type: text/plain; charset="utf-8"

Hi Iy?n,


On Thu, Nov 7, 2019 at 12:21 PM Iy?n M?ndez Veiga <[hidden email]> wrote:

> Hi,
>
> I discovered recently some interesting functions in scipy that are
> useful in the context of information theory. In particular, xlogy,
> entr and rel_entr.
> However, for the calculations I want to do I am interested in using
> log2 instead of natural logarithm. I was expecting to be able to
> choose the base with this functions but it is not possible.
>
> Do you think it would be a nice feature to add to scipy? Maybe not for
> a general base, but at least the bases that are available in numpy
> (log2, log10).
>

This does sound like a useful feature to add to these functions. I'm not
sure how easy it is to add a keyword to those functions though, since the
ufunc machinery may be limited in flexibility. Maybe someone else can say
straight away. If you want to start looking into implementing this, here are
some notes of where to start:
http://scipy.github.io/devdocs/dev/core-dev/index.html#scipy-special

Cheers,
Ralf


> The workaround I am using with xlogy(x,y) (and similar functions) is
> multiplying x array by log2(e).
>
> Best wishes,
> Iy?n
>
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/scipy-user/attachments/20191108/f06b5c12/a
ttachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user


------------------------------

End of SciPy-User Digest, Vol 195, Issue 2
******************************************

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Scipy and information theory

Iyán Méndez Veiga
El viernes, 8 de noviembre de 2019 18:13:32 (CET) Robert Lucente -
Pipeline.Com escribió:
> Why not use Python's built in fcn?
>
> https://docs.python.org/3/library/math.html#math.log2
>
> math.log2(x)

Because I want to avoid long (and slow) for loops and take advantage of numpy/
scipy element wise operations. That is why I prefer numpy.log2(x) over
math.log2(x). My x is a high dimension array, not a scalar.

> This does sound like a useful feature to add to these functions. I'm not
> sure how easy it is to add a keyword to those functions though, since the
> ufunc machinery may be limited in flexibility. Maybe someone else can say
> straight away. If you want to start looking into implementing this, here are
> some notes of where to start:
> http://scipy.github.io/devdocs/dev/core-dev/index.html#scipy-special

Thanks Ralf, I'll have a look. Also waiting if someone else can say if it
would be easy or hard to implement.

Best wishes,
Iyán


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Scipy and information theory

Robert Lucente - Pipeline.Com
> Because I want to avoid long (and slow) for loops and take advantage of
numpy/ scipy element wise operations
I am embarrassed

That was an amateur mistake on my part :-(

-----Original Message-----
From: SciPy-User <scipy-user-bounces+rlucente=[hidden email]> On
Behalf Of Iyán Méndez Veiga
Sent: Sunday, November 10, 2019 7:30 AM
To: SciPy Users List <[hidden email]>
Subject: Re: [SciPy-User] Scipy and information theory

El viernes, 8 de noviembre de 2019 18:13:32 (CET) Robert Lucente -
Pipeline.Com escribió:
> Why not use Python's built in fcn?
>
> https://docs.python.org/3/library/math.html#math.log2
>
> math.log2(x)

Because I want to avoid long (and slow) for loops and take advantage of
numpy/ scipy element wise operations. That is why I prefer numpy.log2(x)
over math.log2(x). My x is a high dimension array, not a scalar.

> This does sound like a useful feature to add to these functions. I'm
> not sure how easy it is to add a keyword to those functions though,
> since the ufunc machinery may be limited in flexibility. Maybe someone
> else can say straight away. If you want to start looking into
> implementing this, here are some notes of where to start:
> http://scipy.github.io/devdocs/dev/core-dev/index.html#scipy-special

Thanks Ralf, I'll have a look. Also waiting if someone else can say if it
would be easy or hard to implement.

Best wishes,
Iyán


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Scipy and information theory

ralfgommers


On Sun, Nov 10, 2019 at 5:25 PM Robert Lucente - Pipeline.Com <[hidden email]> wrote:
> Because I want to avoid long (and slow) for loops and take advantage of
numpy/ scipy element wise operations
I am embarrassed

That was an amateur mistake on my part :-(

-----Original Message-----
From: SciPy-User <scipy-user-bounces+rlucente=[hidden email]> On
Behalf Of Iyán Méndez Veiga
Sent: Sunday, November 10, 2019 7:30 AM
To: SciPy Users List <[hidden email]>
Subject: Re: [SciPy-User] Scipy and information theory

El viernes, 8 de noviembre de 2019 18:13:32 (CET) Robert Lucente -
Pipeline.Com escribió:
> Why not use Python's built in fcn?
>
> https://docs.python.org/3/library/math.html#math.log2
>
> math.log2(x)

Because I want to avoid long (and slow) for loops and take advantage of
numpy/ scipy element wise operations. That is why I prefer numpy.log2(x)
over math.log2(x). My x is a high dimension array, not a scalar.

> This does sound like a useful feature to add to these functions. I'm
> not sure how easy it is to add a keyword to those functions though,
> since the ufunc machinery may be limited in flexibility. Maybe someone
> else can say straight away. If you want to start looking into
> implementing this, here are some notes of where to start:
> http://scipy.github.io/devdocs/dev/core-dev/index.html#scipy-special

Thanks Ralf, I'll have a look. Also waiting if someone else can say if it
would be easy or hard to implement.

Right after I wrote that Josh just posted https://github.com/scipy/scipy/issues/11029#issuecomment-551968035, which states "to add a keyword argument ... and the current Cython special codegen can’t handle that automatically". So I think that confirms what I suspected: it's not really doable right now. Adding a new function is not justified; a Python wrapper could be but it's not great for performance - you could try that and keep the overhead for the current default behavior is low as possible, then report some benchmark result. That should help us decide. Probably best to open an issue or PR for that for further discussion.

Cheers,
Ralf

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user