-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* KNN model trained and tested on generated.csv dataset * Effect of split ratio on performance * Hyperparameter tuning and cross validation implemented * Black formatting applied Co-authored-by: mlopatka <[email protected]>
- Loading branch information
Showing
4 changed files
with
479 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Import necessary modules | ||
from sklearn.metrics import confusion_matrix | ||
from sklearn.metrics import classification_report | ||
from sklearn.model_selection import train_test_split | ||
import k_nn | ||
|
||
|
||
def vary_size(X, y): | ||
"""This function trains tests and evaluates the performance of a dataset in comparison | ||
with a changing train-to-test size ratio. It returns two lists of values; accuracy and test_ratio which | ||
containing the values of accuracy and test_size respectively""" | ||
accuracy = [] | ||
test_ratio = [] | ||
performance = "\n PERFORMANCE \n " | ||
"""starting the range iterator from zero or ending at 101 raises a ValueError: | ||
The test_size = 0 (1.0) should be greater or equal to the number of classes = 4""" | ||
for i in range(5, 100, 5): | ||
size = i / 100 | ||
# split data set into train and test sets | ||
data_train, data_test, target_train, target_test = train_test_split( | ||
X, y, test_size=size, random_state=10, stratify=y | ||
) | ||
# Evaluation of the performance of the K-nearest neighbors prediction model | ||
kn_accuracy, target_pred = k_nn.k_nearest( | ||
data_train, target_train, data_test, target_test | ||
) | ||
# generate classification report to observe values of precision, recall, f1_score and support | ||
class_report = classification_report(target_test, target_pred) | ||
# separator string demarcates results of one iteration from the other | ||
separator = ("+" * 100) + " \n" | ||
performance = performance + separator + class_report + " \n" | ||
# update lists | ||
accuracy.append(kn_accuracy) | ||
test_ratio.append(size) | ||
|
||
return (test_ratio, accuracy, performance) |
Oops, something went wrong.