Home / Past papers / MLP End Term / this paper
Consider the following Python code using Pandas: What does the groupby operation in the code achieve?































Consider a dataset representing various attributes of red wine, loaded into a Pandas DataFrame named data using the following code: Suppose we are interested in extracting the value of the chlorides attribute for the third sample in the dataset (2nd by index). Which of the following expressions correctly achieves this? chlorides is at the 4th index of columns.



















Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
Here’s a structured summary of core topics, concepts, principles, and equations essential for the Diploma Level: Machine Learning Practice (Computer-Based Exam). The exam covers Pandas, scikit-learn, ML algorithms, preprocessing, model evaluation, hyperparameter tuning, and clustering. --- ---
StandardScaler
Example:
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
scaled_data = ss.fit_transform(data)One-Hot Encoding (OneHotEncoder)
LabelEncoder).Example:
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(sparse_output=False)
encoded = ohe.fit_transform(df[['category']])Feature Selection (SelectKBest)
k features based on statistical tests (e.g., chi2, f_classif).Example:
from sklearn.feature_selection import SelectKBest, chi2
skb = SelectKBest(chi2, k=3)
X_selected = skb.fit_transform(X, y)Train-Test Split
Example:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)Linear Regression
coef_) and intercept (intercept_) are learned during training.Ridge Regression (L2 Regularization)
alpha (higher alpha → more regularization).Decision Trees
Hyperparameters:
max_depth: Maximum tree depth.min_samples_split: Minimum samples required to split a node.min_samples_leaf: Minimum samples required in a leaf.Support Vector Machines (SVM)
linear, poly, rbf).Regularization Parameter (C):
C → wider margin (simpler model).C → narrower margin (complex model, risk of overfitting).k-Nearest Neighbors (KNN)
k nearest neighbors.Stochastic Gradient Descent (SGD)
SGDClassifier, SGDRegressor).Variants:
Loss Functions:
hinge (SVM), log_loss (logistic regression), squared_error (regression).Regularization:
l1 (Lasso), l2 (Ridge), elasticnet (combination).Hyperparameters:
alpha: Learning rate.penalty: Regularization type.max_iter: Maximum epochs.Neural Networks (MLPClassifier/MLPRegressor)
Activation Functions:
relu, tanh, logistic.Output layer:
identity.logistic (binary) or softmax (multiclass).Hyperparameters:
hidden_layer_sizes: Number of neurons per layer (e.g., (100, 50)).activation: Activation function for hidden layers.solver: Optimization algorithm (adam, sgd).Regression Metrics
R² (R-squared):
RMSE (Root Mean Squared Error):
MAE (Mean Absolute Error):
Classification Metrics
(TP + TN) / (TP + TN + FP + FN).TP / (TP + FP).TP / (TP + FN).Cross-Validation
k folds; trains on k-1 folds, tests on 1.k = n_samples.Example:
from sklearn.model_selection import cross_val_score, KFold
kf = KFold(n_splits=5)
scores = cross_val_score(model, X, y, cv=kf)Grid Search & Random Search
Example:
from sklearn.model_selection import GridSearchCV
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
grid = GridSearchCV(SVC(), param_grid, cv=5)
grid.fit(X, y)Clustering Methods
Centroid-Based (e.g., K-Means):
Density-Based (e.g., DBSCAN):
Connectivity-Based (e.g., Hierarchical Clustering):
Elbow Method (for K-Means)
k to find optimal k.Example:
inertias = []
for k in range(1, 11):
kmeans = KMeans(n_clusters=k).fit(X)
inertias.append(kmeans.inertia_)
plt.plot(range(1, 11), inertias)Random Forest
Hyperparameters:
n_estimators: Number of trees.max_depth: Depth of each tree.Bagging (Bootstrap Aggregating)
BaggingClassifier with KNeighborsClassifier.Feature Selection vs. Extraction
SelectKBest).GroupBy
mean, sum).Example:
df.groupby('Category')['Price'].mean()Data Access
df['column'], df.column, df.iloc[row, col], df.loc[row_index, 'column'].| Concept | Equation |
|---|---|
| Standard Scaling | |
| R² Score | |
| RMSE | |
| Ridge Regression Loss | |
| Linear Regression | |
| SVM Margin (Hard) | s.t. |
| KNN Distance (Euclidean) | |
| Decision Tree Gini Impurity |
Pandas:
groupby, iloc, loc, and column access methods.Preprocessing:
OneHotEncoder for categorical features (avoid LabelEncoder for non-ordinal data).Model Selection:
max_depth, min_samples_leaf), use regularization (Ridge/Lasso).SGD:
warm_start=True retains weights between .fit() calls.hinge loss → SVM; log_loss → logistic regression.Clustering:
k.Neural Networks:
Output layer activation:
identity.logistic.softmax.Hyperparameter Tuning:
GridSearchCV for small spaces; RandomizedSearchCV for large spaces.