Numpy Array

Numpy array have functions for matrices ,linear algebra ,Fourier Transform. Numpy arrays provide 50x more speed than a python list.

Features

  • It also has functions for matrices, linear algebra, and Fourier Transform.
  • Python has lists which do the work of an array, but numpy will provide 50x more speed than a Python list.
  • Numpy is written in c,c++ and Python.
  • Where fast computations are required c,c++ is used.
  • It works efficiently with the latest CPU architecture.

Numpy Installation

  • pip install numpy
  • Using this command will ensure the installation of the latest version of numpy that is compatible with your system.
Syntax
pip install numpy

Importing Numpy into your code

Python
Python
Python
import numpy

import numpy as np

Numpy datatypes

Python
Python
Python
import numpy as np
pi = np.float32(3.16)
print(type(pi))
print(pi)
Output
<class 'numpy.float32'>
3.16

Create Ndarray with Numpy

Python
Python
Python
num_arr = np.array(42)

print(type(num_arr))

print(num_arr)

Create 1 Dimensional Array

  • This array has a single dimension.
  • It is a collection of a single unit.
Python
Python
Python
num_arr = np.array([1, 2, 3, 4, 5])
print(num_arr)
print(type(num_arr))
print(num_arr.shape)
print(num_arr.ndim)
Output
[1 2 3 4 5]
<class 'numpy.ndarray'>
(5,)
1

Create 2 Dimensional Array

  • An array that hosts multiple 1 Dimensional arrays is a 2 Dimensional array
  • 2 Dimensional arrays store matrices, tensors and vectors.
Python
Python
Python
num_arr1=np.array([1, 2, 3, 4, 5])

num_arr2=np.array([6, 7, 8, 9, 10])

TwoD = np.array( [ num_arr1 , num_arr2 ] ) 

print(TwoD)

print(TwoD.shape)

print(TwoD.ndim)
Output
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
(2, 5)
2

Create 3 Dimensional Array

  • An array that hosts multiple-dimensional arrays is a 3 3-dimensional array.
  • 3D arrays are used to represent 3rd-order Tensor.
Python
Python
Python
num_arr1=np.array([1, 2, 3])
num_arr2=np.array([4, 5, 6])
num_arr3=np.array([7, 8, 9])
T3D=np.array([
              [num_arr1,num_arr2],
 [num_arr2,num_arr3],
              [num_arr1,num_arr3]
              ]
             ) 
print(T3D)
print(T3D.shape)
print(T3D.ndim)
Output
[[[1 2 3]
  [4 5 6]]

 [[4 5 6]
  [7 8 9]]

 [[1 2 3]
  [7 8 9]]]
(3, 2, 3)
3

Higher Dimensions

  • We can create multiple dimensions in a Numpy Array.
  • We either define multidimensional arrays or we can provide them already formatted.

ndim

Furthermore, we will use ndim attribute to determine the number of dimensions.

Python
Python
Python
np.array([[[[1,2,3], [4,5,6], [7,8,9]], [[10,11,12], [13,14,15], [16,17,18]]], [[[19,20,21], [22,23,24], [25,26,27]], [[28,29,30], [31,32,33], [34,35,36]]]])
print(a.ndim)
a
Output
4
array([[[[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9]],

        [[10, 11, 12],
         [13, 14, 15],
         [16, 17, 18]]],


       [[[19, 20, 21],
         [22, 23, 24],
         [25, 26, 27]],

        [[28, 29, 30],
         [31, 32, 33],
         [34, 35, 36]]]])

Shape

Attribute shape determines the length of each dimension inside the whole array since we know the 4 dimensions are created in the array.

Python
Python
Python
import numpy as np
e = np.array([[[[4, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]])
e.shape
Output
(2, 2, 3, 3)

Concatenate

By using concatenate, we can combine two arrays along a given axis. In the following examples, we will be showcasing two examples where axis value is set to 0 by default, and we have to set any other value manually.

Axis 0

Python
Python
Python
import numpy as np
arr1 = np.array([1.1, 2.1, 3.1])
arr2 = np.array([1.2, 2.2, 3.2])
arr = np.concatenate((arr1, arr2))
arr
Output
array([1.1, 2.1, 3.1, 1.2, 2.2, 3.2])

Axis 1

Python
Python
Python
import numpy as np
arr1 = np.array([[1.1, 2.1, 3.1],[1.2, 2.2, 3.2],[1.3, 2.3, 3.3]])
arr2 = np.array([[1,2,3]])
arr = np.concatenate((arr1, arr2.T),axis=1)
arr
Output
array([[1.1, 2.1, 3.1, 1. ],
       [1.2, 2.2, 3.2, 2. ],
       [1.3, 2.3, 3.3, 3. ]])

Stack

The Numpy stack allows us to join multiple arrays. And we can also use an axis with this method similar to concatenate.

Axis 0

Python
Python
Python
import numpy as np
arr1 = np.array([1, 2, 3,4])
arr2 = np.array([4, 5, 6,7])
arr3 = np.array([7, 8, 9,10])
arr4 = np.array([10, 11, 12,13])
arr4 = np.array([11, 12, 13,14])
arr = np.stack((arr1, arr2 , arr3 , arr4 ),axis=0)
print(arr)
Output
[[ 1  2  3  4]
 [ 4  5  6  7]
 [ 7  8  9 10]
 [11 12 13 14]]

Axis 1

Python
Python
Python
import numpy as np
arr1 = np.array([1, 2, 3,4])
arr2 = np.array([4, 5, 6,7])
arr3 = np.array([7, 8, 9,10])
arr4 = np.array([10, 11, 12,13])
arr4 = np.array([11, 12, 13,14])
arr = np.stack((arr1, arr2 , arr3 , arr4 ),axis=1)
print(arr)
Output
[[ 1  4  7 11]
 [ 2  5  8 12]
 [ 3  6  9 13]
 [ 4  7 10 14]]
Python
Python
Python
import numpy as np
arr1 = np.array([1, 2, 3,4])
arr2 = np.array([4, 5, 6,7])
arr3 = np.array([7, 8, 9,10])
arr4 = np.array([10, 11, 12,13])
arr4 = np.array([11, 12, 13,14])
arr = np.stack((arr1, arr2 , arr3 , arr4 ),axis=-1)
print(arr)
Output
[[ 1  4  7 11]
 [ 2  5  8 12]
 [ 3  6  9 13]
 [ 4  7 10 14]]

Also, take note of axis 1 and axis-1 output

Depth stack

Depth stack combines multiple arrays depth-wise

Python
Python
Python
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.dstack(( arr1 , arr2 ))
print(arr)
Output
[[[1 4]
  [2 5]
  [3 6]]

 [[1 4]
  [2 5]
  [3 6]]

 [[1 4]
  [2 5]
  [3 6]]]

Horizontal stack

Horizontal stack combines arrays in a horizontal direction along columns. This can be similar to concatenate.

Python
Python
Python
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.hstack(( arr1 , arr2 ))
print(arr)

Output
[[1 2 3 4 5 6]
 [1 2 3 4 5 6]
 [1 2 3 4 5 6]]
 
 
 
 
 
 
 

Vertical stack

Horizontal stack combines arrays in a horizontal direction along columns. And it is obviously similar to axis 0.

Python
Python
Python
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.vstack(( arr1 , arr2 ))
print(arr)
Output

[[1 2 3]
 [1 2 3]
 [1 2 3]
 [4 5 6]
 [4 5 6]
 [4 5 6]]
 
 
 
 

Vertical stack

Horizontal stack combines arrays in a horizontal direction along columns. And it is obviously similar to axis 0.

Python
Python
Python
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.vstack(( arr1 , arr2 ))
print(arr)
Output

[[1 2 3]
 [1 2 3]
 [1 2 3]
 [4 5 6]
 [4 5 6]
 [4 5 6]]
 
 
 
 

How useful was this post?

Click on a star to rate it!

  • ANCOVA: Analysis of Covariance with python

    ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.

  • Learn Python The Fun Way

    What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!

  • Meet the most efficient and intelligent AI assistant : NotebookLM

    Start using NotebookLM today and embark on a smarter, more efficient learning journey!

  • Break the ice

    This can be a super guide for you to start and excel in your data science career.

  • Manova Quiz

    Solve this quiz for testing Manova Basics

  • Quiz on Group By

    Test your knowledge on pandas groupby with this quiz

  • Visualization Quiz

    Observe the dataset and try to solve the Visualization quiz on it

  • Versions of ANCOVA (Analysis Of Covariance) with python

    To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…

  • Python Variables

    How useful was this post? Click on a star to rate it! Submit Rating

  • A/B Testing Quiz

    Complete the code by dragging and dropping the correct functions

  • Python Functions

    Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…

  • Python Indexing: A Guide for Data Science Beginners

    Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.

  • Diffusion Models: Making AI Creativity

    Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…

Instagram
WhatsApp
error: Content is protected !!