Skip to content

Commit e8a8aad

Browse files
committed
feature scaling and generic steps to train a model
1 parent 1ad9c44 commit e8a8aad

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

content/04-supervised-ML-classification.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,46 @@ Each algorithm offers a unique approach to pattern recognition and generalizatio
197197

198198
Before training, it is also essential to ensure that numerical features are properly scaled via applying standardization or normalization -- especially for distance-based or gradient-based models -- to achieve optimal results.
199199

200+
.. code-block:: python
201+
202+
from sklearn.preprocessing import StandardScaler
203+
204+
# Standardize features
205+
scaler = StandardScaler()
206+
207+
X_train_scaled = scaler.fit_transform(X_train)
208+
X_test_scaled = scaler.transform(X_test)
209+
210+
211+
Below is the generic steps for training a model:
212+
213+
- choosing a model class and importing that model ``from sklearn.neighbors import XXXClassifier``
214+
- choosing the model hyperparameters by instantiating this class with desired values ``xxx_clf = XXXClassifier(<... hyperparameters ...>)``
215+
- training the model to the preprocessed train data by calling the ``fit()`` method of the model instance ``xxx_clf.fit(X_train_scaled, y_train)``
216+
- making predictions using the trained model on test data ``y_pred_xxx = xxx_clf.predict(X_test)``
217+
- evaluating model’s performance using available metrics ``score_xxx = accuracy_score(y_test, y_pred_xxx)``
218+
- (optional) data visualization of confusion matrix and relevant data
219+
220+
221+
k-Nearest Neighbors (KNN)
222+
^^^^^^^^^^^^^^^^^^^^^^^^^
223+
224+
225+
226+
227+
228+
229+
230+
231+
232+
233+
234+
235+
236+
237+
238+
239+
200240

201241

202242

0 commit comments

Comments
 (0)