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.

  • Tourism Trend Prediction

    After tourism was established as a motivator of local economies (country, state), many governments stepped up to the plate.

  • Sentiment Analysis Polarity Detection using pos tag

    Sentiment analysis can determine the polarity of sentiments from given sentences. We can classify them into certain categories.

  • For loop with Dictionary

    Traverse a dictionary with for loop Accessing keys and values in dictionary. Use Dict.values() and Dict.keys() to generate keys and values as iterable. Nested Dictionaries with for loop Access Nested values of Nested Dictionaries How useful was this post? Click on a star to rate it! Submit Rating

  • For Loops with python

    For loop is one of the most useful methods to reuse a code for repetitive execution.

  • Metrics and terminologies of digital analytics

    These all metrics are revolving around visits and hits which we are getting on websites. Single page visits, Bounce, Cart Additions, Bounce Rate, Exit rate,

  • Hypothesis Testing

    Hypothesis testing is a statistical method for determining whether or not a given hypothesis is true. A hypothesis can be any assumption based on data.

  • A/B testing

    A/B tests are randomly controlled experiments. In A/B testing, you get user response on various versions of the product, and users are split within multiple versions of the product to figure out the “winner” of the version.

  • For Loop With Tuples

    This article covers ‘for’ loops and how they are used with tuples. Even if the tuples are immutable, the accessibility of the tuples is similar to that of the list.

  • Multivariate ANOVA (MANOVA) with python

    MANOVA is an update of ANOVA, where we use a minimum of two dependent variables.

  • Two-Way ANOVA

    You only need to understand two or three concepts if you have read the one-way ANOVA article. We use two factors instead of one in a two-way ANOVA.

Instagram
WhatsApp
error: Content is protected !!