🟣 ML + AI  ·  Lesson 60

Cross-Validation और Hyperparameter Tuning

Cross Validation और Tuning क्या है?

Cross Validation और Tuning ka matlab hai: Cross validation tests a model on multiple splits, and hyperparameter tuning finds better model settings. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki testing model on multiple splits jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye testing model on multiple splits me kaam aata hai.
  • Ye finding better settings se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
k-foldCross-validation method that tests model on multiple splits.
GridSearchCVTool that tries many hyperparameter combinations.
hyperparameterSetting chosen before training, such as tree depth or k value.
validationChecking whether data follows the required format.
overfittingModel learns training data too closely and performs poorly on new data.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
X, y = load_iris(return_X_y=True)
model = DecisionTreeClassifier(random_state=0)
scores = cross_val_score(model, X, y, cv=5)
print("Scores:", scores)
print("Average:", scores.mean())

Complete Example Program

Python – cross-validation-tuning.py
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier

X, y = load_iris(return_X_y=True)
model = DecisionTreeClassifier(random_state=0)
scores = cross_val_score(model, X, y, cv=5)
print("Scores:", scores)
print("Average:", scores.mean())

Expected Output

Cross-validation scores and average accuracy will be displayed.

Program Explanation

  • from sklearn.datasets import load_iris imports ready-made features from a module/library.
  • from sklearn.model_selection import cross_val_score imports ready-made features from a module/library.
  • from sklearn.tree import DecisionTreeClassifier imports ready-made features from a module/library.
  • X, y = load_iris(return_X_y=True) stores a value in X, y.
  • model = DecisionTreeClassifier(random_state=0) stores a value in model.
  • scores = cross_val_score(model, X, y, cv=5) stores a value in scores.
  • print("Scores:", scores) displays information or calculated result on the screen.

Practical Uses

  • Testing model on multiple splits.
  • Finding better settings.
  • Reducing random result dependence.

Common Mistakes

  • Training and testing the model on the same data.
  • Using an algorithm without understanding the input features.
  • Reporting only accuracy without checking actual mistakes and limitations.

Practice Tasks

  1. Program ko cross-validation-tuning.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. testing model on multiple splits par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

Cross Validation and Hyperparameter Tuning ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.