Python is the most popular language for AI & Machine Learning due to its simplicity, strong libraries, and large community support.
AI refers to systems that can simulate human intelligence, such as:
Examples
ML is a subset of AI where systems learn from data without being explicitly programmed.
Real-World Examples
| Concept | Meaning |
|---|---|
| AI | Broad concept of intelligent machines |
| ML | Learning from data |
| DL | Neural networks with multiple layers |
Scikit-learn is the most widely used ML library in Python.
pip install scikit-learn
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
Supervised learning uses labeled data (input + output).
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
data = load_iris()
X = data.data
y = data.target
model = DecisionTreeClassifier()
model.fit(X, y)
print(model.predict([[5.1, 3.5, 1.4, 0.2]]))
Unsupervised learning uses unlabeled data.
from sklearn.cluster import KMeans
X = [[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0]]
model = KMeans(n_clusters=2)
model.fit(X)
print(model.labels_)
Model training means teaching the model patterns from data.
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model.fit(X_train, y_train)
Evaluating a model checks how well it performs on unseen data.
from sklearn.metrics import accuracy_score
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([[1], [2], [3], [4]])
y = np.array([30000, 35000, 40000, 45000])
model = LinearRegression()
model.fit(X, y)
print(model.predict([[5]]))