DEV Community

Cover image for Machine Learning, classification explanation
petercour
petercour

Posted on

1 1

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:

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay