【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程(3)

本文主要是介绍【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程(3),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【CS231n】斯坦福大学李飞飞视觉识别课程笔记

由官方授权的CS231n课程笔记翻译知乎专栏——智能单元,比较详细地翻译了课程笔记,我这里就是参考和总结。

在这里插入图片描述

【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程
SciPy

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

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

图像操作

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

from scipy.misc import imread, imsave, imresize# Read an JPEG image into a numpy array
img = imread('assets/cat.jpg')
print(img.dtype, img.shape)  # Prints "uint8 (400, 248, 3)"# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (400, 248, 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 disk
imsave('assets/cat_tinted.jpg', img_tinted)

译者注:如果运行这段代码出现类似ImportError: cannot import name imread的报错,那么请利用pip进行Pillow的下载,可以解决问题。命令:pip install Pillow。
在这里插入图片描述
左边是原始图片,右边是变色和变形的图片。

————————————————————————————————————————————————————————

MATLAB文件

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

点之间的距离

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

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

import numpy as np
from 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 np
import matplotlib.pyplot as plt# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)# Plot the points using matplotlib
plt.plot(x, y)
plt.show()  # You must call plt.show() to make graphics appear.

运行上面代码会产生下面的作图:

————————————————————————————————————————————————————————
在这里插入图片描述
————————————————————————————————————————————————————————
只需要少量工作,就可以一次画不同的线,加上标签,坐标轴标志等。

import numpy as np
import matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)# Plot the points using matplotlib
plt.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 np
import matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curves
x = 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 plot
plt.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 np
from scipy.misc import imread, imresize
import matplotlib.pyplot as pltimg = imread('assets/cat.jpg')
img_tinted = img * [1, 0.95, 0.9]# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)# Show the tinted image
plt.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()

————————————————————————————————————————————————————————
在这里插入图片描述
————————————————————————————————————————————————————————

【CS231n】斯坦福大学李飞飞视觉识别课程笔记(一):Python Numpy教程(1)
【CS231n】斯坦福大学李飞飞视觉识别课程笔记(二):Python Numpy教程(2)
【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程(3)

这篇关于【CS231n】斯坦福大学李飞飞视觉识别课程笔记(三):Python Numpy教程(3)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/837659

相关文章

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Ubuntu固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

PyCharm 接入 DeepSeek最新完整教程

《PyCharm接入DeepSeek最新完整教程》文章介绍了DeepSeek-V3模型的性能提升以及如何在PyCharm中接入和使用DeepSeek进行代码开发,本文通过图文并茂的形式给大家介绍的... 目录DeepSeek-V3效果演示创建API Key在PyCharm中下载Continue插件配置Con

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创