<!-- Example --> <p>Machine Learning helps email systems to filter spam messages based on data patterns.</p>
<!-- Example --> <ul> <li>Supervised Learning</li> <li>Unsupervised Learning</li> <li>Semi-supervised Learning</li> <li>Reinforcement Learning</li> </ul>
<!-- Example --> <p>Training a model to predict house prices using past housing data with known prices.</p>
<!-- Example --> <p>Clustering customers based on purchasing behavior without knowing their buying intent.</p>
<!-- Example --> <p>Teaching a robot to walk by rewarding successful steps and penalizing falls.</p>
<!-- Example --> <p>AI includes ML, natural language processing, robotics, etc. ML specifically deals with data-driven learning.</p>
<!-- Example --> <p>A model predicting stock prices perfectly on past data but failing on new unseen data.</p>
<!-- Example --> <p>Using a linear model for a complex dataset with curves and interactions.</p>
<!-- Example --> <p>70% of the data used for training a spam detection algorithm.</p>
<!-- Example --> <p>Using 30% of the total dataset for testing after training the model.</p>
<!-- Example --> <p>K-fold cross-validation divides the data into K parts and rotates training/testing on each fold.</p>
<!-- Example --> <p>In a dataset about houses, features include square footage, number of rooms, and location.</p>
<!-- Example --> <p>For a spam classifier, labels are "spam" or "not spam" tags for each email.</p>
<!-- Example --> <p>A decision tree model classifies whether a loan should be approved based on applicant details.</p>
<!-- Example --> <p>Predicting house price based on size and number of rooms using a best-fit line.</p>
from sklearn.neighbors import KNeighborsClassifier<br> from sklearn.datasets import load_iris<br> from sklearn.model_selection import train_test_split<br> # Load dataset<br> iris = load_iris()<br> X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)<br> # Create k-NN classifier with k=3<br> knn = KNeighborsClassifier(n_neighbors=3)<br> knn.fit(X_train, y_train)<br> # Predict on test data<br> predictions = knn.predict(X_test)<br> print(predictions)<br>This code trains a k-NN model on Iris dataset and prints predictions for the test set.
from sklearn.preprocessing import MinMaxScaler<br> import numpy as np<br> data = np.array([[10, 200], [15, 300], [20, 400]], dtype=float)<br> scaler = MinMaxScaler()<br> normalized_data = scaler.fit_transform(data)<br> print(normalized_data)<br>This code normalizes the 2D array column-wise between 0 and 1.
# Example: Deploying a model using Flask
from flask import Flask, request, jsonify
import joblib
app = Flask(__name__)
model = joblib.load('model.pkl')
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
prediction = model.predict([data['features']])
return jsonify({'prediction': prediction.tolist()})
app.run(debug=True)
# Example: Using L2 Regularization in Logistic Regression
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(penalty='l2', C=0.1)
model.fit(X_train, y_train)
# Example: Grid Search for tuning SVM parameters
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
grid = GridSearchCV(SVC(), parameters)
grid.fit(X_train, y_train)
print(grid.best_params_)
# Example: Using StandardScaler
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Example: Using SMOTE for oversampling
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=42)
X_res, y_res = sm.fit_resample(X, y)
# Example: PCA in sklearn
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
# Example: Comparing model performance before and after dimensionality reduction
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
model = RandomForestClassifier()
model.fit(X_train, y_train)
original_score = accuracy_score(y_test, model.predict(X_test))
pca = PCA(n_components=5)
X_pca = pca.fit_transform(X)
X_train_pca, X_test_pca, y_train, y_test = train_test_split(X_pca, y, test_size=0.3)
model.fit(X_train_pca, y_train)
pca_score = accuracy_score(y_test, model.predict(X_test_pca))
print("Original Score:", original_score)
print("PCA Score:", pca_score)
# Example: Using Random Forest (Bagging technique)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# Example: Using StackingClassifier
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
base_learners = [ ('dt', DecisionTreeClassifier()), ('svc', SVC(probability=True)) ]
stack_model = StackingClassifier(estimators=base_learners, final_estimator=LogisticRegression())
stack_model.fit(X_train, y_train)
stack_preds = stack_model.predict(X_test)
# Example: AdaBoost with decision trees
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
model = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=1), n_estimators=50)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# Example: Stacking with two base models and a meta-model
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
estimators = [('svm', SVC()), ('tree', DecisionTreeClassifier())]
model = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# Example: Training with different batch sizes
model.fit(X_train, y_train, epochs=10, batch_size=32)
model.fit(X_train, y_train, epochs=10, batch_size=64)
# Example: Overfitting visible in training vs. validation accuracy
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=100)
# Example: Applying dropout
from tensorflow.keras.layers import Dropout
model.add(Dropout(0.5))
# Example: Adding dropout to a layer
model.add(Dropout(0.25))
# Example: Using early stopping in Keras
from tensorflow.keras.callbacks import EarlyStopping
callback = EarlyStopping(monitor='val_loss', patience=3)
model.fit(X_train, y_train, validation_data=(X_val, y_val), callbacks=[callback])
# Example: Applying L1 regularization
from tensorflow.keras import regularizers
Dense(64, kernel_regularizer=regularizers.l1(0.01))
# Example: Applying L2 regularization
Dense(64, kernel_regularizer=regularizers.l2(0.01))
# Example: Simple CNN with Keras
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
# Example: MaxPooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))
# Example: Using KNN in scikit-learn
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# Example: Ridge regression
from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# Example: Plotting ROC curve
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
fpr, tpr, thresholds = roc_curve(y_test, model.predict_proba(X_test)[:,1])
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, label='AUC = %0.2f' % roc_auc)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend()
plt.show()
# Example: Using VIF to detect multicollinearity
from statsmodels.stats.outliers_influence import variance_inflation_factor
import pandas as pd
vif_data = pd.DataFrame()
vif_data["feature"] = X.columns
vif_data["VIF"] = [variance_inflation_factor(X.values, i) for i in range(len(X.columns))]
print(vif_data)
# Example: Confusion Matrix with scikit-learn
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, predictions)
print(cm)
# Example: Impute missing values with mean
from sklearn.impute import SimpleImputer
import numpy as np
imp = SimpleImputer(strategy='mean')
X_imputed = imp.fit_transform(X)
# Example: Ridge regression
from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)
# Example: Logistic regression model
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# Example: 5-fold cross-validation
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
print("Average score:", scores.mean())
# Example: Applying PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
# Example: Plot ROC curve
from sklearn.metrics import roc_curve, auc
y_score = model.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)
# Example: Online learning with SGDClassifier
from sklearn.linear_model import SGDClassifier
model = SGDClassifier()
for X_batch, y_batch in data_stream:
model.partial_fit(X_batch, y_batch, classes=np.unique(y))
# Example: Isolation Forest for anomaly detection
from sklearn.ensemble import IsolationForest
model = IsolationForest()
model.fit(X)
predictions = model.predict(X)
# Example: Stratified train-test split
from sklearn.model_selection import StratifiedShuffleSplit
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2)
for train_index, test_index in split.split(X, y):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# Example: Confusion matrix with sklearn
from sklearn.metrics import confusion_matrix
predictions = model.predict(X_test)
cm = confusion_matrix(y_test, predictions)
print(cm)
# Example: Bagging classifier
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
model = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=10)
model.fit(X_train, y_train)
# Example: Set max depth for pre-pruning
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=3)
model.fit(X_train, y_train)
# Example: Grid search for best parameters
from sklearn.model_selection import GridSearchCV
params = {'n_estimators': [50, 100]}
grid = GridSearchCV(estimator=model, param_grid=params, cv=3)
grid.fit(X_train, y_train)
# Example: Linear SVM from sklearn.svm import SVC model = SVC(kernel='linear') model.fit(X_train, y_train)
# Example: Creating interaction feature
X['new_feature'] = X['feature1'] * X['feature2']
# Example: Use regularization in logistic regression
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(C=0.1)
model.fit(X_train, y_train)
# Example: 5-fold cross-validation
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
print(scores.mean())
# Example: SGD in Scikit-learn
from sklearn.linear_model import SGDClassifier
model = SGDClassifier() model.fit(X_train, y_train)
# Example: Plot ROC curve
from sklearn.metrics import roc_curve, auc
y_scores = model.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_scores)
roc_auc = auc(fpr, tpr)
print(roc_auc)
# Example: Bagging with Decision Tree
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
model = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=10)
model.fit(X_train, y_train)
# Example: Gradient Boosting
from sklearn.ensemble import GradientBoostingClassifier
model = GradientBoostingClassifier(n_estimators=100)
model.fit(X_train, y_train)
# Example: SelectKBest feature selection
from sklearn.feature_selection import SelectKBest, f_classif
selector = SelectKBest(score_func=f_classif, k=5)
X_new = selector.fit_transform(X, y)
# Example: PCA from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
# Example: Using SHAP for interpretability
import shap
explainer = shap.Explainer(model, X_train)
shap_values = explainer(X_test)
shap.summary_plot(shap_values, X_test)
# Example: L2 Regularization with Ridge Regression
from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)
# Example: 5-Fold Cross Validation
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
print(scores.mean())