In this tutorial, you will learn about Python libraries for machine learning with example ( python machine learning library) and python packages for machine learning with example. The definition of the ML is allowing the computers to learn and ob serve the dataset to access the properties of data. The web technology is growing the analyses of data is based on pattern by human intervention and the computer programs are developed who can analyze the data. The science of programming a computer by which the computer will be able to learn from the data. In other words ML is field of study given by computer the ability to learn programmed and solve the real life problems. The previous process of performing the ML is by manual coding the algorithms and statistical formulas. But this process required much time and tedious but nowadays it has become easier. The python language has become popular for the task of ML and replaced by many languages in the IT industry. The process of Ml is dependent on the python libraries. The libraries are set for the functions and written in the language. The Ml is based on the statistics, mathematics and optimization
It is used by the scientists those who task need to be integrated with web apps.
Python is mainly used in the ML field and its combination consists of syntax, development and flexibility which makes well suitable.
The libraries used in the python language for machin learning are as follows:-
The keras is the coolest ML library used in the python programming language.
The beginners in learning ML should use the keras library because it is the easiest and express neutral networks.
The utilities are provided by keras for processing, compiling models and visualization of the graphs.
The keras use either Theano or Tensorflow as backend internally also we can use other networks as CNTK.
The keras library is slow as compared to other libraries and the reason is because it constructs a graph using the backend infrastructure then uses it.
The model of keras are portable and provides many preprocessed data sets and models like squeezeNet,VGG,ResNet,etc.
It is developed by francois chollet for research and has integrated as part of tensorflow.
It provides the API and work with pre trained of models like AlexNET, VGG.
It is easy to understand and helps in the rapid prototyping.
The capability to work for low level library is like theano, mznet, etc.
The performance is dependent on the backend which is used.
The high level library will make it difficult to develop custom components and the loss of functions.
import torch
dtype=torch. float
device=torch. device (“cpu”)
N, D_in, H, D_out=64, 1000, 100, 10
x=torch.randn (N.D_in, device=device, type=dtype)
y=torch.randn (N.D_in, device=device, type=dtype)
w1=torch.randn (D_in, H, device=device, type=dtype)
w2=torch.randn (H.D_out, device=device, type=dtype)
learning_rate=1e-6
for t in range(500):
h=x.mm (w1)
h_relu=h.clamp (min=0)
y_pred=h_relu.mm (w2)
loss= (y_pred-y).pow (2).sum ().item ()
print(t, loss)
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<0] =0
grad_w1=x.t ().mm (grad_h)
w1=learning_rate*grad_w1
w2=learning_rate*grad_w2
Output:-
047168344.0
146385584.0
……..
4993.897604
This library is most popular in the ML of the python programming language.
The examples of these libraries will include the decision trees-means and so on.
The scikit learn will build the basic libraries called as scipy and the Numpy.
It will add the algorithm set for common ML and the data mining task.
The non voice Ml will use the scikit learn tool to work with the algorithms.
The scikit will provide the powerful transform and paradigm to learn data and transform the data.
Advantages:-
Disadvantages:-
0 |
1 |
1.0 |
1.0 |
50 |
1 |
1 |
1.0 |
1.0 |
50 |
2 |
1 |
1.0 |
1.0 |
50 |
Micro avg |
1 |
1.0 |
1.0 |
150 |
Micro avg |
1 |
1.0 |
1.0 |
150 |
Weighted avg |
1 |
1.0 |
1.0 |
150 |
[[50 0 0]
[0 50 0]
[0 0 50]]
Advantages:-
Disadvantages:-
import tensorflow as tf
x1=tf.constant ([1, 2, 3, 4])
x2=tf.constant ([5, 6, 7, 8])
result=tf.multiply(x1, x2)
sess=tf.Session ()
print(sess.run (result))
sess.close
Output:-
[5 12 21 32]
The best and popularly used library for the data analysis is the pandas.
This library is related to the ML from the python programming language.
The pandas have data extraction and preparation techniques which provide data structure for data analysis.
The pandas will also provide inbuilt methods for combining and filtering of data.
The pandas will think on the relational data and library will provide expressive data structures for the data manipulation.
It is faster and used in the programming language.
The pandas are the open souce library which is used to perform the data manipulation in the python language.
This library is build on top of Numpy and need the Numpy to operate.
The library will provide easy way to create and manipulate and provide elegant solution.
Pandas interface is used to express images and sound waves of streams as arrays of real numbers with N dimensions.
The Numpy is very much important for Machine Learning and Data Science.
Pandas is also called as the python package and operate at the fastest sped and designed to make working with ‘relationa’ and ‘labeled’.
Advantages:-
Disadvantages:-
import pandas as pd
data={“country”:[“Brazil”,”Russia”,”India”,”china”,”south Africa”],”capital”:[“Brasilia”,”Moscow”,”New Delhi”,”Beijing”,”Pretoria”],”area”:[8.5,17.10,3.28,9.5,1.22],”population”:[200.4,43.5,1252,1357,52.98]}
Data_table=pd.Dataframe (data)
Print(data_table)
Output:-
This is python library build by the Numpy and uses the Numpy arrays. The pronunciation is as sigh-pie important library used all the times. This is widely used for computing library for the python programming. The scipy is used for performing the operations like the probability, integration and regression. The scipy is used in the ML and contains the modules for linear algebra and the optimization. The difference between the scipy library and scipy stack is the image manipulation.
Example:-
from scipy.misc import imreas, imsave, imresize
img=imread (‘D: /programs/cat.jpg’)
print(img.dtype, img, shape)
img_tint=img*[1, 0.45, 0.3]
imsave (‘D: /Programs/cat_tinted.jpg’, img_tint)
Img_tint_resize=imresize (img_tint, (300,300))
Imsave(‘D: /programs/cat_tinted_resized.jpg’, img_tint_resize)
Output:-
|
Original image:
Tinted image:
Resized tinted image:
The sympy is the symbolic computation python library which focuses on the algebraic computation.
The data scientist uses the symbolic computation and focus on the algebraic computation.
They use for the intermediate analysis of data and consumed by the ML libraries in the python programming.
Example:-
From sympy import *a=Rational(5, 8)
Print(“value of a is:”+str(a))
B=Integer(3.5)
Print(“value of b is:”+str (b))
Output:-
Value of a is: 1/2
Value of b is: 3
This library is free and open source toolbox used for the implementation in ML as the c++ language.
It will support the interface of multiple languages as the Ruby, c#, java, python etc and the platforms as macOS, Linux, Windows.
The shogun is free of cost and can be used by any scientist, journalist, students, etc.
The shogun will provide the effective implementation of algorithms like hypothesis, kernel learning, etc.
The library will come with the binary installation of the packages and provide an extensive testing infrastructure.
The shogun can scale the OS and process about 10 million of data samples accurately.
The shogun is available for the educational purposes at universities and acts as non commercial.
The Tensorflow is used to write a program in the python language and you can compile, run on the CPU or GPU.
Its use is for multilayered nodes which allow for the quick setup and work with large data sets.
It will allow Google to identify the objects in a photo or understand spoken data in the voice recognition application.
Advantages:-
Disadvantages:-
import torch
dtype=torch. float
Device=torch. device(“cpu”)
N, D_in, H, D_out=64, 1000, 100, 10
x=torch.randn (N, D_in, device=device, dtype=dtype)
x=torch.randn (N, D_out, device=device, dtype=dtype)
x=torch.randn (D_in, H, device=device, type=dtype)
x=torch.randn (H, D_out, device=device, type=dtype)
learning_rate=1e-6
Fort in range(500):
H=x.mm (w1)
H_relu=h.clamp(min=0)
y_pred=h_relu.mm(w2)
loss= (y_pred-y).pow (2).sum ().item ()
print(t, loss)
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_w1=x.t ().mm (grad_h)
W1-=learning rate*grad_w1
W2-=learning rate*grad_w2
Output:-
0 47168
1 46378
…
497 3.98e-05
Theano is the python library used for the numerical calculation and is the same as the Numpy.
It will allow defining, optimizing and evaluating the mathematical expressions involved in multidimensional arrays.
It is used for the computation library used for the implementation and the network models.
The advantage of the GPU and allow to make the data calculations 100 times faster than CPU.
This library will allow for the creation of graphs for computing and provide symbolic differentiation.
Theano library is the platform-independent and also provide a unit testing platform for error detection.
Example:-
import theano
import theano.tensor as T
x=T.dmatrix (‘x’)
s=1/ (1+T.exp (-x))
logistic=theano.function ([x], s)
logistics([[0, 1], [-1,-2]])
Output:-
arrays([[0.5, 0.73], [0.2, 0.11]])
Numpy Library Disadvantages:-
Print(my_2d_array.shape)
Print(my_2d_array.data)
Print(my_2d_array.dtype)
Print(my_2d_array.strides)
Output:-
(2, 4)
Int64
(32, 8)
The matplotlib is the popular library used for the data visualization as the pandas.
This library is not related directly to ML.It comes if programmer want to visualize the pattern in data.
The library is 2D library and used for creating the graphs and plots.
Example:-
Import matplotlib.pyplot as plt
Import numpy as np
X=np.linespace (0, 10,100)
Plt.plot (x, x, label=’linear’)
Plt.legand ()
Plt.show()
Output:-