13.2 C
London
Tuesday, July 2, 2024
HomePythonFix Common Errors in PythonHow to Fix: NameError name ‘np’ is not defined

How to Fix: NameError name ‘np’ is not defined

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

One of the most common errors you may encounter when using Python is:

NameError: name 'np' is not defined

This error occurs when you import the python library NumPy, but fail to give it the alias of np when importing it.

The following examples illustrate how this problem occurs and how to fix it.

Example 1: import numpy

Suppose you import the NumPy library using the following code:

import numpy

If you then attempt to define a numpy array of values, you’ll get the following error:

#define numpy array
x = np.random.normal(loc=0, scale=1, size=20)

#attempt to print values in arrary
print(x)

Traceback (most recent call last): 
----> 1 x = np.random.normal(loc=0, scale=1, size=20)
      2 print(x)

NameError: name 'np' is not defined

To fix this error, you need provide the alias of np when importing NumPy:

import numpy as np

#define numpy array
x = np.random.normal(loc=0, scale=1, size=20)

#print values in arrary
print(x)

[-0.93937656 -0.49448118 -0.16772964  0.44939978 -0.80577905  0.48042484
  0.30175551 -0.15672656 -0.26931062  0.38226115  1.4472055  -0.13668984
 -0.74752684  1.6729974   2.25824518  0.77424489  0.67853607  1.46739364
  0.14647622  0.87787596]

Example 2: from numpy import *

Suppose you import all functions from the NumPy library using the following code:

from numpy import *

If you then attempt to define a numpy array of values, you’ll get the following error:

#define numpy array
x = np.random.normal(loc=0, scale=1, size=20)

#attempt to print values in arrary
print(x)

Traceback (most recent call last): 
----> 1 x = np.random.normal(loc=0, scale=1, size=20)
      2 print(x)

NameError: name 'np' is not defined

To fix this error, you need provide the alias of np when importing NumPy:

import numpy as np

#define numpy array
x = np.random.normal(loc=0, scale=1, size=20)

#print values in arrary
print(x)

[-0.93937656 -0.49448118 -0.16772964  0.44939978 -0.80577905  0.48042484
  0.30175551 -0.15672656 -0.26931062  0.38226115  1.4472055  -0.13668984
 -0.74752684  1.6729974   2.25824518  0.77424489  0.67853607  1.46739364
  0.14647622  0.87787596]

Alternatively, you can choose to not use the np syntax at all:

import numpy

#define numpy array
x = numpy.random.normal(loc=0, scale=1, size=20)

#print values in arrary
print(x)

[-0.93937656 -0.49448118 -0.16772964  0.44939978 -0.80577905  0.48042484
  0.30175551 -0.15672656 -0.26931062  0.38226115  1.4472055  -0.13668984
 -0.74752684  1.6729974   2.25824518  0.77424489  0.67853607  1.46739364
  0.14647622  0.87787596]

Note: The syntax “import numpy as np” is commonly used because it offers a more concise way to use NumPy functions. Instead of typing “numpy” each time, you can simply type in “np” which is quicker and easier to read.

Additional Resources

How to Fix: NameError name ‘pd’ is not defined
How to Fix: No module named pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories