6

I need to use a function in numpy package, say numpy.random.choice (another Python lib function random.choice samples the list uniformly while I want it to do that from some discrete distributions).

My program will be distributed to a lot of people to develop and test. So that means they should also install numpy before they are able to run the code. I'm now trying to find a way to get rid of installing the whole numpy library.

Definitely rewriting the function myself is a solution (for example using alias method). But I'm wondering that is there a way that I can only install the part of numpy related to numpy.random.choice?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
Broan
  • 63
  • 3
  • Why would you want to do that? – cel Jul 18 '15 at 17:33
  • 1
    With numpy specifically, I highly doubt this would be possible. Numpy is written in C and compiled on each machine during installation. To install some small component should therefore be pretty hard. I really think it would be easier to write your own version of `numpy.random.choice` (or extract just that code out of the C source) as a new package – inspectorG4dget Jul 18 '15 at 17:49
  • 4
    Is `random.choice` not an acceptable substitute? – kylieCatt Jul 18 '15 at 17:55
  • @IanAuld As I pointed out in my post, it is not, as it can only generate from uniform distribution. – Broan Jul 18 '15 at 19:40

1 Answers1

4

This is probably not worth the hassle but it's up to you to make that trade-off. numpy.random.choice is not implemented in Python but in a .pyx file which needs to be compiled to C using Cython.

You could refactor it and construct a new package which implements only that functionality (possibly with a few related data structures). But with recent improvements with Python wheels files installation of numpy should be much easier than in the past. So I reckon it's easier to install numpy as it is and accept that you have it as a dependency.

Robert Kern
  • 13,118
  • 3
  • 35
  • 32
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • Thanks for your answer. After searching on internet and asking here, It seems that for simple functions like 'numpy.random.choice', rewriting is a better solution. – Broan Jul 18 '15 at 23:27