Scikit-learn Classification
Supervised Learning with scikit-learn
Machine Learning with scikit-learn
Before using supervise learning
- Requirements
- No missing values
- Data in numeriv format
- Data sorted in pandas DataFrame or NumPy array
- Perform Exploratory Data Analysis (EDA) first
Scikit-learn Syntax
- Import scikit-learn.
1
from sklearn.module import Model
K-Nearest-Neighbors:
1
from sklearn.neighbors import KNeighborsClassifier
- Train/test split.
1 2
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 21 stratify = y)
- Build a model.
1
model = Model()
K-Nearest-Neighbors:
1
knn = KneighborsClassifier(n_neighbors=15)
- Model learns from the labeled data (training data) passed to it.
1
model.fit(X,y)
K-Nearest-Neighbors:
1
knn.fit(X,y)
- Pass unlabeled data to the model as input, model predicts the labels of the unseen data.
1
prediction = model.predict(X_new)
Check accuracy K-Nearest-Neighbors:
1
knn.score(X_test,y_test))
References
This post is licensed under CC BY 4.0 by the author.