- Machine Learning for OpenCV
- Michael Beyeler
- 305字
- 2021-07-02 19:47:26
Training the classifier
Creating a logistic regression classifier involves pretty much the same steps as setting up k-NN:
In [14]: lr = cv2.ml.LogisticRegression_create()
We then have to specify the desired training method. Here, we can choose cv2.ml.LogisticRegression_BATCH or cv2.ml.LogisticRegression_MINI_BATCH. For now, all we need to know is that we want to update the model after every data point, which can be achieved with the following code:
In [15]: lr.setTrainMethod(cv2.ml.LogisticRegression_MINI_BATCH)
... lr.setMiniBatchSize(1)
We also want to specify the number of iterations the algorithm should run before it terminates:
In [16]: lr.setIterations(100)
We can then call the train method of the object (in the exact same way as we did earlier), which will return True upon success:
In [17]: lr.train(X_train, cv2.ml.ROW_SAMPLE, y_train)
Out[17]: True
As we just saw, the goal of the training phase is to find a set of weights that best transform the feature values into an output label. A single data point is given by its four feature values (f0, f1, f2, f3). Since we have four features, we should also get four weights, so that x = w0 f0 + w1 f1 + w2 f2 + w3 f3, and ?=σ(x). However, as discussed previously, the algorithm adds an extra weight that acts as an offset or bias, so that x = w0 f0 + w1 f1 + w2 f2 + w3 f3 + w4. We can retrieve these weights as follows:
In [18]: lr.get_learnt_thetas()
Out[18]: array([[-0.04109113, -0.01968078, -0.16216497, 0.28704911, 0.11945518]], dtype=float32)
This means that the input to the logistic function is x = -0.0411 f0 - 0.0197 f1 - 0.162 f2 + 0.287 f3 + 0.119. Then, when we feed in a new data point (f0, f1, f2, f3) that belongs to class 1, the output ?=σ(x) should be close to 1. But how well does that actually work?
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- Objective-C Memory Management Essentials
- Learning RabbitMQ
- Mastering Kotlin
- Python爬蟲開發與項目實戰
- WordPress 4.0 Site Blueprints(Second Edition)
- 新一代SDN:VMware NSX 網絡原理與實踐
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- 硬件產品設計與開發:從原型到交付
- UI設計基礎培訓教程(全彩版)
- 從零開始學Selenium自動化測試:基于Python:視頻教學版
- 面向對象程序設計及C++(第3版)
- UI動效設計從入門到精通
- Mastering JavaScript Promises
- 第五空間戰略:大國間的網絡博弈