Sure π Here are simple Python examples of map(), filter(), zip(), and regression—useful for interviews and practice.
- Get link
- X
- Other Apps
1. map()
Used when you want to apply a function to every element.
numbers = [1, 2, 3, 4, 5] squares = list(map(lambda x: x * x, numbers)) print(squares)
Output:
[1, 4, 9, 16, 25]
2. filter()
Used when you want to select elements that satisfy a condition.
numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers)
Output:
[2, 4, 6]
3. zip()
Used to combine two or more iterables element by element.
names = ["Rahul", "Amit", "Priya"] marks = [85, 90, 95] students = list(zip(names, marks)) print(students)
Output:
[('Rahul', 85), ('Amit', 90), ('Priya', 95)]
You can also create a dictionary:
names = ["Rahul", "Amit", "Priya"] marks = [85, 90, 95] student_marks = dict(zip(names, marks)) print(student_marks)
Output:
{'Rahul': 85, 'Amit': 90, 'Priya': 95}
4. Regression Example in Python
A simple Linear Regression example:
from sklearn.linear_model import LinearRegression # X = hours studied X = [[1], [2], [3], [4], [5]] # y = marks y = [20, 40, 50, 70, 80] model = LinearRegression() # Train the model model.fit(X, y) # Predict marks for 6 hours of study prediction = model.predict([[6]]) print(prediction)
The model learns a relationship like:
Hours Studied → Marks
So after training, if you give:
model.predict([[6]])
it predicts the expected marks for 6 hours of study.
Quick interview difference
| Function | Purpose |
|---|---|
map() | Transform every element |
filter() | Select elements based on condition |
zip() | Combine multiple iterables |
| Regression | Predict a continuous numerical value |
Easy way to remember:
map → change
filter → select
zip → combine
regression → predict
- Get link
- X
- Other Apps
Comments
Post a Comment