How to drop all missing values from a numpy array?

How to drop all missing values from a numpy array?

How to drop all missing values from a numpy array?

This recipe helps you drop all missing values from a numpy array


Recipe Objective

How to drop all missing values from a numpy array?

Droping the missing values or nan values can be done by using the function "numpy.isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy.logical_not()" where the boolean values will be reversed. At last we want the elements which are having non-nan values which can be further filtered out and store it into another array.

Step 1 - Import library

import numpy as np

Step 2 - Take Sample data

Sample_data = np.array([1,2,7,8,np.nan,9,5,np.nan,1,0]) print("This is Sample data with nan values in it:", Sample_data)
This is Sample data with nan values in it: [ 1.  2.  7.  8. nan  9.  5. nan  1.  0.]

Step 3 - Remove Nan values

remove_nan = Sample_data[np.logical_not(np.isnan(Sample_data))]

Step 4 - Print Results

print("This is the original data with nan values:", Sample_data, "\n") print("This is the data without nan values:", remove_nan)
This is the original data with nan values: [ 1.  2.  7.  8. nan  9.  5. nan  1.  0.] 

This is the data without nan values: [1. 2. 7. 8. 9. 5. 1. 0.]

Relevant Projects

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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

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

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

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.