Master Python for artificial intelligence development. Learn NumPy, Pandas, Scikit-learn, data structures, algorithms implementation. Comprehensive BTech tutorial 2024.
Why Python for AI?
Python dominates AI development due to extensive libraries, readable syntax, and strong community. It's the industry standard for ML/DL development.
| - Data Processing | NumPy, Pandas |
| - ML Algorithms | Scikit-learn, XGBoost |
| - Deep Learning | TensorFlow, PyTorch |
| - NLP | NLTK, spaCy, Transformers |
| - Visualization | Matplotlib, Plotly |
| - Production | Flask, FastAPI, Docker |
Essential Libraries
NumPy: Numerical Computing
import numpy as np
# Create arrays
arr = np.array([1, 2, 3, 4, 5])
matrix = np.zeros((3, 4))
random_mat = np.random.randn(5, 5)
# Operations
result = arr * 2 # Element-wise
dot_product = np.dot(matrix1, matrix2)
mean_val = np.mean(arr)
std_val = np.std(arr)
# Reshaping
reshaped = arr.reshape(5, 1)
transposed = matrix.T
# Indexing & Slicing
subset = arr[1:3]
slice_2d = matrix[0:2, 1:3]
# Mathematical Functions
exp_vals = np.exp(arr)
log_vals = np.log(arr)
sqrt_vals = np.sqrt(arr)
print("NumPy shape:", matrix.shape)
print("NumPy size:", matrix.size)
print("NumPy dtype:", matrix.dtype)
Pandas: Data Manipulation
import pandas as pd
# Create DataFrames
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Score': [85.5, 90.0, 88.5]
}
df = pd.DataFrame(data)
# Loading data
df = pd.read_csv('data.csv')
df = pd.read_json('data.json')
df = pd.read_excel('data.xlsx')
# Exploration
print(df.head()) # First 5 rows
print(df.info()) # Data types, non-null counts
print(df.describe()) # Statistical summary
# Selection & Filtering
ages = df['Age'] # Single column
subset = df[['Name', 'Age']] # Multiple columns
filtered = df[df['Age'] > 25] # Conditional filter
loc_select = df.loc[0] # By label
iloc_select = df.iloc[0] # By position
# Data Cleaning
df = df.dropna() # Remove missing values
df = df.fillna(df.mean()) # Fill with mean
df = df.drop_duplicates() # Remove duplicates
# Aggregation & Grouping
grouped = df.groupby('Category').mean()
agg_result = df.groupby('Age').agg({'Score': 'mean', 'Name': 'count'})
# Transformation
df['Score_Normalized'] = (df['Score'] - df['Score'].mean()) / df['Score'].std()
df['Age_Group'] = pd.cut(df['Age'], bins=[0, 25, 30, 40], labels=['Young', 'Mid', 'Senior'])
# Time Series
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
resampled = df.resample('D').mean() # Daily average
# Merging
df_merged = pd.merge(df1, df2, on='Key')
df_concat = pd.concat([df1, df2], axis=0)
Scikit-learn: Machine Learning
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_recall_fscore_support, confusion_matrix
# Data preprocessing
X = df.drop('target', axis=1)
y = df['target']
# Scaling features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Encoding categorical variables
encoder = OneHotEncoder(sparse=False)
X_encoded = encoder.fit_transform(X[['Category']])
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Model training - Logistic Regression
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)
# Evaluation
accuracy = accuracy_score(y_test, y_pred)
precision, recall, f1, _ = precision_recall_fscore_support(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
# Cross-validation
cv_scores = cross_val_score(model, X_train, y_train, cv=5)
# Random Forest
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
feature_importance = rf_model.feature_importances_
Data Processing Pipeline
class AIDataPipeline:
def __init__(self):
self.scaler = StandardScaler()
self.encoder = OneHotEncoder()
def preprocess(self, df):
# Handle missing values
df = df.fillna(df.mean())
# Remove outliers (using IQR)
Q1 = df.quantile(0.25)
Q3 = df.quantile(0.75)
IQR = Q3 - Q1
df = df[~((df < (Q1 - 1.5*IQR)) | (df > (Q3 + 1.5*IQR))).any(axis=1)]
# Separate numeric and categorical
numeric_cols = df.select_dtypes(include=['number']).columns
categorical_cols = df.select_dtypes(include=['object']).columns
# Scale numeric
df[numeric_cols] = self.scaler.fit_transform(df[numeric_cols])
# Encode categorical
if len(categorical_cols) > 0:
encoded = self.encoder.fit_transform(df[categorical_cols])
df = pd.concat([df.drop(categorical_cols, axis=1),
pd.DataFrame(encoded, columns=self.encoder.get_feature_names())],
axis=1)
return df
pipeline = AIDataPipeline()
processed_df = pipeline.preprocess(df)
# Vectorization (avoid loops)
# Slow
result = []
for i in range(len(arr)):
result.append(arr[i] * 2)
# Fast (vectorized)
result = arr * 2
# Broadcasting
matrix = np.ones((3, 4))
bias = np.array([1, 2, 3, 4])
result = matrix + bias # Automatically broadcasts
# Memory efficient
# Load in chunks instead of all at once
for chunk in pd.read_csv('large_file.csv', chunksize=10000):
process(chunk)
# Caching results
import functools
@functools.lru_cache(maxsize=128)
def expensive_computation(x):
return x ** 2
# Parallel processing
from joblib import Parallel, delayed
results = Parallel(n_jobs=4)(
delayed(process)(item) for item in items
)
Common Pitfalls
- Modifying while iterating - Use list comprehension instead
- Not checking data types - Verify with df.info()
- Forgetting to drop target from features - Always: X = df.drop('target', axis=1)
- Using wrong random_state - Set for reproducibility
- Not scaling features - Critical for distance-based algorithms
- Data leakage - Scale on train, apply to test
- Ignoring class imbalance - Use stratified split or class weights
Interview Q&A
Q1: Why vectorize in NumPy?
A: Vectorized operations use C/Fortran under the hood, 10-100x faster than loops. Delegates to optimized libraries. Standard practice for numerical computing.
Q2: Difference between NumPy and Pandas?
A: NumPy: numerical arrays, mathematical operations. Pandas: tabular data, labels, missing values handling. NumPy for numerical, Pandas for data manipulation.
Q3: How to handle missing values?
A: Drop (if few), fill with mean/median/mode, interpolate, use advanced algorithms (KNN imputation). Choose based on pattern and importance of data.
Q4: Train-test split importance?
A: Measure real-world performance on unseen data. Prevent overfitting detection. Standard machine learning practice. Use stratified split for imbalanced data.
Q5: Scaling when needed?
A: Distance-based algorithms (KNN, K-means, SVM): always scale. Tree-based: no need (they're scale-invariant). Linear models: recommended. Check algorithm requirements.
Q6: Optimization techniques?
A: Vectorize operations, use appropriate data structures (numpy for numeric, pandas for tabular), caching, parallel processing, profiling bottlenecks.
Quick Revision Notes
- NumPy: Numerical computing, arrays, matrices
- Pandas: Tabular data, DataFrames, time series
- Scikit-learn: ML algorithms, preprocessing, evaluation
- Pipeline: Preprocess → Train → Evaluate
- Vectorization: Always prefer over loops
- Scaling: Important for most ML algorithms
- Train-Test Split: Always separate for validation
- Missing Values: Handle appropriately per domain
- Data Types: Verify and convert when needed
- Performance: Profile and optimize bottlenecks
Summary
Python is essential for AI development. Master NumPy for numerical computing, Pandas for data manipulation, and Scikit-learn for ML algorithms. These skills directly transfer to TensorFlow, PyTorch, and production systems.