Introduction to Numpy

Introduction to Numpy

Are you trying to find a resource that will teach you everything there is to know about the NumPy Python library? If yes, then allow me to inform you that I will now begin a new blog post series about NumPy. Today's blog will be the first in the series, where we will teach you some fundamental Numpy concepts. So let's get started without further delay.

What is Numpy?

NumPy is a fundamental library for doing scientific computing in Python. At its core, this library provides us with an n-dimensional, homogenous array object (aka ndarray) on which we can perform a lot of mathematical, logical, shape manipulation, sorting, basic linear algebra, and basic statistical operations.

In addition to ndarray, the NumPy library also provides us with various derived objects from ndarray (such as masked arrays and matrices)

How did Numpy get introduced?

NumPy (short for Numerical Python) was created by Travis Oliphant in 2005. Oliphant was a graduate student at the University of Utah and was working on a project that required efficient numerical computation with Python. He noticed that the existing numerical computing libraries for Python at the time (such as Numeric and Numarray) were not sufficient for his needs, so he decided to create a new library that would address these deficiencies.

Oliphant developed NumPy as an open-source project and released it under the BSD license. The library was designed to provide a high-performance multidimensional array object and a large collection of functions and methods for working with these arrays. It was designed to be used in scientific and technical computing applications and was intended to be a replacement for the existing numerical computing libraries for Python.

NumPy quickly gained popularity in the scientific and technical computing community due to its efficiency and ease of use. It became an essential library for many scientific and technical computing applications and is now widely used in fields such as machine learning, data analysis, and scientific simulations.

How Numpy array is different from other python datatypes

  1. NumPy arrays are static which means that when we will change the size of the ndarray then basically a new array will be created by copying all the items from the original array to the new array and the original array will be deleted.

  2. The elements in a NumPy array are all of the same data type.

  3. NumPy arrays are implemented in C the advanced mathematical operations are executed more efficiently and with less code, than is possible using Python’s built-in sequences.

Ways to create a Numpy array

There are a few important methods you need to be aware of, including identity, dtype, and reshape.

  1. Using the np.array method

     import numpy as np
    
     # 1 dimensional ndarray
     ndarray_1d = np.array([1,2,3,4,5,6])
     # 2 dimensional ndarray
     ndarray_2d = np.array([1,2,[3,4]])
     # 3 dimensional ndarray
     ndarray_3d = np.array([1,2,[3,4,[5,6]]])
    
  2. Using arange method

     import numpy as np
    
     # arange method takes 2 arguments { start:inclusive , end:exclusive }
     ndarray = np.arange(1,6)
     print(ndarray)
    
     # Output : [1 2 3 4 5]
    
  3. Using the ones or zeros method

     import numpy as np
    
     # In both methods we can either pass list, tuple or the dimensions
     list = [1,2,3]
     nd_array_1 = np.ones(list)
     tuple = (1,2,3)
     nd_array_1 = np.ones(tuple)
     # Passing the dimensions
     nd_array_1 = np.ones((2,3))
    

  4. Using linspace method for creating a NumPy array of equally spaced numbers over a specified interval

     import numpy as np
    
     array = np.linspace(-10,10,10,dtype=int)
     print(array)
    
     # Output : [-10  -8  -6  -4  -2   1   3   5   7  10]
    
  5. Using random method we can create a NumPy array of specific dimensions having random values

     import numpy as np
     # Generating Numpy array having random vlaues but with specific shape
     array = np.random.random((1,5))
     # Generating Numpy array having n random values
     print(array)
    

Some additional important methods

There are a few important methods, such as reshape, dtype, and identity, that you must know.

  • reshape method is used to change the shape of the Numpy array

      import numpy as np
      ndarray = np.arange(1,7)
      ndarray = ndarray.reshape(2,3)
      print(ndarray)
    
      # Output  [[1 2 3]
      #         [4 5 6]]
    

    Note: While using the reshape method the product of dimensions must be equal to the total elements in the array

  • dtype method can be used to change the data type of the elements present in the array

  • identity method is used to create an identity matrix of specified dimensions

That's all for now, and I hope this blog provided some useful information for you. Additionally, don't forget to check out my 👉 TWITTER handle if you want to receive daily content relating to data science, mathematics for machine learning, Python, and SQL in form of threads.

Did you find this article valuable?

Support Yuvraj Singh by becoming a sponsor. Any amount is appreciated!