Home > Technology peripherals > AI > body text

The 9 most commonly used Python libraries for machine learning

王林
Release: 2024-01-22 21:00:16
forward
817 people have browsed it

The 9 most commonly used Python libraries for machine learning

机器学习是一种编程技术,让计算机能够从多种数据中自动学习。过去,开发人员需要手动编写算法来进行机器学习任务。而现在,使用各种Python库,我们能够更高效地完成机器学习任务。这种技术的发展使得机器学习变得更加普遍和易于实施。

机器学习中使用的Python库

Numpy

NumPy是一个强大的Python库,专门用于处理大型多维数组和矩阵。它提供了丰富的高级数学函数,非常适用于机器学习中的基础计算。其中包括线性代数、傅里叶变换和随机数功能等等。

相应代码

import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[5,6],[7,8]])
v=np.array([9,10])
w=np.array([11,12])
print(np.dot(v,w),"\n")
print(np.dot(x,v),"\n")
print(np.dot(x,y))
Copy after login

Scipy

SciPy在机器学习爱好者中非常受欢迎,因为它包含优化、线性代数、集成和统计等模块,SciPy对于图像处理也非常有用。

SciPy库和SciPy堆栈之间存在差异,SciPy是构成SciPy堆栈的核心包之一。

from scipy.misc import imread,imsave,imresize
img=imread('D:/Programs/cat.jpg')#path of the image
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)
Copy after login

Scikit-learn

Scikit-learn是用于经典ML算法库之一。它是基于NumPy和SciPy库建立的。Scikit-learn支持大多数有监督和无监督学习算法。Scikit-learn还可用于数据挖掘和数据分析。

from sklearn import datasets
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
dataset=datasets.load_iris()
model=DecisionTreeClassifier()
model.fit(dataset.data,dataset.target)
print(model)
expected=dataset.target
predicted=model.predict(dataset.data)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
Copy after login

Theano

机器学习基本上是数学和统计学。Theano是一个流行的Python库,以有效的方式定义、评估和优化涉及多维数组的数学表达式。它是通过优化CPU和GPU的利用率来实现的。它广泛用于单元测试和自我验证,以检测和诊断不同类型的错误。

Theano是一个非常强大的库,无论是大规模计算密集型科学项目,还是个人都能被应用到项目中。

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]])
Copy after login

TensorFlow

TensorFlow是一个非常流行的高性能数值计算开源库,由谷歌的Google Brain团队开发。顾名思义,Tensorflow是一个涉及定义和运行涉及张量的计算的框架。它可以训练和运行可用于开发多个AI应用程序的深度神经网络。TensorFlow广泛应用于深度学习研究和应用领域。

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()
Copy after login

Keras

Keras是一个非常流行的Python机器学习库。提供了许多用于探索、组合和过滤数据的内置方法。能够在TensorFlow、CNTK或Theano之上运行。它可以在CPU和GPU上无缝运行。

Keras的优点之一是它可以轻松快速地进行原型设计。Keras让ML初学者真正能够构建和设计神经网络。

PyTorch

PyTorch是一个流行的基于Torch的Python开源机器学习库,用C语言实现,并在Lua中封装。它有很多工具和库可供选择,支持计算机视觉、自然语言处理(NLP)和更多ML程序。它允许开发人员使用GPU加速对张量执行计算,还有助于创建计算图。

import torch
dtype=torch.float
device=torch.device("cpu")
N,D_in,H,D_out=64,1000,100,10
x=torch.random(N,D_in,device=device,dtype=dtype)
y=torch.random(N,D_out,device=device,dtype=dtype)
w1=torch.random(D_in,H,device=device,dtype=dtype)
w2=torch.random(H,D_out,device=device,dtype=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
Copy after login

Pandas

Pandas是一个流行的Python数据分析库。它与机器学习没有直接关系。众所周知,数据集必须在训练前准备好。在这种情况下,Pandas就派上用场了,因为它是专门为数据提取和准备而开发的。它为数据分析提供了高级数据结构和各种工具。它提供了许多用于分组、组合和过滤数据的内置方法。

import pandas as pd
data={"country":["Brazil","Russia","India","China","South Africa"],
"capital":["Brasilia","Moscow","New Delhi","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)
Copy after login

Matplotlib

Matplotlib是一个非常流行的用于数据可视化的Python库。和Pandas一样,它与机器学习没有直接关系。

但它在可视化方面非常有用,作为2D绘图库,可以创建2D图形和绘图,提供了控制线条样式、字体属性、格式化轴等的功能。

用于数据可视化的各种图形和绘图,即直方图、错误图表、条形聊天等均可以通过Matplotlib实现。

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,10,100)
plt.plot(x,x,label=&#x27;linear&#x27;)
plt.legend()
plt.show()
Copy after login

The above is the detailed content of The 9 most commonly used Python libraries for machine learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:163.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!