NumPy Arrays

chowdhurysayebislam

Chowdhury Sayeb Islam

Posted on December 31, 2022

NumPy Arrays

## NumPy

NumPy (or Numpy) is a Linear Algebra Library for Python. The reason it is so important for Data Science with Python cause almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks. NumPy is also incredibly fast as it has bindings to C libraries. One should use Arrays instead of Lists because of its efficiency is more than the Python's lists. We will now only learn the basics of NumPy. To get started, we need to install it!

Installation instructions

It is highly recommended, you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra Libraries) all sync up with the use of a condo install. If you have Anaconda, install NumPy by going to your terminal or command prompt and type,

conda install numpy

If you cannot download Anaconda in your PC then you can also use Google Colab, also for more-http://docs.scipy.org/doc/numpy-1.10.1/user/install.html
Using numpy

Once you have installed numpy you can import it as a library or in Google Colab you have to write the below sentence in the first cell and ran it. Afterwards, you can do whatever you want.

import numpy as np

Enter fullscreen mode Exit fullscreen mode

NumPy Arrays

NumPy Arrays are the main way we will use NumPy. NumPy Arrays essentially come in two flavors- Vectors and Matrices. Vectors are strictly 1D Arrays and Matrices are 2D Arrays ( you should know that Matrix can it still have one row and one column. One Row Matrix is also called as Row Vector and one Column Matrix is also called as Column Vector.

Creating NumPy Arrays
From a Python List

We can create an array by directly converting a list or list of lists.

my_list = [1,2,3]
my_list
Enter fullscreen mode Exit fullscreen mode
np.array(my_list)
Enter fullscreen mode Exit fullscreen mode
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
Enter fullscreen mode Exit fullscreen mode
np.array(my_matrix)
Enter fullscreen mode Exit fullscreen mode

Built-in methods

There are lots of built-in ways to generate Arrays.

arange- '.arange()'

Return evenly spaced values within a given interval.

np.arange(0,10)
Enter fullscreen mode Exit fullscreen mode
np.arange(0,11,2)
Enter fullscreen mode Exit fullscreen mode

*zeros and ones
*
- '.zeroes()', '.ones()'
Generate arrays of zeros or ones.

np.zeros(3)
Enter fullscreen mode Exit fullscreen mode
np.zeros((5,5))
Enter fullscreen mode Exit fullscreen mode
np.ones(3)
Enter fullscreen mode Exit fullscreen mode
np.ones((3,3))
Enter fullscreen mode Exit fullscreen mode

linspace- '.linspace()'
Return evenly spaced numbers over a specified interval.

np.linspace(0,10,3)
Enter fullscreen mode Exit fullscreen mode
np.linspace(0,10,50)
Enter fullscreen mode Exit fullscreen mode

eye

Creates an Identity Matrix.

np.eye(4)
Enter fullscreen mode Exit fullscreen mode

**

Random

**

NumPy also has lots of ways to create random number Arrays.

*rand
*

Create an array of the given shape and populate it with random samples from a uniform distribution over [0,1)

np.random.rand(2)
Enter fullscreen mode Exit fullscreen mode
np.random.rand(5,5)
Enter fullscreen mode Exit fullscreen mode

randn

Return a sample (or samples) from the "standard normal" distribution. Unlike ".rand()" which is uniform,

np.random.randn(2)
Enter fullscreen mode Exit fullscreen mode
np.random.randn(5,5)
Enter fullscreen mode Exit fullscreen mode

randint

Return random integers from low (inclusive) to high (exclusive)

np.random.randint(1,100)
Enter fullscreen mode Exit fullscreen mode
np.random.randint(1,100,10)
Enter fullscreen mode Exit fullscreen mode

Array Attributes and Methods

Let's see some useful attributes and methods of an array:

arr = np.arange(25)
ranarr = np.random.randint(0,50,10)
Enter fullscreen mode Exit fullscreen mode
arr
Enter fullscreen mode Exit fullscreen mode
ranarr
Enter fullscreen mode Exit fullscreen mode

Reshape

Returns an array containing the same data with a new shape.

arr.reshape(5,5)
Enter fullscreen mode Exit fullscreen mode

*max, min, argmax, argmin *
In the below, these are useful methods for finding Max or Min value also to find their index locations using argmin or argmax.

ranarr
Enter fullscreen mode Exit fullscreen mode
ranarr.max()
Enter fullscreen mode Exit fullscreen mode
ranarr.argmax()
Enter fullscreen mode Exit fullscreen mode
ranarr.min()
Enter fullscreen mode Exit fullscreen mode
ranarr.argmin()
Enter fullscreen mode Exit fullscreen mode

Shape

Shape is an attribute that Arrays have, not a method.

# Vector
arr.shape
Enter fullscreen mode Exit fullscreen mode
# Notice the two sets of brackets
arr.reshape(1,25)
Enter fullscreen mode Exit fullscreen mode
arr.reshape(1,25).shape
Enter fullscreen mode Exit fullscreen mode
arr.reshape(25,1)
Enter fullscreen mode Exit fullscreen mode
arr.reshape(25,1).shape
Enter fullscreen mode Exit fullscreen mode

dtype

You can also grab the data type of the object in the array.

arr.dtype
Enter fullscreen mode Exit fullscreen mode

;)

💖 💪 🙅 🚩
chowdhurysayebislam
Chowdhury Sayeb Islam

Posted on December 31, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related