deeplearning.ai 作业中的Python常用命令
1. print大法
- test = Hello World
- print ("test:" + test)
2. math和numpy的区别:math只对单个元素,numpy会broadcasting。
- import math
- import numpy as np
- x = [1, 2, 3]
- s = 1/(1+math.exp(-x) #这条语句会报错
- s = 1/(1+np.exp(-x)) #这条语句没问题。
3. 定义函数
- def sigmoid_derivative(x):
- s = 1/(1+np.exp(-x)
- ds = s*(1-s)
- return ds
- x = np.array([1, 2, 3])
- print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x))
4. shape和reshape:array的维度是从最外层[]开始数,最外层[]的元素数量就是第一个维度的大小,最内层[]的元素数量是最后一个维度的大小。array的序号是从0开始的。
reshape(x.shape[0], -1)中的-1是留这个维度给程序自己算出结果,其他维度必须指定。
- image = np.array([[[ 0.67826139, 0.29380381],
- [ 0.90714982, 0.52835647],
- [ 0.4215251 , 0.45017551]],
- [[ 0.92814219, 0.96677647],
- [ 0.85304703, 0.52351845],
- [ 0.19981397, 0.27417313]],
- [[ 0.60659855, 0.00533165],
- [ 0.10820313, 0.49978937],
- [ 0.34144279, 0.94630077]],
- [[ 0.85304703, 0.52835647],
- [ 0.10820313, 0.45017551],
- [ 0.34144279, 0.90714982]]])
- print ("image[3][2][1]: " + str(image[3][2][1])) # image[2][3][1]: 0.90714982
- print ("image.shape = " + str(image.shape)) # image.shape = (4, 3, 2)
- vector = image.reshape(image.shape[0]*image.shape[1]*image.shape[2], 1)
- print ("vector.shape = " + str(vector.shape)) # vector.shape = (24, 1)
5. Normalization: x_norm = np.linalg.norm(x, ord = 2, axis = 1, keepdims = True),其中ord=2是默认值可以不写,axis=1是对横向量归一化,对于一维向量,axis只可以为0,keepdims=True是保持array的shape,防止出现(2, )这种shape,以防万一尽量都写上keepdims=True。
- x = np.array([[0,3,4],[2,6,4]])
- x_norm = np.linalg.norm(x, ord=2, axis =1, keepdims=True)
- x_new = x/x_norm
- print ("x: " + str(x))
- print ("x_norm: "+str(x_norm))
- print ("x_new: "+str(x_new))
- 输出:
- x: [[0 3 4]
- [2 6 4]]
- x_norm: [[ 5. ]
- [ 7.48331477]]
- x_new: [[ 0. 0.6 0.8 ]
- [ 0.26726124 0.80178373 0.53452248]]
6. 求和:x_sum = np.sum(x, axis = 1, keepdims = True),其中axis=1是对第1个维度求和,比如shape是(4,2,3)的array,求和后是(4,1,3)。对于二维图像来说第1个维度就是横向量。
x_sum = np.sum(x),是把x的所有元素都加起来得到一个scalar。尽量axis和keepdims的参数都填写,避免出错。
- x = np.array([[0,3,4],[2,6,4]])
- x_sum = np.sum(x, axis = 1, keepdims = True)
- print ("x_sum: "+str(x_sum))
- 输出:
- x_sum: [[ 7]
- [12]]
7. 不同的乘法:
np.dot(x1, x2)对于矩阵是正常的矩阵乘法,对于一维向量是对应元素相乘再求和,Z = WX+b的WX部分就用np.dot。
np.multiply(x1, x2)是一维向量对应元素相乘,得到一个一维向量。
这里time是计时的方法。
- import time
- x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
- x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]
- ### 向量点乘,对应元素相乘再求和 ###
- tic = time.process_time()
- dot = np.dot(x1,x2)
- toc = time.process_time()
- print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
- ### x1和x2的转置做矩阵乘法,n*1的矩阵乘以1*n的矩阵 ###
- tic = time.process_time()
- outer = np.outer(x1,x2)
- toc = time.process_time()
- print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
- ### 对应元素相乘得到1*n的向量 ###
- tic = time.process_time()
- mul = np.multiply(x1,x2)
- toc = time.process_time()
- print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
- ### 正常的矩阵乘法 ###
- W = np.random.rand(3,len(x1)) # Random 3*len(x1) numpy array
- tic = time.process_time()
- dot = np.dot(W,x1)
- toc = time.process_time()
- print ("gdot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
- 输出:
- dot = 278
- ----- Computation time = 0.0ms
- outer = [[81 18 18 81 0 81 18 45 0 0 81 18 45 0 0]
- [18 4 4 18 0 18 4 10 0 0 18 4 10 0 0]
- [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- [63 14 14 63 0 63 14 35 0 0 63 14 35 0 0]
- [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- [81 18 18 81 0 81 18 45 0 0 81 18 45 0 0]
- [18 4 4 18 0 18 4 10 0 0 18 4 10 0 0]
- [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
- ----- Computation time = 0.0ms
- elementwise multiplication = [81 4 10 0 0 63 10 0 0 0 81 4 25 0 0]
- ----- Computation time = 0.0ms
- gdot = [ 14.98632469 18.30746169 17.30396991]
- ----- Computation time = 0.0ms
8. Broadcasting:loss = np.sum((yhat - y)**2, keepdims = True),这种**的运算,也是对每个元素计算平方。类似的+-*/也都是如此。
9. 初始化: w = np.zeros((dim, 1), dtype=float),注意这里shape是要加括号的,例如w = np.zeros((4,2))。
10. 画图:matplotlib。
- import matplotlib.pyplot as plt
- # Plot learning curve (with costs)
- costs = np.squeeze(d['costs'])
- plt.plot(costs)
- plt.ylabel('cost')
- plt.xlabel('iterations (per hundreds)')
- plt.title("Learning rate =" + str(d["learning_rate"]))
- plt.show()
画散点图:plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)。第一个参数和第二个参数分别是横轴和纵轴的坐标;参数c是指明散点颜色,一般用‘b’(蓝色)这种指明,这里使用Y这样的数值为0或者1的数组指明;参数s是散点的大小,数值越大点越大;cmap指明了颜色,这个网站列出了全部http://scipy-cookbook.readthedocs.io/items/Matplotlib_Show_colormaps.html。
11. 图像处理:scipy
改变图像尺寸:my_image = scipy.misc.imresize(image, size=(num_px,num_px))
deeplearning.ai 作业中的Python常用命令的更多相关文章
- python常用命令和基础运算符
基础运算符 http://www.cnblogs.com/alex3714/articles/5465198.html 身份运算符:is is not成员运算符:in not in ##in 判断元素 ...
- Linux 下 expect 脚本语言中交互处理常用命令
Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...
- 【视频开发】Gstreamer中一些gst-launch常用命令
GStreamer是著名的开源多媒体框架,功能强大,其命令行程序 gst-launch 可以实现很多常规测试.播放等,作为系统调试等是非常方便的. 1.摄像头测试 gst-launch v4l2src ...
- [转帖]「日常小记」linux中强大且常用命令:find、grep
「日常小记」linux中强大且常用命令:find.grep https://zhuanlan.zhihu.com/p/74379265 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍 ...
- 图解git中的最常用命令
图解git中的最常用命令 Git命令参考手册(文本版) git init # 初始化本地git仓库(创 ...
- 用纯Python实现循环神经网络RNN向前传播过程(吴恩达DeepLearning.ai作业)
Google TensorFlow程序员点赞的文章! 前言 目录: - 向量表示以及它的维度 - rnn cell - rnn 向前传播 重点关注: - 如何把数据向量化的,它们的维度是怎么来的 ...
- 【日常小记】linux中强大且常用命令:find、grep【转】
转自:http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍fin ...
- Linux 中强大且常用命令:find、grep
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 f ...
- 【转载】Linux中强大且常用命令:find、grep
转载自:http://www.linuxeden.com/html/softuse/20130804/142065.html 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find. ...
随机推荐
- 为什么在有的服务器上禅道、蝉知安装会报错? 之理解MySQL的SQL_MODE
最近用蝉知的CMS 建站比较多,感觉蛮顺手的,但在给客户安装的时候却会出现安装报错,其原因也很简单 查看了一下他们的install.sql文件中,有些时间字段的默认值是0000-00-00 00:00 ...
- Base64转换二进制文件对象 Blob/Base64转换 File 对象
function convertBase64UrlToBlob(urlData) { var arr = dataurl.split(','),//去掉url的头,并转换为byte type = ar ...
- 初探XRebel
一.什么是XRebel? 1.介绍 XRebel 是不间断运行在 web 应用的交互式分析器.可以看到网页上的每一个操作在前端以及服务端.数据库.网络传输都花费多少时间,当发现问题会在浏览器中显示警告 ...
- 在windows 10下安装python
windows系统默认状态下是没有安装python的,我们需要下载并安装它. 首先检查是否安装了python 在"开始"菜单中输入cmd,然后右击选择管理员身份运行,这样就打开了一 ...
- bzoj 4012: [HNOI2015]开店
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
- 模板引擎(smarty)知识点总结三
阔别了几个月,小杨又来分享php知识.话不多说,言归正传,今天继续带来smarty的知识点. -----------------smarty assign append 详解 对于这两个的区别和联系 ...
- mybatis源码分析(一)
mybatis源码分析(sqlSessionFactory生成过程) 1. mybatis框架在现在各个IT公司的使用不用多说,这几天看了mybatis的一些源码,赶紧做个笔记. 2. 看源码从一个d ...
- hiberation4 获取session
T t; Configuration cfg = new Configuration(); cfg.configure(); ServiceRegistry serviceRegistry = new ...
- ASP.NET Core学习之三 NLog日志
上一篇简单介绍了日志的使用方法,也仅仅是用来做下学习,更何况只能在console输出. NLog已是日志库的一员大佬,使用也简单方便,本文介绍的环境是居于.NET CORE 2.0 ,目前的版本也只有 ...
- golang 栈操作
Monk's Love for Food Our monk loves food. Hence,he took up position of a manager at Sagar,a restau ...