DEV Community

Cover image for Machine Learning, classification explanation
petercour
petercour

Posted on

Machine Learning, classification explanation

Do you have data? Machine Learning lets you play with data. In this article we'll use Python (Why Python for Machine Learning)

Data can be classified with algorithms. So the first thing you need is data. You can measure objects and collect data on it.

For instance, suppose you have these measurements. (2 values for each measurement).

From the look of it, there's clearly 2 classes. An algorithm could find these two classes.

Then you can predict the class, given two new measurements. So how do you know how well an algorithm works? is it accurate?

You can split the data set into a training set and a test set. Then you can run the algorihtm and measure the output. As the example below:

#!/usr/bin/python3
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.datasets.samples_generator import make_classification
from sklearn.svm import SVC

import matplotlib.pyplot as plt


X,y = make_classification(n_samples=600,n_features=2,n_redundant=0,n_informative=2,
                          random_state=2020,n_clusters_per_class=1,scale=100)

plt.scatter(X[:,0],X[:,1],c='black')
plt.show()

plt.scatter(X[:,0],X[:,1],c=y)
plt.show()

X = preprocessing.scale(X)
X_train, X_test,y_train,y_test = train_test_split(X, y, test_size=0.3)
clf = SVC()
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))
Enter fullscreen mode Exit fullscreen mode

Related links:

Top comments (0)