How to Flatten a Matrix?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

# How to Flatten a Matrix?

This recipe helps you Flatten a Matrix

0

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

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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

##### Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

##### Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

##### Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

##### Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

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

##### Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

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

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