0% found this document useful (0 votes)
17 views11 pages

Python SciKit Learn Tutorial _ DigitalOcean

The document is a tutorial on Python's Scikit-learn library, which is used for machine learning and includes various algorithms for regression, classification, and clustering. It covers installation instructions, basic usage, and examples of different machine learning models such as SVM, linear regression, k-nearest neighbors, and k-means clustering. The tutorial emphasizes that Scikit-learn is open source and popular in the community, making it accessible for users to experiment with machine learning techniques.

Uploaded by

rodrigo camargos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views11 pages

Python SciKit Learn Tutorial _ DigitalOcean

The document is a tutorial on Python's Scikit-learn library, which is used for machine learning and includes various algorithms for regression, classification, and clustering. It covers installation instructions, basic usage, and examples of different machine learning models such as SVM, linear regression, k-nearest neighbors, and k-means clustering. The tutorial emphasizes that Scikit-learn is open source and popular in the community, making it accessible for users to experiment with machine learning techniques.

Uploaded by

rodrigo camargos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

CONTENTS
Scikit Learn
Python Scikit-learn
Using Scikit-Learn
Conclusion
R E L AT E D

Python time sleep()


View
Vectors in Python - A Quick Introduction!
View

// Tutorial //

Python SciKit Learn Tutorial

Published on August 3, 2022


Machine Learning Python
By Shubham
Developer and author at DigitalOcean.

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 1/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

While we believe that this content benefits our community, we have not yet thoroughly
reviewed it. If you have any suggestions for improvements, please let us know by clicking
the “report an issue“ button at the bottom of the tutorial.

Scikit Learn

Web hosting without headaches. Try Cloudways with $100 in free We're Blog Docs Get Sales
credit! Sign up -> hiring Support
Scikit-learn is a machine learning library for Python. It features several regression,
classification and clustering algorithms including SVMs, gradient boosting, k-means,
random forests and DBSCAN. It is designed to work with Python Numpy and SciPy. The
ialsscikit-learn
QuestionsprojectLearning
kicked Paths
off as a Google Summer of Product
For Businesses Code (also
DocsknownSocial
as GSoC)
Impact
project by David Cournapeau as scikits.learn. It gets its name from “Scikit”, a separate
third-party extension to SciPy.
https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 2/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

Python Scikit-learn

Scikit is written in Python (most of it) and some of its core algorithms are written in
Cython for even better performance. Scikit-learn is used to build models and it is not
recommended to use it for reading, manipulating and summarizing data as there are
better frameworks available for the purpose. It is open source and released under BSD
license.
Install Scikit Learn

Scikit assumes you have a running Python 2.7 or above platform with NumPY (1.8.2 and
above) and SciPY (0.13.3 and above) packages on your device. Once we have these
packages installed we can proceed with the installation. For pip installation, run the
following command in the terminal:
pip install scikit-learn

If you like conda , you can also use the conda for package installation, run the following
command:
conda install scikit-learn

Using Scikit-Learn

Once you are done with the installation, you can use scikit-learn easily in your Python
code by importing it as:
import sklearn

Scikit Learn Loading Dataset

Let’s start with loading a dataset to play with. Let’s load a simple dataset named Iris. It is
a dataset of a flower, it contains 150 observations about different measurements of the
flower. Let’s see how to load the dataset using scikit-learn.
# Import scikit learn
from sklearn import datasets
# Load data
iris= datasets.load_iris()
# Print shape of data to confirm data is loaded
print(iris.data.shape)

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 3/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

We are printing shape of data for ease, you can also print whole data if you wish so,
running the codes gives an output like this:

Scikit Learn SVM - Learning and Predicting

Now we have loaded data, let’s try learning from it and predict on new data. For this
purpose we have to create an estimator and then call its fit method.
from sklearn import svm
from sklearn import datasets
# Load dataset
iris = datasets.load_iris()
clf = svm.LinearSVC()
# learn from the data
clf.fit(iris.data, iris.target)
# predict for unseen data
clf.predict([[ 5.0, 3.6, 1.3, 0.25]])
# Parameters of model can be changed by using the attributes ending with an und
print(clf.coef_ )

Here is what we get when we run this script:

Scikit Learn Linear Regression

Creating various models is rather simple using scikit-learn. Let’s start with a simple
example of regression.
#import the model
from sklearn import linear_model
reg = linear_model.LinearRegression()
# use it to fit a data
reg.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
# Let's look into the fitted data
print(reg.coef_)

Running the model should return a point that can be plotted on the same line:
https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 4/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

k-Nearest neighbour classifier

Let’s try a simple classification algorithm. This classifier uses an algorithm based on ball
trees to represent the training samples.
from sklearn import datasets
# Load dataset
iris = datasets.load_iris()
# Create and fit a nearest-neighbor classifier
from sklearn import neighbors
knn = neighbors.KNeighborsClassifier()
knn.fit(iris.data, iris.target)
# Predict and print the result
result=knn.predict([[0.1, 0.2, 0.3, 0.4]])
print(result)

Let’s run the classifier and check results, the classifier should return 0. Let’s try the
example:

K-means clustering

This is the simplest clustering algorithm. The set is divided into ‘k’ clusters and each
observation is assigned to a cluster. This is done iteratively until the clusters converge.
We will create one such clustering model in the following program:
from sklearn import cluster, datasets
# load data
iris = datasets.load_iris()
# create clusters for k=3
k=3
k_means = cluster.KMeans(k)
# fit data
k_means.fit(iris.data)
# print results
print( k_means.labels_[::10])
print( iris.target[::10])

On running the program we’ll see separate clusters in the list. Here is the output for
above code snippet:
https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 5/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

Conclusion

In this tutorial, we have seen that Scikit-Learn makes it easy to work with several
machine learning algorithms. We have seen examples of Regression, Classification and
Clustering. Scikit-Learn is still in development phase and being developed and
maintained by volunteers but is very popular in community. Go and try your own
examples.

If you’ve enjoyed this tutorial and our broader community, consider checking
out our DigitalOcean products which can also help you achieve your
development goals.
Learn more here ->

Get $200 to try DigitalOcean - and do all the below

for free!

Build applications, host websites, run open source software, learn cloud
computing, and more – every cloud resource you need. If you’ve never tried
DigitalOcean’s products or services before, we’ll cover your first $200 in the next
60 days.
Sign up now to activate this offer ->

About the authors

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 6/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

Shubham Author
Developer and author at DigitalOcean.

Still looking for an answer?

Ask a question Search for more help

Was this helpful? Yes No

Comments

JournalDev • July 12, 2018


Hi,… While installing i am getting the following error rom distutils customize
MSVCCompiler Missing compiler_cxx fix for MSVCCompiler customize
MSVCCompiler using build_clib building ‘libsvm-skl’ library compiling C sources
error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++
Build Tools”: https://landinghub.visualstudio.com/visual-cpp-build-tools -
--------------------------------------- Command
“c:\users\sidtrive\appdata\local\programs\python\python37-32\python.exe -u -
c “import setuptools, tokenize;__file__=‘C:\\Users\\sidtrive\\AppData\\Local
\\Temp\\pip-install-2rnp9ekh\\scikit-learn\\setup.py’;f=getattr(tokenize, ‘open’ ,
open)(__file__);code=f.read().replace(‘\r\n’, ‘\n’);f.close();exec(compile(cod e,
__file__, ‘exec’))” install --record C:\Users\sidtrive\AppData\Local\Temp\pip -
record-g6eq9i3m\install-record.txt --single-version-externally-managed --
compil e” failed with error code 1 in C:\Users\sidtrive\AppData\Local\Temp\pip-
install- 2rnp9ekh\scikit-learn\

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 7/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

- Sid

This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0


International License.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!
Sign up ->

Popular Topics

Ubuntu
Linux Basics
JavaScript
React
Python
Security
MySQL
Docker
Kubernetes
Browse all topic tags
Free Managed Hosting ->

All tutorials ->

Questions

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 8/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

Q&A Forum
Ask a question
DigitalOcean Support
Congratulations on unlocking the whale ambience easter egg! Click the whale button
in the bottom left of your screen to toggle some ambient whale noises while you
read.
Thank you to the Glacier Bay National Park & Preserve and Merrick079 for the sounds
behind this easter egg.
Interested in whales, protecting them, and their connection to helping prevent climate
change? We recommend checking out the Whale and Dolphin Conservation.
Reset easter egg to be discovered again / Permanently dismiss and hide easter egg

GET OUR BIWEEKLY NEWSLETTER


Sign up for Infrastructure as a
Newsletter.

HOLLIE'S HUB FOR GOOD


Working on improving health and
education, reducing inequality,

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 9/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

and spurring economic growth?


We'd like to help.

BECOME A CONTRIBUTOR
You get paid; we donate to tech
nonprofits.

Featured on Community Kubernetes Course Learn Python 3 Machine Learning in Python


Getting started with Go Intro to Kubernetes
DigitalOcean Products Virtual Machines Managed Databases Managed Kubernetes Block Storage
Object Storage Marketplace VPC Load Balancers

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the


cloud and scale up as you grow – whether you’re
running one virtual machine or ten thousand.
Learn More

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 10/11
12/2/22, 10:38 AM Python SciKit Learn Tutorial | DigitalOcean

Company Products Community Solutions Contact


About Products Tutorials Website Support
Leadership Overview Q&A Hosting Sales
Blog Droplets CSS-Tricks VPS Hosting Report Abuse
Careers Kubernetes Write for Web & Mobile System Status
App Platform DOnations Apps
Customers Game Share your
Partners Functions Currents Research Development ideas
Channel Cloudways Hatch Startup Streaming
Partners Managed Program
Databases deploy by VPN
Referral DigitalOcean SaaS Platforms
Program Spaces
Affiliate Marketplace Shop Swag Cloud Hosting
Program Research Program for Blockchain
Load Balancers Startup
Press Block Storage Open Source Resources
Legal Tools & Code of Conduct
Security Integrations Newsletter Signup
Investor API Meetups
Relations Pricing
DO Impact Documentation
Release Notes
Uptime

© 2022 DigitalOcean, LLC. All rights


reserved.

https://www.digitalocean.com/community/tutorials/python-scikit-learn-tutorial 11/11

You might also like