How to find common values between two arrays?

How to find common values between two arrays?

How to find common values between two arrays?

This recipe helps you find common values between two arrays


Recipe Objective

Many times we are dealing with mulitple arrays. Sometimes there is need of finding the intersection between two for working on common elements further.

So this recipe is a short example on how to find common values between two arrays. Let's get started.

Step 1 - Import the library

import numpy as np

Let's pause and look at these imports. Numpy is generally used for working with arrays and performing mathematical operations in domain of linear algebra, fourier transform and matrices.

Step 2 - Setup the Data

x = np.array([0, 1, 2, 3, 4]) y = np.array([0, 2, 4])

We have simply setup two random arrays.

Step 3 - Finding intersection and printing

print(np.intersect1d(x, y))

intersect1d basically helps in finding common elements of 1-D array.

Step 4 - Lets look at our dataset now

Once we run the above code snippet, we will see:

[0 2 4]

Relevant Projects

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.

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

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.

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.

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

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.

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.

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.

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.