Problem-solving guide
This page summarizes the recurring operations from the documentation as tasks you should be able to perform from memory.
Core Python and files
- Use variables to store values, then combine them with expressions.
- Use a loop and accumulator when a result must be built step by step.
- Use
with open(...)to write text files safely. - Know which file commands are Colab-specific:
drive.mount,files.download, andfiles.upload.
Images
- Load with Pillow using
Image.open(path), or with OpenCV usingcv2.imread(path). - Inspect
.size,.mode, and.shape. - Crop with array slicing:
image[y1:y2, x1:x2]. - Convert OpenCV BGR images to RGB before displaying with Matplotlib.
Datasets and arrays
- For scikit-learn datasets, use
dataset.datafor features anddataset.targetfor labels. - Use
dataset.target_namesanddataset.feature_namesto interpret the numbers. - Use NumPy slicing:
array[start:stop:step, columns]. - Use
np.unique(y, return_counts=True)to count classes.
Train/test splitting
- Manual slicing is possible, but
train_test_splitis the standard tool. - Always split X and y together so rows and labels remain aligned.
- Use
random_statewhen you want reproducible splits.
Plotting
- Use
plt.plotfor line plots andplt.scatterfor point clouds. - Use labels and legends to distinguish multiple series.
- Use
plt.subplot(rows, columns, position)to build grids of many images.
Clustering
- Clustering is unsupervised: it groups data without true class labels.
- Create
KMeans(n_clusters=...), then usefitandpredict. - Use centroids to understand cluster centers.
- Use elbow and silhouette methods to evaluate cluster choices.
Regression
- Regression predicts continuous numbers.
- Use
LinearRegression().fit(X_train, y_train). - For one feature, reshape X with
reshape(-1, 1). - Interpret
intercept_andcoef_as the regression equation.
Classification
- Classification predicts discrete class labels.
- Train KNN with
KNeighborsClassifier(n_neighbors=...). - Measure performance with accuracy, confusion matrix, precision, recall, F1-score, and
classification_report. - Compare models by using the same train/test split.
Neural networks
- Normalize image inputs before training dense or convolutional neural networks.
- Use
Flattenbefore dense layers when the input is a 2D image. - Use a softmax output layer for multi-class classification.
- Use
history.historyto inspect training and validation metrics.
Convolutional neural networks
- Use
Conv2Dlayers to learn local image patterns andMaxPooling2Dto reduce spatial size. - Use one-hot labels with
categorical_crossentropy. - Use dropout and early stopping to reduce overfitting.
- Resize external images to the model input shape before prediction.
Autoencoders
- Train autoencoders with the input image as both input and target.
- Use the bottleneck layer as the compressed representation.
- For denoising, train on noisy inputs and clean targets.
- Use transposed convolutions when decoding convolutional feature maps back into images.
Transfer learning and model zoos
- Resize and preprocess input images to match the pretrained model.
- Use
include_top=Falsewhen replacing the original classifier head. - Freeze pretrained layers with
trainable = Falsewhen using the base model as a feature extractor. - Model zoos such as TensorFlow Hub, PyTorch, and Ultralytics provide pretrained models that can be adapted instead of trained from scratch.
Exam variations
- If CIFAR-100 appears instead of CIFAR-10, change the final softmax layer and one-hot encoding from 10 classes to 100 classes.
- For CIFAR images, flattened input size is
32 * 32 * 3 = 3072; for MNIST it is28 * 28 = 784. - For denoising autoencoders, train with noisy inputs and clean targets:
autoencoder.fit(x_noisy, x_clean). - After adding noise to normalized images, use
np.clip(x_noisy, 0., 1.). - If the task uses
Input(...)andModel(inputs=..., outputs=...), it is Keras Functional API. The architecture idea is the same asSequential. - Use
stratifybefore one-hot encoding when splitting class labels. - If the task says padding, edge filling, or preserving spatial size in a convolution, use
padding="same".