You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-19Lines changed: 28 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Support Vector Machines Exercise
2
2
3
3
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
5
5
6
6
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.
7
7
@@ -19,16 +19,20 @@ To simplify this task, we will again use the popular python library for machine
19
19
20
20
6. Plot the confusion matrix using `sklearn.metrics.ConfusionMatrixDisplay.from_estimator`. Use `display_labels=iris.target_names` for better visualization.
21
21
22
-
### Task 2: Classification with soft-margin SVMs
22
+
### ⊙ Task 2: Classification with soft-margin SVMs
23
23
24
24
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.
25
25
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
+
<spanstyle="color:red">TODO: Check nr of images per person</span>
27
31
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
32
2. Gather information about the dataset: Print the number of samples, the number of image features (pixels) and the number of classes.
30
33
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.
32
36
33
37
4. Split the data 80:20 into training and test data. Use `random_state=42` in the split function.
34
38
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
39
43
40
44
Now we need to find the best values for our hyperparameters. Implement the hyperparameter search in the function `cv_svm` following these steps:
41
45
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`.)
43
47
44
48
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.
45
49
46
50
7. Print the parameters of the best estimator found with the function `cv_svm`.
47
51
48
52
8. Calculate and print the accuracy of the best performing model.
49
53
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.
51
55
52
56
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.
53
57
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.
56
61
57
-
### Task 3 (Optional): Time-series prediction with SVM
62
+
### ✪ Task 3 (Optional): Time-series prediction with SVM
58
63
59
64
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.
60
65
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.
63
67
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.
64
68
65
69
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
83
87
*$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,
84
88
*and the $gamma$ parameter of the `rbf` kernel.
85
89
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$rangefrom0.1 to 0.001andfor$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$rangefrom0.1 to 0.001andfor$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 next5 days. Don't forget to denormalize your predictions. Use `numpy.round` to round the predictions after denormalization.
89
101
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 next5 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`forall available data.
0 commit comments