Numpy快速入门教程(三):SciPy,Matplotlib

来源:互联网 发布:广州蛋糕店哪家好 知乎 编辑:程序博客网 时间:2024/05/21 06:19

首先声明本篇博客是本人学习CS231n的学习笔记,分享给大家当作参考。

SciPy

Numpy提供了高性能的多维数组,以及计算和操作数组的基本工具。SciPy基于Numpy,提供了大量的计算和操作数组的函数,这些函数对于不同类型的科学和工程计算非常有用。

熟悉SciPy的最好方法就是阅读文档。我们会强调对于本课程有用的部分。

图像操作

SciPy提供了一些操作图像的基本函数。比如,它提供了将图像从硬盘读入到数组的函数,也提供了将数组中数据写入的硬盘成为图像的函数。下面是一个简单的例子:

from scipy.misc import imread, imsave, imresize# Read an JPEG image into a numpy arrayimg = imread('dog.jpg')print(img.dtype, img.shape)  # We can tint the image by scaling each of the color channels# by a different scalar constant. The image has shape uint8 (960, 540, 3);# we multiply it by the array [1, 0.95, 0.9] of shape (3,);# numpy broadcasting means that this leaves the red channel unchanged,# and multiplies the green and blue channels by 0.95 and 0.9# respectively.img_tinted = img * [1, 0.95, 0.9]# Resize the tinted image to be 300 by 300 pixels.img_tinted = imresize(img_tinted, (300, 300))# Write the tinted image back to diskimsave('dog_tinted.jpg', img_tinted)

可能会报错显示没有安装相关的科学计算包,所以在此强烈推荐使用Anaconda而不是其他IDE。
Anaconda在python语言外,还集成了numpy、scipy、matplotlib等科学计算包,以及beautiful-soup、requests、lxml等网络相关包。
安装Anaconda后,基本不再需要费劲地安装其他第三方库了。

结果如图:
原图
变色变形后的图

MATLAB文件

函数scipy.io.loadmat和scipy.io.savemat能够让你读和写MATLAB文件。具体请查看文档。

点之间的距离

SciPy定义了一些有用的函数,可以计算集合中点之间的距离。

函数scipy.spatial.distance.pdist能够计算集合中所有两点之间的距离:

import numpy as npfrom scipy.spatial.distance import pdist, squareform# Create the following array where each row is a point in 2D space:# [[0 1]#  [1 0]#  [2 0]]x = np.array([[0, 1], [1, 0], [2, 0]])print(x)# Compute the Euclidean distance between all rows of x.# d[i, j] is the Euclidean distance between x[i, :] and x[j, :],# and d is the following array:# [[ 0.          1.41421356  2.23606798]#  [ 1.41421356  0.          1.        ]#  [ 2.23606798  1.          0.        ]]d = squareform(pdist(x, 'euclidean'))print(d)

具体细节请阅读文档。

函数scipy.spatial.distance.cdist可以计算不同集合中点的距离,具体请查看文档。

Matplotlib

Matplotlib是一个作图库。这里简要介绍matplotlib.pyplot模块,功能和MATLAB的作图功能类似。

绘图

matplotlib库中最重要的函数是Plot。该函数允许你做出2D图形,如下:

import numpy as npimport matplotlib.pyplot as plt# Compute the x and y coordinates for points on a sine curvex = np.arange(0, 3 * np.pi, 0.1)y = np.sin(x)# Plot the points using matplotlibplt.plot(x, y)plt.show()  # You must call plt.show() to make graphics appear.

结果如下图:
这里写图片描述
只需要少量工作,就可以一次画不同的线,加上标签,坐标轴标志等。

import numpy as npimport matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curvesx = np.arange(0, 3 * np.pi, 0.1)y_sin = np.sin(x)y_cos = np.cos(x)# Plot the points using matplotlibplt.plot(x, y_sin)plt.plot(x, y_cos)plt.xlabel('x axis label')plt.ylabel('y axis label')plt.title('Sine and Cosine')plt.legend(['Sine', 'Cosine'])plt.show()

结果图如下:
这里写图片描述

可以在文档中阅读更多关于plot的内容。

绘制多个图像

可以使用subplot函数来在一幅图中画不同的东西:

import numpy as npimport matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curvesx = np.arange(0, 3 * np.pi, 0.1)y_sin = np.sin(x)y_cos = np.cos(x)# Set up a subplot grid that has height 2 and width 1,# and set the first such subplot as active.plt.subplot(2, 1, 1)# Make the first plotplt.plot(x, y_sin)plt.title('Sine')# Set the second subplot as active, and make the second plot.plt.subplot(2, 1, 2)plt.plot(x, y_cos)plt.title('Cosine')# Show the figure.plt.show()

结果如下图:
这里写图片描述
关于subplot的更多细节,可以阅读文档。

图像

你可以使用imshow函数来显示图像,如下所示:

import numpy as npfrom scipy.misc import imread, imresizeimport matplotlib.pyplot as pltimg = imread('dog.jpg')img_tinted = img * [1, 0.95, 0.9]# Show the original imageplt.subplot(1, 2, 1)plt.imshow(img)# Show the tinted imageplt.subplot(1, 2, 2)# A slight gotcha with imshow is that it might give strange results# if presented with data that is not uint8. To work around this, we# explicitly cast the image to uint8 before displaying it.plt.imshow(np.uint8(img_tinted))plt.show()

结果如下图:
这里写图片描述

阅读全文
0 0
原创粉丝点击