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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.