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常用命令的更多相关文章

  1. python常用命令和基础运算符

    基础运算符 http://www.cnblogs.com/alex3714/articles/5465198.html 身份运算符:is is not成员运算符:in not in ##in 判断元素 ...

  2. Linux 下 expect 脚本语言中交互处理常用命令

    Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...

  3. 【视频开发】Gstreamer中一些gst-launch常用命令

    GStreamer是著名的开源多媒体框架,功能强大,其命令行程序 gst-launch 可以实现很多常规测试.播放等,作为系统调试等是非常方便的. 1.摄像头测试 gst-launch v4l2src ...

  4. [转帖]「日常小记」linux中强大且常用命令:find、grep

    「日常小记」linux中强大且常用命令:find.grep https://zhuanlan.zhihu.com/p/74379265 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍 ...

  5. 图解git中的最常用命令

    图解git中的最常用命令 Git命令参考手册(文本版) git init                                                  # 初始化本地git仓库(创 ...

  6. 用纯Python实现循环神经网络RNN向前传播过程(吴恩达DeepLearning.ai作业)

    Google TensorFlow程序员点赞的文章!   前言 目录: - 向量表示以及它的维度 - rnn cell - rnn 向前传播 重点关注: - 如何把数据向量化的,它们的维度是怎么来的 ...

  7. 【日常小记】linux中强大且常用命令:find、grep【转】

    转自:http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍fin ...

  8. Linux 中强大且常用命令:find、grep

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下:    find命令        f ...

  9. 【转载】Linux中强大且常用命令:find、grep

    转载自:http://www.linuxeden.com/html/softuse/20130804/142065.html 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find. ...

随机推荐

  1. iOS UICollectionView(转三)

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  2. iOS tableViewCell 在自定义高度方法中遇到的问题,cell高度为0,cell显示不出来,cell直接显示第几个而不是...cell显示个数不对

    遇到以上问题可以看看你的cell高度中是否有,自定的高度,有了继续看,没有了继续百度... 在文字排版中,少不了自适应文字高度,行间距什么的:显然cell的高度时不固定的,如果复用自定义的cell的话 ...

  3. [C/C++语言标准] ISO C99/ ISO C11/ ISO C++11/ ISO C++14 Downloads

    语言法典,C/C++社区人手一份,技术讨(hu)论(peng)必备 ISO IEC C99 https://files.cnblogs.com/files/racaljk/ISO_C99.pdf IS ...

  4. 探索C++对象模型

    只说C++对象模型在内存中如何分配这是不现实的,所以这里选择VS 2013作为调试环境具体探讨object在内存中分配情况.目录给出了具体要探讨的所有模型,正文分标题依次讨论.水平有限,如有错误之处请 ...

  5. Pytorch windows10安装教程

    强烈建议安装anaconda之后再来安装这个pytorch,具体怎么安装百度搜索就知道了. 温馨提示,在安装anaconda的时候记得将"添加到环境变量"(安装的时候是英文的)这一 ...

  6. 解题思路:house robber i && ii && iii

    这系列题的背景:有个小偷要偷钱,每个屋内都有一定数额的钱,小偷要发家致富在北京买房的话势必要把所有屋子的钱都偷了,但是屋子之内装了警报器,在一定条件下会触发朝阳群众的电话,所以小偷必须聪明一点,才能保 ...

  7. ArcGIS API for JavaScript 4.2学习笔记[14] 弹窗的位置、为弹窗添加元素

    这一节我们来看看弹窗的位置和弹窗上能放什么. 先一句话总结: 位置:可以随便(点击时出现或者一直固定在某个位置),也可以指定位置 能放什么:四种,文字.媒体(图片等).表格.附件. [Part I 位 ...

  8. PHP-CGI进程占用过多CPU

    一般情况下,PHP-CGI只在用户访问的时候会占用CPU资源,但是最近有同事反映,服务器上的的PHP-CGI进程占用了非常多的CPU,但是访问流量却非常少.这显然是一个不正常的现象,说有些地方存在故障 ...

  9. New Life With 2018

    2017年转眼过去了.对自己来说.这一大年是迷茫和认知的一年.我的第一篇博客就这样记录下自己的历程吧 一:选择 从进入这一行到现在已经一年多了,2016年11月份就像所有的应届毕业生一样,都贼反感毕业 ...

  10. Unable to make the module: related gradle configuration was not found. Please, re-import the Gradle project and try again

    到stack overflow找到的答案,老外还是专业 I also had a similar problem, Go to : View -> Tool Windows -> Grad ...