CS231n Convolutional Neural Networks for Visual Recognition------Scipy and MatplotlibTutorial

源链接为:http://cs231n.github.io/python-numpy-tutorial/。

这篇指导书是由Justin Johnson编写的。

在这门课程中我们将使用Python语言完成所有变成任务!Python本身就是一种很棒的通用编程语言,但是在一些流行的库帮助下(numpy,scipy,matplotlib)它已经成为科学计算的强大环境。
我们希望你们中的许多人都有一些Python和numpy的使用经验; 对你们其他人来说,这个section将作为Python用于科学计算和使用的快速速成课程。
你们中的一些人可能已经掌握了Matlab的知识,在这种情况下我们也推荐使用numpy。

你也可以阅读由Volodymyr Kuleshov和Isaac Caswell(CS 228)编写的Notebook版笔记。

本教程使用的Python版本为Python3.


目录

Scipy

Image operations

MATLAB files

Distance between points

Matplotlib

Plotting

Subplots

Images


原文共分为4部分,分别介绍了Python、Numpy、Scipy和Matplotlib的使用。本次翻译为最后两个部分:Scipy和Matplotlib的使用指导!

Scipy

Numpy提供了一个高性能的多维数组以及计算和操作这些数组的基本工具。Scipy(官方链接)以此为基础,提供大量在numpy数组上运行的函数,适用于不同类型的科学和工程应用。熟悉Scipy最好的方法就是浏览官方文档。我们将重点介绍一些对你可能有用的部分。

Image operations

Scipy提供了一些处理图像的基本函数。例如,它有将图像从磁盘读取到numpy数组,将numpy数组作为图像写入磁盘以及调整图像大小的功能。 这是一个展示这些功能的简单示例:

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)

   

Left: The original image. Right: The tinted and resized image.


MATLAB files

scipy.io.loadmatscipy.io.savemat函数允许你读取和写入MATLAB文件。你可以看这篇文档了解更多。

Distance between points

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里的画图系统相似。

Plotting

在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的信息。

Subplots

您可以使用子图功能在同一图中绘制不同的东西。 这是一个例子:

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的信息。

Images

你可以使用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()

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439961.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自签名CA认证

自签名CA认证 用openssl命令生成自己的根证书,让用户安装信任它,之后所有用这个根证书签名的证书,就可以被信任。 生成根证书 创建文件并配置环境mkdir /root/ca cd /root/ca mkdir certs crl newcerts private chmod 700 private touch i…

注解由谁读取并解析的?

问题: 注解由谁读取并解析的?描述: java web 开发,使用ssh框架。在dao层,定义的类的头上有Component("GzDAO")注解,在service层, 定义的类的头上有Service(value "GzService")&…

Python之Numpy入门实战教程(1):基础篇

Numpy、Pandas、Matplotlib是Python的三个重要科学计算库,今天整理了Numpy的入门实战教程。NumPy是使用Python进行科学计算的基础库。 NumPy以强大的N维数组对象为中心,它还包含有用的线性代数,傅里叶变换和随机数函数。 强烈建议大家将本文中…

Go初识与问题

变量&常量 变量 命名 由字母、数字、下划线组成,首个字符不能是数字关键字、保留字不能作为变量名变量名字区分大小写驼峰命名声明 1. var : 全局变量var 变量名称 类型var 变量名称1,变量名称2 类型 (同一种类型)var (变量名称1 类型1变量名称2 类型…

【HDU - 4597】Play Game(博弈dp)

题干: Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his …

1.3)深度学习笔记------浅层神经网络

目录 1)Neural Network Overview 2)Neural Network Representation 3)Computing a Neural Network’s Output(重点) 4)Vectorizing across multiple examples 5)Activation functions 6&a…

git 实战

配置ssh git config --global user.name "用户名" git config --global user.email "邮箱"ssh-keygen -t rsa -C "邮箱"需要进行确认:1. 确认秘钥的保存路径(不需要改直接回车)2. 如果上一步置顶的保存路径下已经有秘钥文件&…

SpringMVC 的执行流程

SpringMVC 的执行流程 1)用户向服务器发送请求,请求被 Spring 前端控制 Servelt DispatcherServlet捕获; 2)DispatcherServlet 对请求 URL 进行解析,得到请求资源标识符(URI)。然后根据该 URI&…

【POJ - 2987】Firing(最大权闭合图,网络流最小割,输出方案最小,放大权值法tricks)

题干: You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed …

kafka初识

kafka中文文档 本文环境:ubuntu:18.04 kafka安装、配置与基本使用(单节点) 安装kafka 下载 0.10.0.1版本并解压缩 > tar -xzf kafka_2.11-0.10.0.1.tgz > cd kafka_2.11-0.10.0.1.tgzkafka简单配置 > vi config/server.properties主要注意三个地方&a…

1.4)深度学习笔记------深层神经网络

目录 1)Deep L-layer neural network 2)Forward Propagation in a Deep Network(重点) 3)Getting your matrix dimensions right 4)Building blocks of deep neural networks 5)Forward and Backward Propagation…

Struts1工作原理

Struts1工作原理图 1、初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(struts-config.xml)的配置信息,为struts中不同的模块初始化相应的对象。(面…

【洛谷 - P1772 】[ZJOI2006]物流运输(dp)

题干: 题目描述 物流公司要把一批货物从码头A运到码头B。由于货物量比较大,需要n天才能运完。货物运输过程中一般要转停好几个码头。物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪。由于各种因素的存在&am…

RabbitMQ初识

官方介绍 - 中文 本文环境:ubuntu:20.04 RabbitMQ安装、配置与基本使用 安装RabbitMQ # 简易脚本安装 curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.deb.sh | sudo bash sudo apt-get install rabbitmq-server -y --f…

Apollo进阶课程 ⑦ | 高精地图的采集与生产

目录 1.高精地图采集过程中需要用到的传感器 1.1)GPS 1.2)IMU 1.3)轮速计 2.高精地图采集过程中的制图方案 2.1)方案一 激光雷达 2.2)Camera融合激光雷达 原文链接:Apollo进阶课程 ⑦ | 高精地图的采…

【BZOJ 3831】【Poi2014】Little Bird(单调队列优化dp)

题干: Description In the Byteotian Line Forest there are trees in a row. On top of the first one, there is a little bird who would like to fly over to the top of the last tree. Being in fact very little, the bird might lack the strength to f…

你看不懂的spring原理是因为不知道这几个概念

背景 问题从一杯咖啡开始。 今天我去楼下咖啡机买了一杯「粉黛拿铁」。制作过程中显示: 我取了做好的粉黛拿铁,喝了一口,果然就是一杯热巧克力。咦咦咦,说好的拿铁呢?虽然我对「零点吧」的咖啡评价很高,觉…

EasyOcr 安装(linux、docker)、使用(gin、python)

EasyOcr git地址 EasyOCR是一款用python语言编写的OCR第三方库,同时支持GPU和CPU,目前已经支持超过70种语言. 安装(CPU) 注意: 本文是在仅在cpu下使用。如要使用CUDA版本,请在pytorch网站上选择正确的,并关闭此文章。…

Python之Numpy入门实战教程(2):进阶篇之线性代数

Numpy、Pandas、Matplotlib是Python的三个重要科学计算库,今天整理了Numpy的入门实战教程。NumPy是使用Python进行科学计算的基础库。 NumPy以强大的N维数组对象为中心,它还包含有用的线性代数,傅里叶变换和随机数函数。 本文主要介绍Numpy库…

【牛客 - 369F】小D的剑阵(最小割建图,二元关系建图,网络流最小割)

题干: 链接:https://ac.nowcoder.com/acm/contest/369/F 来源:牛客网 题目描述 现在你有 n 把灵剑,其中选择第i把灵剑会得到的 wiw_iwi​ 攻击力。 于此同时,还有q个约束,每个约束形如: …