Numpy - The Basics

Numpy is the most powerful library and is used for scientific computing. It provides a high-performance multidimensional array Object and tools for working on those arrays. It is a powerful N-dimensional array object which is Linear Algebra for python.


Numpy arrays essential comes in two flavors - Vector and Matrices. Vector is strictly 1-dimensional (1-D) array and Matrics are 2-dimensional (2-D) array. Matrices can also have only one row and one column.



In Bulletins, What is Numpy?


  • NumPy is an open-source numerical Python library.

  • NumPy contains a multi-dimensional array and matrix data structures.

  • It can be utilized to perform mathematical operations on arrays such as trigonometry, statistical and algebra functions.

  • NumPy is an extension of Numeric and num-array.

  • It contains a large number of mathematical, algebraic, and transformation functions.

  • It also contains random number generators.

  • NumPy is a wrapper around a library implemented in C.

  • Pandas objects rely heavily on NumPy objects. Pandas extend Numpy.


Data Types in Numpy



Importing Numpy


#alias 'np' reference to the library.


import numpy as np

import numpy


Numpy Vs List


Numpy Array objects and Python List Objects are similar to each other, they can store data, indexed and iterated. Numpy uses less memory, is faster and convenient to use than Python lists.


Mathematical operations such as add, subtract, multiply, divide and exponential is not possible with Lists.


Example # 1


# In-built Python List

In [3] : p_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]


In [4] : print((p_list) * 2)

Out[4] : TypeError: unsupported operand type for *:'list' and 'int'


# Numpy Arrays In [5]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [6]: print((numpy_array) * 2) Out[6]: [2 4 6 8 10 12 14 16 18] Example # 2 # In-built Python List In [7]: print(p_list + p_list) Out[7]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Numpy Arrays In [8]: print(numpy_array + numpy_array) Out[8]: [2 4 6 8 10 12 14 16 18]

Creating NumPy Array Structure

In []: simple_list = [101,102,103,104,105,106,107,108,109,110]

In []: np.array(simple_list) Out[]: array([101, 102, 103, 104, 105, 106, 107, 108, 109, 110]) In []: simple_list_of_lists = [[10,11,12],[20,21,22],[30,31,32]]

In []: np.array(simple_list_of_lists) Out]: array([[10, 11, 12], [20, 21, 22], [30, 31, 32]])

arange

np.arange( ) is the best way to create large matrices with n-dimensional. Return evenly spaced values within a given interval as input. np.arange(start=None, stop=None, step=None, dtype=None)

  • start — starting the array from the start number.

  • stop — end the array (excluded in stop value)

  • step — jump the value

  • dtype — the type of array or matrices

In []: np.arange(0,20) Out[]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) # Specify start, stop and step values as input In []: np.arange(0,20,4) Out[]: array([ 0, 4, 8, 12, 16])

0's and 1's - Generate arrays of 0's or 1's

# Specify the count of 0's or 1's required in the array In []: np.zeros(10) Out[]: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In []: np.ones(10) Out[]: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) # Specify the number of rows by columns In []: np.zeros((4,3)) Out[]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) In []: np.ones((4,5)) Out[]: array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]])

linspace

np.linspace ( ) is the best way to create any size of the evenly spaced array or matrices between specified interval. By default, np.linspace( ) created one dimensional array, if you want to create 2-Dimensional or 3-Dimensional matrices, you can use np.reshape ( ) with np.linspace. np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) np.linspace ( ) has 6 parameters :

  1. start — Starting value of the sequence

  2. stop — Last value of the sequence

  3. num — Number of samples to be generated

  4. endpoint — This is a boolean value. If the value of the endpoint is true, then stop is the last sample of the sequence. The default value of the endpoint is true.

  5. retstep — Default value is true. If the value of retstep is true then return samples and steps(the difference between 2 samples)

  6. dtype — The type of array or matrices


In []: np.linspace(0,20,5) Out[]: array([ 0., 5., 10., 15., 20.]) In []: np.linspace(5,10) Out[]: array([ 5. , 5.10204082, 5.20408163, 5.30612245, 5.40816327, 5.51020408, 5.6122449 , 5.71428571, 5.81632653, 5.91836735, 6.02040816, 6.12244898, 6.2244898 , 6.32653061, 6.42857143, 6.53061224, 6.63265306, 6.73469388, 6.83673469, 6.93877551, 7.04081633, 7.14285714, 7.24489796, 7.34693878, 7.44897959, 7.55102041, 7.65306122, 7.75510204, 7.85714286, 7.95918367, 8.06122449, 8.16326531, 8.26530612, 8.36734694, 8.46938776, 8.57142857, 8.67346939, 8.7755102 , 8.87755102, 8.97959184, 9.08163265, 9.18367347, 9.28571429, 9.3877551 , 9.48979592, 9.59183673, 9.69387755, 9.79591837, 9.89795918, 10. ])

eye

Return a 2-D array with ones on the diagonal and zeros elsewhere. Also called an identity matrix

In []: np.eye(10) Out[]: array([[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])

Mathematical Functions

Let's consider two lists or array a = [1,2] b = [3,4] # Adding two lists In []: np.add(a,b) Out[]: array([4, 6]) # To Subtract from one list to another In []: np.subtract(a,b) Out[]: array([-2, -2]) # To multiply two lists In []: np.multiply(a,b) Out[]: array([3, 8]) # To divide one list to another In []: np.divide(a,b) Out[]: array([0.33333333, 0.5 ]) # To expoential In []: np.power(a,b) Out[]: array([ 1, 16], dtype=int32) In []: np.power(a,2) Out[]: array([1, 4], dtype=int32) # To get the reminder om division In []: np.mod(a,b) Out[]: array([1, 2], dtype=int32) In []: np.remainder(a,b) Out[]: array([1, 2], dtype=int32) For a more in-depth guide to NumPy, you can refer to the documentation at https://docs.scipy.org/doc/. I hope you got something out of this blog. If you found this post illuminating at all, consider sharing this post and following for more upcoming content.

13 views0 comments

Recent Posts

See All