Skip to content

Commit f26f8da

Browse files
authored
Update grid-search.md
1 parent ef42e88 commit f26f8da

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed
+21-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
# Grid Search
22

33
Grid Search is a hyperparameter tuning technique in Machine Learning that helps to find the best combination of hyperparameters for a given model. It works by defining a grid of hyperparameters and then training the model with all the possible combinations of hyperparameters to find the best performing set.
4+
45
The Grid Search Method considers some hyperparameter combinations and selects the one returning a lower error score. This method is specifically useful when there are only some hyperparameters in order to optimize. However, it is outperformed by other weighted-random search methods when the Machine Learning model grows in complexity.
56

67
## Implementation
78

8-
Before applying Grid Searching on any algorithm, Data is used to divided into training and validation set, a validation set is used to validate the models. A model with all possible combinations of hyperparameters is tested on the validation set to choose the best combination.
9-
Grid Searching can be applied to any hyperparameters algorithm whose performance can be improved by tuning hyperparameter. For example, we can apply grid searching on K-Nearest Neighbors by validating its performance on a set of values of K in it. Same thing we can do with Logistic Regression by using a set of values of learning rate to find the best learning rate at which Logistic Regression achieves the best accurac
9+
Before applying Grid Searching on any algorithm, data is divided into training and validation set, a validation set is used to validate the models. A model with all possible combinations of hyperparameters is tested on the validation set to choose the best combination.
10+
11+
Grid Searching can be applied to any hyperparameters algorithm whose performance can be improved by tuning hyperparameter. For example, we can apply grid searching on K-Nearest Neighbors by validating its performance on a set of values of K in it. Same thing we can do with Logistic Regression by using a set of values of learning rate to find the best learning rate at which Logistic Regression achieves the best accuracy.
12+
1013
Let us consider that the model accepts the below three parameters in the form of input:
11-
1. Number of hidden layers [2, 4]
12-
2. Number of neurons in every layer [5, 10]
13-
3. Number of epochs [10, 50]
14+
1. Number of hidden layers `[2, 4]`
15+
2. Number of neurons in every layer `[5, 10]`
16+
3. Number of epochs `[10, 50]`
17+
18+
If we want to try out two options for every parameter input (as specified in square brackets above), it estimates different combinations. For instance, one possible combination can be `[2, 5, 10]`. Finding such combinations manually would be a headache.
1419

15-
If we want to try out two options for every parameter input (as specified in square brackets above), it estimates different combinations. For instance, one possible combination can be [2, 5, 10]. Finding such combinations manually would be a headache.
1620
Now, suppose that we had ten different parameters as input, and we would like to try out five possible values for each and every parameter. It would need manual input from the programmer's end every time we like to alter the value of a parameter, re-execute the code, and keep a record of the outputs for every combination of the parameters.
21+
1722
Grid Search automates that process, as it accepts the possible value for every parameter and executes the code in order to try out each and every possible combination outputs the result for the combinations and outputs the combination having the best accuracy.
23+
1824
Higher values of C tell the model, the training data resembles real world information, place a greater weight on the training data. While lower values of C do the opposite.
1925

2026
## Explaination of the Code
@@ -32,37 +38,34 @@ The code provided performs hyperparameter tuning for a Logistic Regression model
3238
10. logit.score(X, y) calculates the accuracy of the fitted model on the dataset and appends this score to the scores list.
3339
11. After the loop, the scores list is printed, showing the accuracy for each value of C.
3440

35-
## Python Code
41+
### Python Code
3642

43+
```python
3744
from sklearn import datasets
38-
3945
from sklearn.linear_model import LogisticRegression
4046

4147
iris = datasets.load_iris()
42-
4348
X = iris['data']
44-
4549
y = iris['target']
4650

4751
logit = LogisticRegression(max_iter = 10000)
4852

4953
C = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
5054

5155
scores = []
52-
5356
for choice in C:
54-
5557
logit.set_params(C=choice)
56-
5758
logit.fit(X, y)
58-
5959
scores.append(logit.score(X, y))
60-
6160
print(scores)
61+
```
6262

63-
## Results
63+
#### Results
6464

65+
```
6566
[0.9666666666666667, 0.9666666666666667, 0.9733333333333334, 0.9733333333333334, 0.98, 0.98, 0.9866666666666667, 0.9866666666666667]
67+
```
68+
69+
We can see that the lower values of `C` performed worse than the base parameter of `1`. However, as we increased the value of `C` to `1.75` the model experienced increased accuracy.
6670

67-
We can see that the lower values of C performed worse than the base parameter of 1. However, as we increased the value of C to 1.75 the model experienced increased accuracy.
68-
It seems that increasing C beyond this amount does not help increase model accuracy.
71+
It seems that increasing `C` beyond this amount does not help increase model accuracy.

0 commit comments

Comments
 (0)