Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Data classification in 8 lines of code
petercour
petercour

Posted on

     

Data classification in 8 lines of code

Classification is something very natural to do. Each time you look at something, you decide what group it belongs to. Is it a bird? is it a plane?

So much for human classification. Can computers do the same?
Yes, they can!

The popular solution is Machine Learning or Artificial Intelligence.
The Python module sklearn is a good choice. (Why Python for Machine Learning)

So why Machine Learning?

Can't you just write a bunch of if statements?

Well, great programmers are very lazy. Imagine having to write a computer program each time a customer wants an "intelligent robot".

To much work.

The code must learn itself! How? make the code learn from data.

And so you code

So to start you need to load Python and sklearn. We'll use a classifier named svm.

#!/usr/bin/python3from sklearn import svm
Enter fullscreen modeExit fullscreen mode

Then we need data. No problem, here is a little bit of data.

x = [[2, 0], [1, 1], [2, 3]]y = [0, 0, 1]
Enter fullscreen modeExit fullscreen mode

So what is x and y?

  • x are the measurements.
  • y is the output.

Look at y, there are two possible outputs: 0 and 1.

Remember, svm is a classifier. So it's output is eitherclass 0 orclass 1.

Train and predict

The next step, create the svm and train it. Like humans, Machine Learning algorithms need training or learning.

clf = svm.SVC(kernel = 'linear')clf.fit(x, y)
Enter fullscreen modeExit fullscreen mode

Training time!

After training is completed, it can classify. Given new data [2,0] which class is it:

print (clf.predict([[2,0]]))
Enter fullscreen modeExit fullscreen mode

The magical program below

#!/usr/bin/python3from sklearn import svmx = [[2, 0], [1, 1], [2, 3]]y = [0, 0, 1]clf = svm.SVC(kernel = 'linear')clf.fit(x, y)print (clf.predict([[2,0]]))
Enter fullscreen modeExit fullscreen mode

So if you were curious about the terminology

  • svm = support vector machine
  • classifier = algorithm which outputs class
  • fit = train algorithm
  • artificial intelligence = computer code + data
  • machine learning = algorithms + data

With respect to code, it doesn't really matter what we call it all.

Related links:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More frompetercour

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp