Best Python libraries for machine learning

Arrays | NLP | NumPy | Python Methods and Functions

Machine Learning, as the name suggests, — it is the science of computer programming with which they can study different kinds of data. A more general definition given by Arthur Samuel is “Machine Learning”; it is an area of ​​learning that enables computers to learn without explicit programming. " They are commonly used to solve various types of life problems.
In the old days, people used to perform machine learning tasks by manually coding all the algorithms and mathematical and statistical formulas. This made the process time consuming, tedious, and inefficient. But these days it has become very easy and efficient compared to the days of old thanks to various Python libraries, frameworks and modules. Python is one of the most popular programming languages ​​for this task today and has replaced many languages ​​in the industry, one of the reasons is its vast collection of libraries. Python Libraries Used in Machine Learning:

  • Numpy
  • SciPy
  • Scikit Learn
  • Theano
  • TensorFlow
  • Keras
  • PyTorch
  • Pandas
  • Matplotlib

Numpy

NumPy — it is a very popular python library for handling large multidimensional arrays and matrices with a large set of high level math functions. This is very useful for fundamental scientific computing in machine learning. This is especially useful for linear algebra, Fourier transforms, and random number possibilities. High quality libraries like TensorFlow use NumPy to manipulate Tensors.

# Python program using NumPy
# for some basic math
# operations

  

import numpy as np

  
# Create two arrays of rank 2

x = np.array ([[ 1 , 2 ], [ 3 , 4 ]])

y = np.array ([[ 5 , 6 ], [ 7 , 8 ]])

 
# Create two rank 1 arrays

v = np.array ([ 9 , 10 ])

w = np.array ([ 11 , 12 ])

 
# Inner product of vectors

print (np.dot (v, w), "" )

 
# Matrix and vector product

print (np.dot (x, v), "" )

 
# Matrix and matrix product

print (np.dot (x, y))

Output:

 219 [29 67] [[19 22] [43 50]] 

For more information refer to Numpy .

SciPy

SciPy — it is a very popular library among machine learning enthusiasts as it contains various modules for optimization, linear algebra, integration and statistics. There is a difference between the SciPy library and the SciPy stack. SciPy is one of the main packages that make up the SciPy stack. SciPy is also very useful for image manipulation.

# Python script using Scipy
# for working with images

 

from scipy.misc import imread, imsave, imresize

 
# Reading a JPEG image into an array

img = imread ( `D: / Programs / cat.jpg` ) # path to image

print (img.dtype, img.shape)

 
# Image tinting

img_tint = img * [ 1 , 0.45 , 0.3 ]

 
# Save toned image

imsave ( `D: / Programs / cat_tinted.jpg ` , img_tint)

  
# Resize rendered image to 300 x 300 pixels

img_tint_resize = imresize (img_tint, ( 300 , 300 ))

 
# Save the modified rendered image

imsave ( ` D: / Programs / cat_tinted_resized.jpg` , img_tint_resize)

Original image:

Rendered image:

Resize rendered image:

For more information, see documentation .

Scikit learn

Skikit-learn — one of the most popular ML libraries for classic ML algorithms. It is built on top of two main Python libraries, namely NumPy and SciPy. Scikit-learn supports most supervised and unsupervised learning algorithms. Scikit-learn can also be used for data analysis and data analysis, making it a great tool for beginners with ML.

# Python script using Scikit-learn
# for the decision tree classifier

 
# Decision tree classifier example

from sklearn import datasets

from sklearn import metrics

from sklearn.tree import DecisionTreeClassifier

  
# load iris datasets

dataset = datasets.load_iris ()

 
# fit the CART model to the data

model = DecisionTreeClassifier ()

model.fit (dataset.data, dataset.target)

print (model)

 
# make predictions

expected = dataset.target

predicted = model.predict (dataset.data)

 
# summarize model fitting

print (metrics.classification_report (expected, predicted))

print (metrics.confusion_matrix (expected , predicted))

Output:

 DecisionTreeClassifier (class_weight = None, criterion = `gini`, max_depth = None, max_features = None, max_leaf_nodes = None, min_impurity_decrease = 0.0, min_impurity_split = None, min_samples_sleafs = 1 0.0, presort = False, random_state = None, splitter = `best`) precision recall f1-score support 0 1.00 1.00 1.00 50 1 1.00 1.00 1.00 50 2 1.00 1.00 1.00 50 micro avg 1.00 1.00 1.00 150 macro avg 1.00 1.00 1.00 150 weighted avg 1.00 1.00 1.00 150 [[50 0 0] [0 50 0] [0 0 50]] 

For more information, refer to documentation .

Theano

We all know machine learning is — it`s mostly mathematics and statistics. Theano — a popular Python library that is used to efficiently define, evaluate, and optimize mathematical expressions involving multidimensional arrays. It does this by optimizing CPU and GPU usage. It is widely used for unit testing and self-testing to detect and diagnose various types of errors. Theano — a very powerful library that has been used for a long time in large-scale computationally intensive scientific projects, but it is simple and affordable enough to be used by individuals for their own projects.

# Python program using Theano
# for calculating logistics
# Function

 

import theano

import theano.tensor as T

x = T.dmatrix ( ` x` )

s = 1 / ( 1 + T.exp ( - x))

logistic = theano.function ([x], s)

logistic ([[ 0 , 1 ], [ - 1 , - 2 ]])

Exit:

 array ([[0.5, 0.73105858], [0.26894142, 0.11920292]]) 

For more information, refer to documentation .

TensorFlow

# Python program using TensorFlow
# to multiply two arrays

 
# import `tensorflow`

import tensorflow as tf

 
# Initialize two constants

x1 = tf.constant ([ 1 , 2 , 3 , 4 ])

x2 = tf.constant ([ 5 , 6 , 7 , 8 ])

 
# Multiply

result = tf.multiply (x1, x2)

  
# Initialize session

sess = tf.Session ()

  
# Print result

print (sess.run (result))

 
# Close session
sess.close ()

Exit:

 [5 12 21 32] 

For see documentation .

Keras

#

Keras & & quot; 8212; very popular machine learning library for Python. It is a high-level neural network API capable of running on top of TensorFlow, CNTK, or Theano. It can run on both CPU and GPU. Keras makes it really for ML beginners to build and design a neural network. One of the best benefits of Keras is that it makes prototyping easy and fast.

For more details, refer to the documentation .

PyTorch

/espressocode.top/images/prolilatelgestump379117.jpg "/>

PyTorch — is a popular open source machine learning library for Python based on Torch, which is an open source machine learning library that is implemented in C with a Lua wrapper. It has a wide variety of tools and libraries that support Computer Vision, Natural Language Processing (NLP) and many other ML programs. This allows developers to perform GPU-accelerated tensor computations and also helps in the creation of computational graphs.

# Python program using PyTorch
# to define tensors
# random two-layer network
# data and loss calculation

  

import torch

 

 

dtype = torch. float

device = torch.device ( "cpu" )

# device = torch.device (& quot ; cuda: 0 & quot;) Uncomment this to run on GPU

 
# N - lot size; D_in - input dimension;
# H - hidden dimension; D_out is the output dimension.

N, D_in, H, D_out = 64 , 1000 , 100 , 10

 
# Generate random inputs and outputs

x = torch.randn (N, D_in, device = device, dtype = dtype)

y = torch.randn (N, D_out, device = device, dtype = dtype)

 
# Arbitrary initializing weights

w1 = torch.randn (D_in, H, device = device, dtype = dtype)

w2 = torch.randn (H, D_out, device = device, dtype = dtype)

 

learning_rate = 1e - 6

for t in range ( 500 ):

# Forward pass: compute predicted y

h = x.mm (w1)

  h_relu = h.clamp ( min = 0 )

y_pred = h_relu.mm (w2)

 

  # Calculate and lose print

  loss = (y_pred - y). pow ( 2 ). sum (). item ()

print (t, loss)

 

# Backprop for calculating gradients w1 and w2 regarding losses

grad_y_pred = 2.0 * (y_pred - y)

grad_w2 = h_relu.t (). mm (grad_y_pred)

grad_h_relu = grad_y_pred.mm (w2.t ())

grad_h = grad_h_relu.clone ()

  grad_h [h & lt; 0 ] = 0

grad_w1 = xt (). mm (grad_h)

 

# Updating weights using gradient descent

  w1 - = learning_rate * grad_w1

w2 - = learning_rate * grad_w2

Logout :

 0 47168344.0 1 46385584.0 2 43153576.0 ... ... ... 497 3.987660602433607e-05 498 3.945609932998195e-05 499 3.897604619851336e-05 

For see documentation for more details.

Pandas

Pandas — it is a popular Python library for data analysis. This is not directly related to machine learning. As we know, the dataset must be prepared prior to training. This is where Pandas comes in handy as it was designed specifically for retrieving and preparing data. It provides high-level data structures and a variety of data analysis tools. It provides many built-in methods for finding, merging and filtering data.

# Python program using Pandas for
# location of the given dataset
# on the table

  
# import pandas as pd

import pandas as pd

 

data = { "country" : [ "Brazil" , "Russia" , " India " , " China " , " South Africa " ],

"capital" : [ "Brasilia" , "Moscow" , " New Dehli " , " Beijing " , "Pretoria" ],

"area" : [ 8.516 , 17.10 , 3.286 , 9.597 , 1.221 ],

  " population " : [ 200.4 , 143.5 , 1252 , 1357 , 52.98 ]}

                                                                                                                                                                   

data_table = pd.DataFrame (data)

print (data_table)

Output:

For more information, refer to
#pol12; very popular Python library for data visualization. Like Pandas, this is not directly related to machine learning. This is especially useful when a programmer wants to visualize patterns in data. It is a 2D plotting library used to create 2D plots and graphs. A module called pyplot makes plotting easier for programmers because it provides functions to manipulate line styles, font properties, formatting axes, etc. It provides various kinds of graphs and graphs for visualizing data, namely histograms, error charts, histograms, and etc,

# Python program using Matplotib
# to generate a line graph

 
# import required packages and modules

import matplotlib.pyplot as plt

import numpy as np

 
# Prepare data

x = np.linspace ( 0 , 10


Get Solution for free from DataCamp guru