首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

机器学习的最佳Python库

顾名思义,机器学习是一门对计算机进行编程的科学,通过编程,它们能够从不同类型的数据中学习。阿瑟·塞缪尔(Arthur Samuel)给出了一个更普遍的定义——“机器学习是一个研究领域,它使计算机能够在没有明确编程的情况下学习。” 它们通常用于解决各种类型的生活问题。

在过去,人们习惯于通过手动编码所有算法以及数学和统计公式来执行机器学习任务。这使得处理变得耗时、乏味和低效。但在现代,与过去使用各种Python库、框架和模块相比,它变得非常简单和高效。如今,Python是最受欢迎的编程语言之一,它已经取代了行业中的许多语言,其中一个原因是它拥有大量的库。机器学习中使用的Python库有:

Numpy

Scipy

Scikit-learn

Theano

TensorFlow

Keras

PyTorch

Pandas

Matplotlib

Numpy

NumPy是一个非常流行的Python库,用于大型多维数组和矩阵处理,借助大量高级数学函数。它对于机器学习中的基础科学计算非常有用。它对于线性代数、傅立叶变换和随机数功能特别有用。像TensorFlow这样的高端库在内部使用NumPy来操作张量。例:

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

import numpy as np

# Creating two arrays of rank 2 x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]])

# Creating two arrays of rank 1 v = np.array([9, 10]) w = np.array([11, 12])

# Inner product of vectors print(np.dot(v, w), "\n")

# Matrix and Vector product print(np.dot(x, v), "\n")

# Matrix and matrix product print(np.dot(x, y))

输出:219[29 67][[19 22] [43 50]]

SciPy

SciPy是一个在机器学习爱好者中非常受欢迎的库,因为它包含用于优化、线性代数、集成和统计的不同模块。SciPy库和SciPy堆栈之间存在差异。SciPy是组成SciPy堆栈的核心包之一。SciPy对于图像处理也非常有用。例:

# Python script using Scipy # for image manipulation

from scipy.misc import imread, imsave, imresize

# Read a JPEG image into a numpy array img = imread('~/ImageLib/cat.jpg') # path of the image print(img.dtype, img.shape)

# Tinting the image img_tint = img * [1, 0.45, 0.3]

# Saving the tinted image imsave('~/ImageLib/cat_tinted.jpg', img_tint)

# Resizing the tinted image to be 300 x 300 pixels img_tint_resize = imresize(img_tint, (300, 300))

# Saving the resized tinted image imsave('~/ImageLib/cat_tinted_resized.jpg', img_tint_resize)

注:如果scipy.misc import imread,imsave,imresize在您的操作系统上不起作用,请尝试使用下面的代码继续执行上面的代码

!pip install imageioimport imageiofrom imageio import imread, imsave

Scikit-Learn

Scikit-Learn是经典ML算法中最流行的ML库之一。它构建在两个基本的Python库之上,即NumPy和SciPy。Scikit-Learn支持大多数监督和非监督学习算法。Scikit-Learn还可以用于数据挖掘和数据分析,这使它成为开始使用ML的人的一个很好的工具。例:

# Python script using Scikit-learn # for Decision Tree Classifier

# Sample Decision Tree Classifier from sklearn import datasets from sklearn import metrics from sklearn.tree import DecisionTreeClassifier

# load the iris datasets dataset = datasets.load_iris()

# fit a 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 the fit of the model print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted))

输出: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_leaf=1, min_samples_split=2, min_weight_fraction_leaf=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 150weighted avg 1.00 1.00 1.00 150

[[50 0 0] [ 0 50 0] [ 0 0 50]]

Theano

我们都知道机器学习基本上是数学和统计学。Theano是一个流行的Python库,用于以高效的方式定义、计算和优化涉及多维数组的数学表达式。它通过优化CPU和GPU的利用率来实现。它广泛用于单元测试和自我验证,以检测和诊断不同类型的错误。Theano是一个非常强大的库,已经在大型计算密集型科学项目中使用了很长一段时间,但它足够简单和平易近人,可供个人用于自己的项目。例:

# Python program using Theano # for computing a Logistic # 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]])

输出:array([[0.5, 0.73105858], [0.26894142, 0.11920292]])

TensorFlow

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

# Python program using TensorFlow # for multiplying 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 the Session sess = tf.Session() # Print the result print(sess.run(result)) # Close the session sess.close() 输出:[5 12 21 32]

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OO8CAgUemZge5BmqhHCUkLrQ0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券