Memuat
Memuat
Want to go deeper?
Join one of our courses or ask us directly on WhatsApp.
Picked from the software, topic, and level of “Class Machine Learning Python #6: Classification: Logistic Regression, KNN & Naive Bayes”.
Other ways we can help with Python.
Learn with a mentor
Scheduled AI & Machine Learning courses with live guidance and a certificate.

Multi-Objective Optimization Course: NSGA-II, MOPSO & Hybrid Algorithms (Python)

ECG Arrhythmia Classification Course with Wavelet & Deep Learning (PyTorch)

ANFIS + PSO/GA Course for Non-Linear Time Series Forecasting (Python)
Have it built for you
No time to learn Python? Our team can do the project for you.

Python Machine Learning — Prediction Models, Customer Segmentation, Fraud Detection & AI Apps

AI Image Creation with Stable Diffusion — Unlimited Custom Visual Production
★ 4.9

AI Image & Video Creation with Leonardo AI — All-in-One Visuals for Business
★ 4.9
Get notified about new Python classes
At most one email a week. Unfollow anytime with one click.
The full narration of this video. The current sentence is highlighted while the video plays; click any sentence to jump there.
This is the final result, the accuracy graph of KANN for various values of KA, which helps us choose the best KA. In this episode, we also created a model for breast cancer detection using logistic regression, and an email spam filter in Indonesian using naive Bayes. Let's get started. Hello, welcome to episode 6 of the machine learning series with Python. If regression predicts numbers, classification predicts categories, such as spam or not, gentle or aggressive, flower types, and so on. In this episode, we learn three classic classification algorithms: logistic regression, which is probability-based, KANN, which is distance-based, and naive Bayes, which is theorem-based.
We create a file called sigmoid.py. Logistic regression calculates a linear score like regular regression, then feeds it into a sigmoid function. Sigmoid transforms any number into a value between 0 and 1, so it can be read as a probability. If the probability is above 0.5, the prediction is class 1; if below, it's class 0. The dashed line in the graph is the threshold or cutoff. Now a real-world case, the built-in breast cancer dataset from scikit-learn, 569 patients with 30 features from cell examinations.
Create a file called kanker.py. We split the data with stratify so the proportion of benign and malignant remains the same in both training and test data. We standardize our features with a pipeline, then train logistic regression. The accuracy is almost 99%. The advantage of logistic regression is that it can provide probabilities through predict_proba.
For the first five patients, we look at the probability of being malignant or benign. The confusion matrix shows how many were correctly and incorrectly predicted for each class. We discuss this in more depth in the model evaluation episode.
The second algorithm, KNN or KANN. The idea is simple. To predict new data, look at the K nearest neighbors, then take the most common vote. Create a file called KNN.py. KNN is distance-based, so scaling is very important. We compare accuracy without and with standard scaler, and the results increase quite a bit after features are standardized. Which K-fold is best?
We try K-fold from 1 to 30 and plot the training and test data accuracy. K-fold 1 memorizes the training data, training accuracy is perfect but prone to overfitting. Too large K-fold makes the model too general. We choose K-fold with the highest test accuracy.
The third algorithm, Knife Base, is very popular for text classification. Create a spam.py file. We have 12 examples of Indonesian messages labeled as spam or not. Count Factorizer converts text into word counts. Then Multinomial NBI calculates the probability of each word appearing in spam and not spam. We try 3 new messages, and the model immediately recognizes the reward message as spam.
Done. We have learned the sigmoid function and logistic regression, prediction probability, KNN along with the importance of scaling and choosing K-fold, and spam filtering with Knife Base. In the next episode, we learn a model that is very easy for humans to understand, decision tree and random forest.
Want to master machine learning and data science? Cerdas Inovasi Digital offers Python courses, machine learning, and AI from the basics, predictive model development services, and data analysis and AI applications for business and research, as well as selling the complete source code of this tutorial. Want to master the entire series, from machine learning basics to deploying models to the web? All 18 episodes, complete with step-by-step PDF guides, are available to premium tutorial subscribers. Subscribe starting at Rp69.000 per month, save 28% with the 12-month package, or pay once Rp999.000 for lifetime access. Want just one class? Buy individual classes with lifetime access. Subscribe now on the premium page at cerdasinovasidigital.com. Contact us via WhatsApp, or visit cerdasinovasidigital.com. Thank you for watching, see you in the next episode.
Type a question or keyword; we find the part of this episode that explains it and jump the video there.
Notes are saved with the current video time and are only visible to you.
Stuck on a step? Ask here; the instructor and other learners can answer.
No questions yet.

Cerdas Inovasi Digital Team
Software developers, BIM & 3D engineers, and AI practitioners
We build software, BIM models, games and AI automations for clients every day, and turn that hands-on work into step-by-step classes: every step is shown on screen and comes with a PDF guide you can keep.
See our clients →