A Comprehensive Overview of Common Python Libraries for Artificial Intelligence
This article provides a concise yet comprehensive introduction to popular Python libraries for artificial intelligence, including NumPy, OpenCV, scikit-image, Pillow, SimpleCV, Mahotas, Ilastik, scikit-learn, SciPy, NLTK, spaCy, LibROSA, Pandas, Matplotlib, Seaborn, Orange, PyBrain, Milk, TensorFlow, PyTorch, Theano, Keras, Caffe, MXNet, PaddlePaddle, and CNTK, and demonstrates their basic usage with code examples.
To help readers gain a preliminary understanding of commonly used Python libraries for artificial intelligence, this article briefly introduces each library and provides simple code examples.
1. NumPy – NumPy (Numerical Python) is an extension library that supports large‑dimensional arrays and matrix operations, offering many mathematical functions. Because its core is written in C and stores objects directly, it runs much faster than pure Python. The example compares the execution time of computing sine values with pure Python loops versus NumPy vectorized operations.
import numpy as np
import math
import random
import time
start = time.time()
for i in range(10):
list_1 = list(range(1,10000))
for j in range(len(list_1)):
list_1[j] = math.sin(list_1[j])
print("使用纯Python用时{}s".format(time.time()-start))
start = time.time()
for i in range(10):
list_1 = np.array(np.arange(1,10000))
list_1 = np.sin(list_1)
print("使用Numpy用时{}s".format(time.time()-start))Result shows NumPy is significantly faster (≈0.0016 s vs. ≈0.0174 s).
2. OpenCV – OpenCV is a cross‑platform computer‑vision library written in C/C++ with Python bindings. It provides efficient image‑processing functions such as filtering and Gaussian blur. The sample code reads an image, applies averaging, Gaussian, and bilateral filters, and displays the results with Matplotlib.
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('h89817032p0.png')
kernel = np.ones((5,5),np.float32)/25
dst = cv.filter2D(img,-1,kernel)
blur_1 = cv.GaussianBlur(img,(5,5),0)
blur_2 = cv.bilateralFilter(img,9,75,75)
plt.figure(figsize=(10,10))
plt.subplot(221),plt.imshow(img[:,:,::-1]),plt.title('Original')
plt.subplot(222),plt.imshow(dst[:,:,::-1]),plt.title('Averaging')
plt.subplot(223),plt.imshow(blur_1[:,:,::-1]),plt.title('Gaussian')
plt.subplot(224),plt.imshow(blur_2[:,:,::-1]),plt.title('Bilateral')
plt.show()3. scikit‑image – Built on SciPy, scikit‑image treats images as NumPy arrays and offers functions such as rescale, resize, and downscale_local_mean for geometric transformations.
from skimage import data, color, io
from skimage.transform import rescale, resize, downscale_local_mean
image = color.rgb2gray(io.imread('h89817032p0.png'))
image_rescaled = rescale(image, 0.25, anti_aliasing=False)
image_resized = resize(image, (image.shape[0]//4, image.shape[1]//4), anti_aliasing=True)
image_downscaled = downscale_local_mean(image, (4, 3))
plt.figure(figsize=(20,20))
plt.subplot(221),plt.imshow(image, cmap='gray'),plt.title('Original')
plt.subplot(222),plt.imshow(image_rescaled, cmap='gray'),plt.title('Rescaled')
plt.subplot(223),plt.imshow(image_resized, cmap='gray'),plt.title('Resized')
plt.subplot(224),plt.imshow(image_downscaled, cmap='gray'),plt.title('Downscaled')
plt.show()4. Pillow (PIL) – Pillow is the actively maintained fork of the original Python Imaging Library. It provides a simple API for opening, manipulating, and saving many image formats.
5. SimpleCV – SimpleCV is an open‑source framework that wraps OpenCV and other vision libraries, allowing rapid prototyping without deep knowledge of image formats or matrix operations. The example loads an image from a URL, detects keypoints, draws them, and saves the result.
from SimpleCV import Image, Color, Display
img = Image('http://i.imgur.com/lfAeZ4n.png')
feats = img.findKeypoints()
feats.draw(color=Color.RED)
img.show()
output = img.applyLayers()
output.save('juniperfeats.png')Running under Python 3 may raise a SyntaxError due to outdated print statements, so SimpleCV is not recommended for Python 3 projects.
6. Mahotas – Mahotas is a fast computer‑vision library built on NumPy, offering over 100 image‑processing functions. The sample loads the classic “Lena” image, computes the fraction of zero pixels, and displays the image.
import numpy as np
import mahotas
import mahotas.demos
from mahotas.thresholding import soft_threshold
from matplotlib import pyplot as plt
f = mahotas.demos.load('lena', as_grey=True)
f = f[128:,128:]
print("Fraction of zeros in original image: {0}".format(np.mean(f==0)))
plt.imshow(f, cmap='gray')
plt.show()7. Ilastik – Ilastik provides user‑friendly, interactive machine‑learning tools for bio‑image analysis such as segmentation, classification, and tracking, requiring no deep ML expertise.
8. scikit‑learn – A free machine‑learning library for Python offering classification, regression, clustering, and dimensionality‑reduction algorithms. The example demonstrates K‑Means clustering on synthetic data.
import time, numpy as np, matplotlib.pyplot as plt
from sklearn.cluster import MiniBatchKMeans, KMeans
from sklearn.metrics.pairwise import pairwise_distances_argmin
from sklearn.datasets import make_blobs
np.random.seed(0)
centers = [[1,1],[-1,-1],[1,-1]]
X, _ = make_blobs(n_samples=3000, centers=centers, cluster_std=0.7)
k_means = KMeans(init='k-means++', n_clusters=3, n_init=10)
k_means.fit(X)
mbk = MiniBatchKMeans(init='k-means++', n_clusters=3, batch_size=45, n_init=10)
mbk.fit(X)
# Plotting omitted for brevity9. SciPy – SciPy builds on NumPy to provide efficient numerical routines such as integration, interpolation, optimization, and special functions (e.g., Bessel, gamma, hypergeometric). The example visualizes a drum‑head surface using special functions.
from scipy import special
import numpy as np, matplotlib.pyplot as plt
theta = np.r_[0:2*np.pi:50j]
radius = np.r_[0:1:50j]
x = np.array([r*np.cos(theta) for r in radius])
y = np.array([r*np.sin(theta) for r in radius])
z = np.cos(0.5) * np.cos(1*theta) * special.jn(1, radius*special.jn_zeros(1,1)[-1])
fig = plt.figure()
ax = fig.add_axes([0,0.05,0.95,0.95], projection='3d')
ax.plot_surface(x, y, z, cmap='RdBu_r')
plt.show()10. NLTK – The Natural Language Toolkit supplies interfaces to over 50 corpora and lexical resources, plus tools for tokenization, tagging, parsing, and semantic reasoning. The snippet tokenizes a sentence, tags parts of speech, and extracts named entities.
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('maxent_ne_chunker')
nltk.download('words')
sentence = "At eight o'clock on Thursday morning Arthur didn't feel very good."
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
print(entities)11. spaCy – spaCy is a high‑performance NLP library for industrial‑strength text processing, offering tokenization, part‑of‑speech tagging, named‑entity recognition, and easy integration with deep‑learning pipelines.
import spacy
nlp = spacy.load('en_core_web_sm')
texts = ["Net income was $9.4 million compared to the prior year of $2.7 million.",
"Revenue exceeded twelve billion dollars, with a loss of $1b."]
for doc in nlp.pipe(texts, disable=["tok2vec","tagger","parser","attribute_ruler","lemmatizer"]):
print([(ent.text, ent.label_) for ent in doc.ents])12. LibROSA – LibROSA is a Python library for music and audio analysis, providing functions for loading audio, beat tracking, and feature extraction.
import librosa
filename = librosa.example('nutcracker')
y, sr = librosa.load(filename)
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
print('Estimated tempo: {:.2f} BPM'.format(tempo))13. Pandas – Pandas offers fast, powerful, flexible data structures for data analysis and manipulation, supporting CSV, JSON, SQL, Excel, and many operations such as merging, reshaping, and time‑series handling.
import pandas as pd, numpy as np, matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)).cumsum()
df = pd.DataFrame(np.random.randn(1000,4), index=ts.index, columns=list('ABCD')).cumsum()
df.plot()
plt.show()14. Matplotlib – Matplotlib is a comprehensive 2‑D plotting library that mimics MATLAB’s API, enabling publication‑quality figures.
import numpy as np, matplotlib.pyplot as plt
x = np.linspace(0.1, 2*np.pi, 100)
plt.plot(x, x, label='y=x')
plt.plot(x, np.square(x), label='y=x^2')
plt.plot(x, np.log(x), label='y=log(x)')
plt.plot(x, np.sin(x), label='y=sin(x)')
plt.legend()
plt.show()15. Seaborn – Built on Matplotlib, Seaborn provides a higher‑level interface for statistical graphics, making it easy to create attractive visualizations such as pair plots.
import seaborn as sns, matplotlib.pyplot as plt
penguins = sns.load_dataset('penguins')
sns.pairplot(penguins, hue='species')
plt.show()16. Orange – Orange is an open‑source data‑mining and machine‑learning suite with a visual programming front‑end, suitable for rapid prototyping and teaching.
# Install via pip
pip install orange3
# Launch GUI
orange-canvas17. PyBrain – PyBrain is a modular machine‑learning library offering flexible neural‑network components. The example builds a simple multi‑layer perceptron.
from pybrain.structure import FeedForwardNetwork, LinearLayer, SigmoidLayer, FullConnection
n = FeedForwardNetwork()
inLayer = LinearLayer(2)
hiddenLayer = SigmoidLayer(3)
outLayer = LinearLayer(1)
n.addInputModule(inLayer)
n.addModule(hiddenLayer)
n.addOutputModule(outLayer)
n.addConnection(FullConnection(inLayer, hiddenLayer))
n.addConnection(FullConnection(hiddenLayer, outLayer))
n.sortModules()18. Milk – Milk (Machine Learning Toolkit) provides a collection of classifiers (SVM, k‑NN, random forest, decision trees) and feature‑selection tools, supporting both supervised and unsupervised learning.
import numpy as np, milk
features = np.random.rand(100,10)
labels = np.zeros(100)
features[50:] += .5
labels[50:] = 1
learner = milk.defaultclassifier()
model = learner.train(features, labels)
print(model.apply(np.random.rand(10)))
print(model.apply(np.random.rand(10)+.5))19. TensorFlow – TensorFlow is an end‑to‑end open‑source platform for machine learning. The snippet builds a simple CNN for CIFAR‑10 using the TensorFlow 2.x Keras API.
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images/255.0, test_images/255.0
model = models.Sequential([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10)
])
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))20. PyTorch – PyTorch offers dynamic computation graphs and a Pythonic interface for building neural networks. The example defines a fully connected network for MNIST digit classification.
import torch
from torch import nn
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512), nn.ReLU(),
nn.Linear(512, 512), nn.ReLU(),
nn.Linear(512, 10), nn.ReLU()
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = NeuralNetwork().to('cuda' if torch.cuda.is_available() else 'cpu')21. Theano – Theano allows definition, optimization, and efficient evaluation of mathematical expressions involving multi‑dimensional arrays. The snippet computes the Jacobian of a vector‑valued function.
import theano
import theano.tensor as T
x = T.dvector('x')
y = x ** 2
J, updates = theano.scan(lambda i, y, x: T.grad(y[i], x), sequences=T.arange(y.shape[0]), non_sequences=[y, x])
f = theano.function([x], J, updates=updates)
print(f([4, 4]))22. Keras – Keras is a high‑level neural‑network API that runs on top of TensorFlow, CNTK, or Theano, focusing on rapid prototyping.
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32)23. Caffe – Caffe (now part of PyTorch) is a deep‑learning framework originally designed for speed and modularity.
24. MXNet – MXNet is a flexible deep‑learning framework supporting both symbolic and imperative programming. The example builds a CNN for handwritten digit recognition.
import mxnet as mx
from mxnet import gluon, autograd as ag, nd
from mxnet.gluon import nn
mnist = mx.test_utils.get_mnist()
batch_size = 100
train_data = mx.io.NDArrayIter(mnist['train_data'], mnist['train_label'], batch_size, shuffle=True)
val_data = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'], batch_size)
class Net(gluon.Block):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
self.conv1 = nn.Conv2D(20, kernel_size=5)
self.pool1 = nn.MaxPool2D(pool_size=2, strides=2)
self.conv2 = nn.Conv2D(50, kernel_size=5)
self.pool2 = nn.MaxPool2D(pool_size=2, strides=2)
self.fc1 = nn.Dense(500)
self.fc2 = nn.Dense(10)
def forward(self, x):
x = self.pool1(nd.tanh(self.conv1(x)))
x = self.pool2(nd.tanh(self.conv2(x)))
x = x.reshape((0, -1))
x = nd.tanh(self.fc1(x))
x = nd.tanh(self.fc2(x))
return x
net = Net()
ctx = [mx.gpu() if mx.test_utils.list_gpus() else mx.cpu()]
net.initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.03})
metric = mx.metric.Accuracy()
loss = gluon.loss.SoftmaxCrossEntropyLoss()
for epoch in range(10):
train_data.reset()
for batch in train_data:
data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0)
label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0)
with ag.record():
outputs = [net(X) for X, y in zip(data, label)]
losses = [loss(y_hat, y) for y_hat, y in zip(outputs, label)]
for l in losses:
l.backward()
trainer.step(batch.data[0].shape[0])
metric.update(label, outputs)
name, acc = metric.get()
metric.reset()
print('epoch %d: %s = %f' % (epoch, name, acc))25. PaddlePaddle – PaddlePaddle (PArallel Distributed Deep LEarning) is Baidu’s open‑source deep‑learning platform. The snippet implements LeNet‑5 for digit classification.
import paddle
import paddle.nn.functional as F
from paddle.nn import Conv2D, MaxPool2D, Linear
class LeNet(paddle.nn.Layer):
def __init__(self, num_classes=10):
super(LeNet, self).__init__()
self.conv1 = Conv2D(1, 6, 5)
self.pool1 = MaxPool2D(2, 2)
self.conv2 = Conv2D(6, 16, 5)
self.pool2 = MaxPool2D(2, 2)
self.fc1 = Linear(120, 64)
self.fc2 = Linear(64, num_classes)
def forward(self, x):
x = F.sigmoid(self.conv1(x))
x = self.pool1(x)
x = F.sigmoid(self.conv2(x))
x = self.pool2(x)
x = paddle.reshape(x, [x.shape[0], -1])
x = F.sigmoid(self.fc1(x))
x = self.fc2(x)
return x26. CNTK – Microsoft Cognitive Toolkit (CNTK) describes neural networks as directed graphs, allowing concise definition of layers, parameters, and loss functions.
# Example NDL description (simplified)
NDLNetworkBuilder = [
run = ndlLR,
ndlLR = [
SDim = $dimension$, LDim = 1,
features = Input(SDim, 1),
labels = Input(LDim, 1),
B0 = Parameter(4), W0 = Parameter(4, SDim),
B = Parameter(LDim), W = Parameter(LDim, 4),
t0 = Times(W0, features), z0 = Plus(t0, B0), s0 = Sigmoid(z0),
t = Times(W, s0), z = Plus(t, B), s = Sigmoid(z),
LR = Logistic(labels, s), EP = SquareError(labels, s)
]
]This extensive list equips readers with a solid starting point for selecting and applying Python AI libraries in research or development projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
