How to Flatten a Matrix?

How to Flatten a Matrix?

How to Flatten a Matrix?

This recipe helps you Flatten a Matrix


Recipe Objective

Have you ever tried to flatten a Matrix i.e. converting and m x n matrix in a array of values.

So this is the recipe on how we can Flatten a Matrix.

Step 1 - Import the library

import numpy as np

We have only imported numpy which is needed.

Step 2 - Setting up the Data

We have created a matrix using array and we will flatten this. matrixA = np.array([[1, 2, 3, 97], [4, 5, 6, 98], [7, 8, 9, 99], [10, 11, 12, 100]]) matrixB = np.array([[2, 3, 4], [5, 6, 9], [7, 8, 1]])

Step 3 - Calculating inverse of matrix

We can flatten the matrix by using flatten function with no parameters. print(matrixA.flatten()) print(matrixB.flatten()) So the output comes as

[  1   2   3  97   4   5   6  98   7   8   9  99  10  11  12 100]

[2 3 4 5 6 9 7 8 1]

Relevant Projects

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 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.

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

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

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.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.