True if both variables point to the same object in memory
is not
True if variables point to different objects in memory
These operators donโt compare values, they compare object identity (location in memory).
Why Identity Operators Matter in Machine Learning
In Machine Learning:
We often deal with large datasets (numpy, pandas) โ copying or referencing matters.
We deal with model objects โ checking if two models are the same instance.
Helps prevent unexpected bugs when working with shared memory/data.
Step-by-Step: Identity Operators with Examples
Step 1: is and is not basics
x =[1,2,3]y = xz =[1,2,3]print(x is y)# True (y points to x's object)print(x is z)# False (different object, same value)print(x == z)# True (same value)
Note:
is: checks identity (memory location)
==: checks value
Step 2: Checking id() to understand memory address
Step 3: Practical Example with numpy arrays
copy() creates a new array in memory โ which is very important in data preprocessing, so you donโt modify your original dataset unintentionally.
Step 4: Identity check in ML model objects
When comparing models (e.g., during pipeline debugging), use is to check if theyโre truly the same instance.
Step 5: Example in a Function (Avoiding Side Effects)
In Machine Learning:
Changing training data accidentally happens often.
Use .copy() and is to protect your original datasets.
Summary of Identity Operators
Use Case
Operator
Result
Same memory location?
is
True or False
Different memory locations?
is not
True or False
Check values
==
Don't confuse with is
Video Tutorial
Support Our Work
If you find this post helpful and would like to support my work, you can send a donation via TRC-20 (USDT). Your contributions help us keep creating and sharing more valuable content.
print(id(x)) # Memory address of x
print(id(y)) # Same as x
print(id(z)) # Different from x and y
import numpy as np
a = np.array([1, 2, 3])
b = a # b references a
c = a.copy() # c is a new object
print(a is b) # True
print(a is c) # False
print(np.array_equal(a, c)) # True (same values)
from sklearn.linear_model import LogisticRegression
model1 = LogisticRegression()
model2 = model1
model3 = LogisticRegression()
print(model1 is model2) # True
print(model1 is model3) # False
def modify_array(arr):
if arr is original_array:
print("You are modifying the original data!")
else:
print("You are safe.")
arr[0] = 999
original_array = np.array([1, 2, 3])
modify_array(original_array) # Warning: modifying original
modify_array(original_array.copy()) # Safe