How to create strides of length n from a given 1D array in numpy

This recipe helps you create strides of length n from a given 1D array in numpy

Recipe Objective

How to create strides of length n from a given 1D array?

Strides It is nothing but the tuple of integer values, in which the bytes of particular dimension is indicated by each one of in it. To tell how many bytes to jump in the data buffer Numpy uses strides. Stride will indicate the number of bytes to jump in the order for reaching to the next value in the given dimension which also known as axis of travel. It is always constant for given axis.

Step 1 - Import libraries

import numpy as np from numpy.lib.stride_tricks import as_strided

Step 2 - Take Sample data

Sample_data = np.array([12,13,14,15,16,17,18,19], dtype = "int32") print("This is a Sample 1D array:", Sample_data)
This is a Sample 1D array: [12 13 14 15 16 17 18 19]

Step 3 - Create Stride

Result = np.lib.stride_tricks.as_strided(Sample_data,((8-2)//3+1,2),(3*4,4)) print("This is the Following Result","\n",Result, "\n") print("This is the shape of original array which is an 1D array:","\n",Sample_data.shape,"\n") print("This is the shape of our Result which is an 2D array:","\n",Result.shape)
This is the Following Result 
 [[12 13]
 [15 16]
 [18 19]] 

This is the shape of original array which is an 1D array: 
 (8,) 

This is the shape of our Result which is an 2D array: 
 (3, 2)

In the above we have used "np.lib.stride_tricks.as_strided(array, new_array_shape, Stride_steps)" syntax for Stride in which there various functions ar working lets understand them: array - This is nothing but the original array that we want to stride. In our case we have taken 1D array of name "Sample_data". new array shape - This is nothing but the shape of our output array, in our case the resulted array is 2D so the shape will be (3,2) which means 3 rows and 2 columns. Stride steps - It is nothing but the stride that which is measured in bytes. In our case it is (12,4) because we want to jump over 3 indices in the array in which each of them is an integer i.e 4 bytes, So therefore 3*4 = 12 for row stride step and for column is 4 because the next integer is 4 bytes away.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.