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

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

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.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

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.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.