0
0
NumPydata~15 mins

np.intersect1d() for intersection in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Find Common Elements Using np.intersect1d()
📖 Scenario: Imagine you have two lists of favorite fruits from two friends. You want to find which fruits both friends like.
🎯 Goal: Build a small program that finds the common fruits between two lists using np.intersect1d().
📋 What You'll Learn
Create two numpy arrays with exact fruit names
Use a variable to store the common fruits
Use np.intersect1d() to find the intersection
Print the common fruits array
💡 Why This Matters
🌍 Real World
Finding common items between lists is useful in surveys, recommendations, and data comparison tasks.
💼 Career
Data scientists often need to compare datasets to find overlaps or shared features.
Progress0 / 4 steps
1
Create two numpy arrays of fruits
Import numpy as np and create two numpy arrays called fruits1 and fruits2 with these exact values: ['apple', 'banana', 'cherry', 'date'] and ['banana', 'date', 'fig', 'grape'] respectively.
NumPy
Need a hint?

Use np.array() to create numpy arrays from lists.

2
Create a variable to store common fruits
Create a variable called common_fruits to hold the common fruits between fruits1 and fruits2. Use np.intersect1d(fruits1, fruits2) to find the intersection.
NumPy
Need a hint?

Use np.intersect1d(array1, array2) to get common elements.

3
Print the common fruits
Write a print() statement to display the common_fruits array.
NumPy
Need a hint?

Use print(common_fruits) to show the result.

4
Final check: Display the common fruits clearly
Print a friendly message followed by the common_fruits array to clearly show the fruits both friends like.
NumPy
Need a hint?

Use print('Fruits both friends like:', common_fruits) to show a clear message.