In [1]:
import numpy as np
import pandas as pd
from sklearn.impute import SimpleImputer
# Create DataFrame with missing values
data = {"Feature 1": [1, 2, 3, 4], "Feature 2": [2, 3, np.nan, 5]}
df = pd.DataFrame(data)
# Apply Mean Imputation only on Feature 2
imputer = SimpleImputer(strategy="mean")
df["Feature 2"] = imputer.fit_transform(df[["Feature 2"]])
# Print before and after imputation
print("Original DataFrame:")
print(data)
print("\nDataFrame after Mean Imputation:")
print(df)
Original DataFrame:
{'Feature 1': [1, 2, 3, 4], 'Feature 2': [2, 3, nan, 5]}
DataFrame after Mean Imputation:
Feature 1 Feature 2
0 1 2.000000
1 2 3.000000
2 3 3.333333
3 4 5.000000
In [3]:
import numpy as np
import pandas as pd
from sklearn.impute import KNNImputer
# Create DataFrame with missing values
data = {"Feature 1": [1, 2, 3, 4], "Feature 2": [2, 3, np.nan, 5]}
df = pd.DataFrame(data)
# Apply KNN Imputer
imputer = KNNImputer(n_neighbors=2) # Using 2 nearest neighbors for imputation
df_imputed = pd.DataFrame(imputer.fit_transform(df), columns=df.columns)
# Print before and after imputation
print("Original DataFrame:")
print(df)
print("\nDataFrame after KNN Imputation:")
print(df_imputed)
Original DataFrame: Feature 1 Feature 2 0 1 2.0 1 2 3.0 2 3 NaN 3 4 5.0 DataFrame after KNN Imputation: Feature 1 Feature 2 0 1.0 2.0 1 2.0 3.0 2 3.0 4.0 3 4.0 5.0
In [5]:
import numpy as np
import pandas as pd
from sklearn.experimental import enable_iterative_imputer # Enable MICE
from sklearn.impute import IterativeImputer
from sklearn.linear_model import LinearRegression
# Create DataFrame with missing Age values
data = {
"Age": [25, np.nan, 30, 35, np.nan, 40, 45, np.nan, 50, 55],
"Income": [3000, 3500, 4000, 4500, 5000, 6000, 7000, 7500, 8000, 8500],
"Education Level": [12, 14, 16, 12, 18, 16, 14, 12, 16, 18] # Years of education
}
df = pd.DataFrame(data)
# Apply MICE (Multiple Imputation by Chained Equations)
imputer = IterativeImputer(estimator=LinearRegression(), max_iter=10, random_state=42)
df_imputed = pd.DataFrame(imputer.fit_transform(df), columns=df.columns)
# Print before and after imputation
print("Original DataFrame with Missing Values:")
print(df)
print("\nDataFrame after MICE Imputation:")
print(df_imputed)
Original DataFrame with Missing Values:
Age Income Education Level
0 25.0 3000 12
1 NaN 3500 14
2 30.0 4000 16
3 35.0 4500 12
4 NaN 5000 18
5 40.0 6000 16
6 45.0 7000 14
7 NaN 7500 12
8 50.0 8000 16
9 55.0 8500 18
DataFrame after MICE Imputation:
Age Income Education Level
0 25.000000 3000.0 12.0
1 27.828162 3500.0 14.0
2 30.000000 4000.0 16.0
3 35.000000 4500.0 12.0
4 35.202864 5000.0 18.0
5 40.000000 6000.0 16.0
6 45.000000 7000.0 14.0
7 48.854415 7500.0 12.0
8 50.000000 8000.0 16.0
9 55.000000 8500.0 18.0
In [6]:
import pandas as pd
import numpy as np
# Create DataFrame
data = {
"Student": ["A", "B", "B", "C", "D"],
"Math": [90, 95, 95, 80, 85],
"Science": [85, 75, 75, np.nan, 90],
"English": [np.nan, 88, 88, 93, 87]
}
df = pd.DataFrame(data)
# Find duplicated rows
duplicates = df.duplicated()
# Display the original DataFrame with a column indicating duplicates
df["Is_Duplicate"] = duplicates
print("Original DataFrame with Duplicates Marked:")
print(df)
Original DataFrame with Duplicates Marked: Student Math Science English Is_Duplicate 0 A 90 85.0 NaN False 1 B 95 75.0 88.0 False 2 B 95 75.0 88.0 True 3 C 80 NaN 93.0 False 4 D 85 90.0 87.0 False
In [7]:
df_cleaned = df.drop_duplicates()
In [9]:
print(df_cleaned)
Student Math Science English Is_Duplicate 0 A 90 85.0 NaN False 1 B 95 75.0 88.0 False 2 B 95 75.0 88.0 True 3 C 80 NaN 93.0 False 4 D 85 90.0 87.0 False
In [10]:
import pandas as pd
# Create DataFrame
data = {"Color": ["Red", "Blue", "Green", "Blue"]}
df = pd.DataFrame(data)
# Apply One-Hot Encoding
df_encoded = pd.get_dummies(df, columns=["Color"])
# Display the result
print("Original DataFrame:")
print(df)
print("\nOne-Hot Encoded DataFrame:")
print(df_encoded)
Original DataFrame: Color 0 Red 1 Blue 2 Green 3 Blue One-Hot Encoded DataFrame: Color_Blue Color_Green Color_Red 0 False False True 1 True False False 2 False True False 3 True False False
In [11]:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# Create DataFrame
data = {"Color": ["Red", "Blue", "Green", "Blue"]}
df = pd.DataFrame(data)
# Apply Label Encoding
encoder = LabelEncoder()
df["Color_Encoded"] = encoder.fit_transform(df["Color"])
# Display the result
print(df)
Color Color_Encoded 0 Red 2 1 Blue 0 2 Green 1 3 Blue 0
In [13]:
import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
# Generate sample data (positive values only, required for Box-Cox)
np.random.seed(42)
data = np.random.exponential(scale=2, size=1000) # Positively skewed data
# Convert to a Pandas DataFrame
df = pd.DataFrame(data, columns=["Original"])
# Apply Box-Cox transformation
df["Transformed"], lambda_value = stats.boxcox(df["Original"])
# Plot before and after transformation
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].hist(df["Original"], bins=30, color="skyblue", edgecolor="black")
ax[0].set_title("Original Data (Skewed)")
ax[1].hist(df["Transformed"], bins=30, color="lightcoral", edgecolor="black")
ax[1].set_title(f"Box-Cox Transformed Data ( = {lambda_value:.2f})")
plt.show()
/usr/local/lib/python3.13/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 65535 (\uffff) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw)
In [14]:
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
# Generate sample data
np.random.seed(42)
data = np.random.randint(10, 100, size=(10, 1))
# Random values between 10 and 100
# Convert to DataFrame
df = pd.DataFrame(data, columns=["Original"])
# Apply Min-Max Scaling
scaler = MinMaxScaler() # Scale values between 0 and 1
df["Scaled"] = scaler.fit_transform(df[["Original"]])
# Plot before and after scaling
fig, ax = plt.subplots(1, 2, figsize=(10, 4))
ax[0].hist(df["Original"], bins=5, color="skyblue", edgecolor="black")
ax[0].set_title("Original Data")
ax[1].hist(df["Scaled"], bins=5, color="lightcoral", edgecolor="black")
ax[1].set_title("Min-Max Scaled Data")
plt.show()
In [15]:
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
# Generate sample data (real numbers)
np.random.seed(42)
data = np.random.uniform(50.0, 150.0, size=(10, 1))
# Random real numbers between 50.0 and 150.0
# Convert to DataFrame
df = pd.DataFrame(data, columns=["Original"])
# Apply Z-score Standardization
scaler = StandardScaler()
df["Standardized"] = scaler.fit_transform(df[["Original"]])
# Print original and standardized values
print(df)
Original Standardized 0 87.454012 -0.485878 1 145.071431 1.436904 2 123.199394 0.707000 3 109.865848 0.262039 4 65.601864 -1.215119 5 65.599452 -1.215199 6 55.808361 -1.541943 7 136.617615 1.154787 8 110.111501 0.270237 9 120.807258 0.627171
In [16]:
# Plot before and after standardization
fig, ax = plt.subplots(1, 2, figsize=(10, 4))
ax[0].hist(df["Original"], bins=5, color="skyblue", edgecolor="black")
ax[0].set_title("Original Data")
ax[1].hist(df["Standardized"], bins=5, color="lightcoral", edgecolor="black")
ax[1].set_title("Z-Score Standardized Data")
plt.show()
In [17]:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import SequentialFeatureSelector
from sklearn.metrics import mean_squared_error
# Generate Synthetic Data
np.random.seed(42)
X = np.random.rand(100, 5) # 100 samples, 5 features
y = 3 * X[:, 0] + 2 * X[:, 1] - 1.5 * X[:, 2] + np.random.randn(100)
# Target variable with noise
df = pd.DataFrame(X, columns=[f'Feature_{i}' for i in range(1, 6)])
df['Target'] = y
# Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(df.drop(columns=["Target"]),\
df["Target"], test_size=0.2, random_state=42)
# Initialize Model
model = LinearRegression()
# Forward Feature Selection (Adding Features Step by Step)
forward_selector = SequentialFeatureSelector(model, n_features_to_select="auto", \
direction="forward", cv=5)
forward_selector.fit(X_train, y_train)
selected_forward = X_train.columns[forward_selector.get_support()]
print(f"Selected Features (Forward): {list(selected_forward)}")
# Backward Feature Selection (Removing Features Step by Step)
backward_selector = SequentialFeatureSelector(model, n_features_to_select="auto", \
direction="backward", cv=5)
backward_selector.fit(X_train, y_train)
selected_backward = X_train.columns[backward_selector.get_support()]
print(f"Selected Features (Backward): {list(selected_backward)}")
# Train and Evaluate Models
X_train_forward, X_test_forward = X_train[selected_forward], \
X_test[selected_forward]
X_train_backward, X_test_backward = X_train[selected_backward], \
X_test[selected_backward]
# Train forward selection model
model.fit(X_train_forward, y_train)
y_pred_forward = model.predict(X_test_forward)
mse_forward = mean_squared_error(y_test, y_pred_forward)
# Train backward selection model
model.fit(X_train_backward, y_train)
y_pred_backward = model.predict(X_test_backward)
mse_backward = mean_squared_error(y_test, y_pred_backward)
print(f"Forward Selection MSE: {mse_forward:.4f}")
print(f"Backward Selection MSE: {mse_backward:.4f}")
Selected Features (Forward): ['Feature_1', 'Feature_2'] Selected Features (Backward): ['Feature_1', 'Feature_2', 'Feature_3'] Forward Selection MSE: 1.1239 Backward Selection MSE: 0.9299
In [19]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
# Generate Synthetic Data
np.random.seed(42)
X = np.linspace(-3, 3, 50).reshape(-1, 1)
y = X**3 - 3*X + np.random.randn(50, 1) * 3
# Split Data into Train & Test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train and Plot Models with Different Degrees
plt.figure(figsize=(12, 5))
degrees = [1, 3, 10] # Linear, Cubic, Overfitting
titles = ["Underfitting (Linear)", "Good Fit (Cubic)", "Overfitting (Degree=10)"]
for i, degree in enumerate(degrees):
poly = PolynomialFeatures(degree=degree)
X_train_poly = poly.fit_transform(X_train)
X_test_poly = poly.transform(X_test)
model = LinearRegression().fit(X_train_poly, y_train)
y_pred = model.predict(X_test_poly)
mse = mean_squared_error(y_test, y_pred)
# Plot Results
plt.subplot(1, 3, i+1)
plt.scatter(X_train, y_train, color="blue", label="Train Data")
plt.scatter(X_test, y_test, color="red", label="Test Data")
X_range = np.linspace(-3, 3, 100).reshape(-1, 1)
plt.plot(X_range, model.predict(poly.transform(X_range)), color="black", linewidth=2)
plt.title(f"{titles[i]}\nMSE: {mse:.2f}")
plt.legend()
plt.tight_layout()
plt.show()
In [21]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso, Ridge
from sklearn.metrics import mean_squared_error
# Generate Synthetic Data
np.random.seed(42)
X = 2 * np.random.rand(100, 1) # 100 samples, 1 feature
y = 4 + 3 * X[:, 0] + np.random.randn(100) # y = 4 + 3X + noise
# Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train Lasso Regression Model
lasso_model = Lasso(alpha=0.1)
lasso_model.fit(X_train, y_train)
# Train Ridge Regression Model
ridge_model = Ridge(alpha=0.1)
ridge_model.fit(X_train, y_train)
# Make Predictions
y_pred_lasso = lasso_model.predict(X_test)
y_pred_ridge = ridge_model.predict(X_test)
# Evaluate Models (MSE)
lasso_mse = mean_squared_error(y_test, y_pred_lasso)
ridge_mse = mean_squared_error(y_test, y_pred_ridge)
print(f"Lasso Regression MSE: {lasso_mse:.4f}")
print(f"Ridge Regression MSE: {ridge_mse:.4f}")
# Visualizing the Results
plt.scatter(X_test, y_test, color="blue", label="Actual Data")
plt.plot(X_test, y_pred_lasso, color="red", label="Lasso Regression")
plt.plot(X_test, y_pred_ridge, color="green", label="Ridge Regression")
plt.legend()
plt.xlabel("Feature X")
plt.ylabel("Target y")
plt.title("Lasso vs Ridge Regression")
plt.show()
Lasso Regression MSE: 0.6584 Ridge Regression MSE: 0.6527
In [23]:
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score, recall_score, f1_score, roc_auc_score, roc_curve
import matplotlib.pyplot as plt
# Generate Synthetic Classification Data
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
# Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train Logistic Regression Model
model = LogisticRegression()
model.fit(X_train, y_train)
# Make Predictions
y_pred = model.predict(X_test)
y_pred_prob = model.predict_proba(X_test)[:, 1] # Probabilities for AUC
# Calculate Precision, Recall, F-score, AUC
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f_score = f1_score(y_test, y_pred)
auc = roc_auc_score(y_test, y_pred_prob)
# Calculate Sensitivity and Specificity
tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
sensitivity = tp / (tp + fn) # Also called True Positive Rate or Recall
specificity = tn / (tn + fp) # True Negative Rate
# Print the results
print(f"Precision: {precision:.2f}")
print(f"Recall (Sensitivity): {recall:.2f}")
print(f"F-score: {f_score:.2f}")
print(f"AUC: {auc:.2f}")
print(f"Sensitivity: {sensitivity:.2f}")
print(f"Specificity: {specificity:.2f}")
# Plot ROC Curve
fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob)
plt.figure(figsize=(6, 6))
plt.plot(fpr, tpr, color='blue', label=f'AUC = {auc:.2f}')
plt.plot([0, 1], [0, 1], color='gray', linestyle='--') # Diagonal line (random classifier)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend()
plt.show()
Precision: 0.91 Recall (Sensitivity): 0.80 F-score: 0.86 AUC: 0.92 Sensitivity: 0.80 Specificity: 0.91
In [24]:
import numpy as np
import pandas as pd
from sklearn.datasets import make_regression
from sklearn.model_selection import KFold, cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Generate a Synthetic Regression Dataset
X, y = make_regression(n_samples=1000, n_features=20, noise=10, random_state=42)
# Set Up K-Fold Cross-Validation
k = 5 # Number of folds
kf = KFold(n_splits=k, shuffle=True, random_state=42)
# Initialize Model
model = LinearRegression()
# Perform Cross-Validation and Get Scores (Using Negative MSE)
mse_scores = cross_val_score(model, X, y, cv=kf, scoring='neg_mean_squared_error')
# Convert Negative MSE to Positive Values
mse_scores = -mse_scores # Since cross_val_score returns negative MSE
# Print Results
print(f"MSE for each fold: {mse_scores}")
print(f"Mean MSE: {np.mean(mse_scores):.4f}")
MSE for each fold: [114.27751339 94.62469548 105.45154013 114.41539632 106.57179726] Mean MSE: 107.0682
In [ ]: