Skip to content

Commit 20495da

Browse files
committed
adjusted readme
1 parent 42c0c74 commit 20495da

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

README.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Support Vector Machines Exercise
22

33
Today we will experiment with a very powerful algorithm, called *Support Vector Machine (SVM)*. SVMs are widely used for classification, but can also solve regression problems, as we will demostrate in our optional task for time series prediction.
4-
### Task 1: Linear Classification with SVMs
4+
### Task 1: Linear Classification with SVMs
55

66
In this exercise, your task is to implement a linear SVM classifier for the Iris dataset, which contains data for different iris species that we want to classify.
77

@@ -19,16 +19,20 @@ To simplify this task, we will again use the popular python library for machine
1919

2020
6. Plot the confusion matrix using `sklearn.metrics.ConfusionMatrixDisplay.from_estimator`. Use `display_labels=iris.target_names` for better visualization.
2121

22-
### Task 2: Classification with soft-margin SVMs
22+
### Task 2: Classification with soft-margin SVMs
2323

2424
In the lecture, you have seen that linear hard-margin SVMs fail to classify the data if it is not linearly separable inside the input space. For this reason, we use different kernels that transform the data into a different space, where it is easier to perform the separation. Additionally, some points may be on the wrong side of the hyperplane due to noise. To handle this, we often use *soft-margin* SVMs. Different from the hard-margin SVM, soft-margin SVM allows for certain data points to be on "the wrong side" of the hyperplane based on their distance to said plane and introduces a hyperparameter $C$ to control the effect of the regularization.
2525

26-
We will now apply this algorithm for face recognition. We will use the [Labeled Faces in the Wild Dataset](http://vis-www.cs.umass.edu/lfw/).
26+
We will now apply this algorithm for face recognition. We will use the *Labeled Faces in the Wild Dataset*.
27+
28+
1. Starting in the `__main__` function of `src/ex2_soft_margin_svm.py` load the dataset from ``sklearn.datasets.fetch_lfw_people``. This can take a while when running for the first time because it has to download the dataset. For this exercise, we only want classes with at least 70 images per person. To improve the runtime, you can also resize the images. Use a resize factor below 0.5.
29+
30+
<span style="color:red">TODO: Check nr of images per person</span>
2731

28-
1. Starting in the `__main__` function of `src/ex2_soft_margin_svm.py` load the dataset from ``sklearn.datasets.fetch_lfw_people``. This can take a while when running for the first time because it has to download the dataset. For this exercise, we only want classes with at least 70 images per person. To improve the runtime, you can also resize the images. Use a resize factor below 0.5.
2932
2. Gather information about the dataset: Print the number of samples, the number of image features (pixels) and the number of classes.
3033

31-
3. Use the provided function `plot_image_matrix` to plot the first 12 images with their corresponding labels as titles.
34+
3. Use the provided function `plot_image_matrix` to plot the first 12 images with their corresponding names according to their labels as titles.
35+
> **Hint:** You get the name for the image by indexing the field *target_names* of the dataset with the label.
3236
3337
4. Split the data 80:20 into training and test data. Use `random_state=42` in the split function.
3438
5. Use the `StandardScaler` from `sklearn.preprocessing` on the train set and scale both the train and the test set.
@@ -39,27 +43,27 @@ A lot of machine learning approaches are configurable. This means that there are
3943

4044
Now we need to find the best values for our hyperparameters. Implement the hyperparameter search in the function `cv_svm` following these steps:
4145

42-
6.1. Define a dictionary of parameters that you want to cross-validate. (Hint: Reasonable values for $C$ range from 0.01 to 1000, while for kernels it is usually sufficient to test `linear`, `rgb` and `poly`.)
46+
6.1. Define a dictionary of parameters that you want to cross-validate. (Hint: Reasonable values for $C$ range from 0.01 ($10^{-2}$) to 1000 ($10^3$) in logarithmic steps, while for kernels it is usually sufficient to test `linear`, `rgb` and `poly`.)
4347

4448
6.2. Initialize your model using the `sklearn.svm.SVC` class. Use the ``sklearn.model_selection.GridSearchCV`` class to find optimal hyperparameters for this task.
4549

4650
7. Print the parameters of the best estimator found with the function `cv_svm`.
4751

4852
8. Calculate and print the accuracy of the best performing model.
4953

50-
9. Plot the output for some images from the test set using the function `plot_image_matrix`. Plot the predictions and the true labels of images as titles.
54+
9. Plot the output for some images from the test set using the function `plot_image_matrix`. Plot the names of the person on the image according to the predictions and the true labels as titles.
5155

5256
Another way to evaluate the performance of a model is the [ROC-Curve](https://en.wikipedia.org/wiki/Receiver_operating_characteristic). It is obtained by plotting the true positive rate (TPR) against the false positive rate (FPR) for different threshold settings.
5357

54-
10. (Optional) Calculate the ROC curve and the area under the curve (AUC) for each class of the test set. Appropriate functions can be found inside ``sklearn.metrics``. Plot the results.
55-
(Hint: You can obtain the scores of each prediction by calling the ``decision_function`` of your classifier.)
58+
10. ✪ (Optional) Calculate the ROC curve and the area under the curve (AUC) for each class of the test set. Appropriate functions can be found inside ``sklearn.metrics``. Plot the RoC Curves for each class in one plot and print the corresponding AUC's.
59+
> **Hint:** You can obtain the scores of each prediction by calling the ``decision_function`` of your classifier.
60+
> **Hint:** You can put the AUC-value in the label for the legend of the plot.
5661
57-
### Task 3 (Optional): Time-series prediction with SVM
62+
### Task 3 (Optional): Time-series prediction with SVM
5863

5964
You can also use SVMs for regression. In this exercise, we will take a brief look at time-series predictions. The goal is to infer new values from a set of old observations. For this we will look at the number of Covid-19 cases.
6065

61-
0. Open `src/ex3_time_series.py`, move to the `__main__` function and have a look at the code. Inspect the dataset closely and make sure you understand what information the columns depict. To do this, you can use the `head()` function of the pandas dataframe.
62-
66+
0. Open `src/ex3_time_series.py`, move to the `__main__` function and have a look at the code. Inspect the dataset closely and make sure you understand what information the columns depict.
6367
1. In the code we generate two arrays: `raw_data` and `raw_data_short`. Plot both curves with the `plot_curve` function. Do you notice any change in behavior in these curves? Is there a point were the rate of change increases? The data that lies before this point won't be considered anymore.
6468

6569
2. With the number of covid cases for the last week (7 days), we want to predict the expected number of cases for the next 5 days. Set the number of days you want to forecast and the number of days that will be taken into account for the forecast.
@@ -83,11 +87,16 @@ Now we need to train an SVM regressor and find the best values for the hyperpara
8387
* $epsilon$ in the epsilon-SVR model. It specifies the the epsilon-tube, within which no penalty is associated in the training loss function with points predicted within a distance epsilon from the actual value,
8488
* and the $gamma$ parameter of the `rbf` kernel.
8589

86-
5. Implement the hyperparameter search in the function `cv_svr` following these steps:
87-
1. Define a dictionary of parameters that you want to cross-validate. (Hint: Reasonable values for $epsilon$ range from 0.1 to 0.001 and for $gamma$ you can try the values `auto` and `scale`.)
88-
2. Initialize your model using the `sklearn.svm.SVR` class. Use the grid search to find optimal hyperparameters.
90+
Implement the hyperparameter search in the function `cv_svr` following these steps:
91+
92+
5.1. Define a dictionary of parameters that you want to cross-validate. (Hint: Reasonable values for $epsilon$ range from 0.1 to 0.001 and for $gamma$ you can try the values `auto` and `scale`.)
93+
94+
5.2. Initialize your model using the `sklearn.svm.SVR` class. Use the grid search to find optimal hyperparameters.
95+
96+
6 . Print the parameters of the best estimator found with the function `cv_svr`.
97+
98+
7 . After that go to the ``recursive_forecast()`` function where the new predictions are recursivley used to generate predictions even further in the future. Implement the TODOs.
99+
100+
8 . Use the function `recursive_forecast` to make predictions for the next 5 days. Don't forget to denormalize your predictions. Use `numpy.round` to round the predictions after denormalization.
89101

90-
6. Print the parameters of the best estimator found with the function `cv_svr`.
91-
7. After that go to the ``recursive_forecast()`` function where the new predictions are recursivley used to generate predictions even further in the future. Implement the TODOs.
92-
8. Use the function `recursive_forecast` to make predictions for the next 5 days. Don't forget to denormalize your predictions. Use `numpy.round` to round the predictions after denormalization.
93-
9. Plot the predicted results with `plot_prediction`.
102+
9 . Plot the predicted results with `plot_prediction` for all available data.

0 commit comments

Comments
 (0)