Python Forum

Full Version: Changing "import numpy as np" to "from numpy import"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I'm new to this website and need to help. I'm trying to change code that loads the python library by "import numpy as np" to loading the library as "from numpy import".

So, I'm using this code:

# load the numpy numerical python library
import numpy as np

# define a function "Area", that performs the calculations
def Area(r, N):

   # compute the width of each rectangle
   step = (r[1] - r[0]) / N

   # split the interval x_start to x_stop into "step" spaced intervals
   x = np.arange(r[0], r[1], step)

   # The midpoint of these intervals is half a step
   # into the middle of them.
   x_m = x + (step / 2)

   # Compute the value of the function f(x) at the
   # midpoints of the intervals.
   f = 0.1 * (x_m**2) + np.cos(0.8 * np.pi * x_m) + np.exp(-x_m**2 / 10)

   # Sum up the areas of the triangles, where area is given
   # by height into width.
   A = np.sum(f * step)

   return A

A = Area([0, 10], 5)
print(A)
...and replacing "import numpy as np" with "from numpy import *". Here's what I tried to do:

# load the numpy numerical python library
from numpy import arange
import math

# define a function "Area", that performs the calculations
def Area(r, N):

   # compute the width of each rectangle
   step = (r[1] - r[0]) / N

   # split the interval x_start to x_stop into "step" spaced intervals
   x = arange(r[0], r[1], step)

   # The midpoint of these intervals is half a step
   # into the middle of them.
   x_m = x + (step / 2)

   # Compute the value of the function f(x) at the
   # midpoints of the intervals.
   f = 0.1 * (x_m**2) + math.cos(0.8 * math.pi * x_m) + math.exp(-x_m**2 / 10)

   # Sum up the areas of the triangles, where area is given
   # by height into width.
   A = math.sum(f * step)

   return A

A = Area([0, 10], 5)
print(A)
I ended up with the "TypeError: only size-1 arrays can be converted to Python scalars" error on Python 3 on GDB Online.

Can anyone help?
The functions in numpy are generally designed to work on vectors, while math is designed to work with scalars (ints or floats). So you can't just replace the numpy functions with the math ones. You need to import everything with an np before it. But that's a problem, because if you do from numpy import sum that overrides the Python built-in sum. That could totally mess up other code in Python. This is why you stick with import numpy as np, so you can distinguish between sum and np.sum.
Okay, that might probably be for the better. Thanks. Smile
You can also write
from numpy import arange, sum as asum
and use asum instead of sum. However, using qualified names is more ergonomic, especially if you start moving pieces of code from one file to another. The runtime cost of direct attribute access is low.
(Nov-28-2018, 03:18 AM)ClintWestwood Wrote: [ -> ]I'm trying to change code that loads the python library by "import numpy as np" to loading the library as "from numpy import".

I think you should reconsider what you're trying to do. You're turning well structured code into spaghetti. Why would you purposefully make it uglier/harder to read/worse?