Python Forum
Changing "import numpy as np" to "from numpy import"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing "import numpy as np" to "from numpy import"
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Okay, that might probably be for the better. Thanks. Smile
Reply
#4
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.
Reply
#5
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to import in photos CoderX 2 1,082 Jan-17-2024, 07:00 PM
Last Post: deanhystad
Question Need help with NumPy Histogram function coding Triikey 1 939 May-15-2023, 01:45 PM
Last Post: deanhystad
  "Import" Help Chief816 2 1,633 Apr-30-2023, 10:39 PM
Last Post: Chief816
  Trying to import packages on Pycharm KTam0916 1 1,847 Jun-14-2021, 12:56 PM
Last Post: Larz60+
  math,numpy pyStudent 2 2,407 Apr-26-2021, 08:02 PM
Last Post: pyStudent
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,872 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  How to solve problem 1181 of the URI with numpy. thiagohps 4 2,679 Mar-22-2021, 04:11 PM
Last Post: buran
  How to use Converters in numpy loadtxt? kmasic18 3 5,882 Nov-10-2020, 01:54 PM
Last Post: perfringo
  Singular error with numpy.linalg.inv LennartLindner 1 10,876 Oct-04-2020, 05:15 AM
Last Post: scidam
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,837 May-28-2020, 09:50 AM
Last Post: bryan0901

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020