How to reshape a Numpy array in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

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

0

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

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

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.

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.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
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 on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

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.