How to reshape a Numpy array in Python?

How to reshape a Numpy array in Python?

How to reshape a Numpy array in Python?

This recipe helps you reshape a Numpy array in Python


Recipe Objective

Have you ever tried to reshape a numpy array? Creating a new array to reshape it might be a hectic task. So how we can do it very easily in python.

So this is the recipe on how we can Reshape a Numpy Array.

Step 1 - Import the library

import numpy as np

We have only imported numpy which is needed.

Step 2 - Setting up the Vector and Matrix

We have created a 4 x 3 matrix using array and we will reshape it. matrix = np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99], [110, 121, 132]])

Step 3 - Reshaping a matrix

We can reshape the matrix by using reshape function. In the function we have to pass the shape of the final matrix we want. (If we want a matrix of n by m then we have to pass (n,m)). print(matrix.reshape(2, 6)) print(matrix.reshape(3, 4)) print(matrix.reshape(6, 2)) So the output comes as

[[ 11  22  33  44  55  66]
 [ 77  88  99 110 121 132]]

[[ 11  22  33  44]
 [ 55  66  77  88]
 [ 99 110 121 132]]

[[ 11  22]
 [ 33  44]
 [ 55  66]
 [ 77  88]
 [ 99 110]
 [121 132]]

Relevant Projects

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.