cs231n notes:Python Numpy Tutorial
来源:互联网 发布:2m数据测试仪 编辑:程序博客网 时间:2024/06/01 10:05
##cs231n notes:Python Numpy Tutorial#idctionaryd = {'cat':'cute','dog':'furry'}#三种遍历方式for k,v in d.items(): print(k,v)for i in d: print(i,d[i])
cat cutedog furrycat cutedog furry
#get(key[, default])¶# Return the value for key if key is in the dictionary, else default. # If default is not given, it defaults to None, so that this method never raises a KeyError.d['monkey'] = 'wet'print(d.get('fish')) # Noneprint(d) #get 并不会让字典里面的key-value增加print(d.get('monkey', 'N/A')) # wetdel d['monkey'] print(d.get('monkey', 'N/A')) # N/Aprint(d)
None{'cat': 'cute', 'dog': 'furry', 'monkey': 'wet'}wetN/A{'cat': 'cute', 'dog': 'furry'}
# sorted(iterable,key,reverse)# 其中iterable表示可以迭代的对象,例如可以是dict.items()、dict.keys()等,key是一个函数,用来选取参与比较的元素,# reverse则是用来指定排序是倒序还是顺序,reverse=true则是倒序,reverse=false时则是顺序,默认时reverse=false。# 根据value排序a = [9,4,9,9,3,4,3,6,7,6,9]d = {}for i in a: d[i] = d.get(i,1)+1 #如果i第一次放进来,即不存在,则d.get()=1sorted(d.items(),key=lambda item:item[1],reverse=False)
[(7, 2), (4, 3), (3, 3), (6, 3), (9, 5)]
# lambda 对简单函数的简洁表示# map()函数接受两个参数,一个是函数,一个是iterablea = [1,2,3]b = map(lambda a:a+10,li)# print(list(b))print(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
<map object at 0x7f299876ff28>
# reduce()函数# 这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算from functools import reducedef add(x, y): return 10 * x + yreduce(add, [1, 3, 5, 7, 9])
13579
# enumerateanimals = {'cat', 'dog', 'fish'}for idx, animal in enumerate(animals): print('#%d: %s' % (idx + 1, animal))
#1: dog#2: fish#3: cat
# strings = 'hello'print(s.capitalize())print(s.upper()) #转换为大写print(s.rjust(7))print(s.replace('l','(ell)'))print(s.rjust(10).strip()) #Strip leading and trailing whitespace 去掉开头和结尾的空格
HelloHELLO hellohe(ell)(ell)ohello
# numpyimport numpy as npa = np.array([[1,2],[3,4],[5,6]])print(a[[0,1,2],[0,1,1]]) ## array([1, 4, 6]) 相当于a[0,0],a[1,1],a[2,1]print([a[0,0],a[1,1],a[2,1]])print(np.array([a[0,0],a[1,1],a[2,1]]))
[1 4 6][1, 4, 6][1 4 6]
#和MATLAB不同,*是元素逐个相乘,而不是矩阵乘法。#在Numpy中使用dot来进行矩阵乘法x = np.array([[1,2],[3,4]], dtype=np.float64)y = np.array([[5,6],[7,8]], dtype=np.float64)print(x * y)print(np.multiply(x,y))print(np.dot(x,y))# 求和 sumprint(np.sum(x)) # print(np.sum(x, axis=0)) # 列print(np.sum(x, axis=1)) # 行
[[ 5. 12.] [ 21. 32.]][[ 5. 12.] [ 21. 32.]][[ 19. 22.] [ 43. 50.]]10.0[ 4. 6.][ 3. 7.]
# broadcastingx = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])v = np.array([1, 0, 1])y = x + v # Add v to each row of x using broadcastingprint(y) # Prints "[[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]]"x = np.arange(4) # [0 1 2 3]xx = x.reshape(4,1) # np.array([[0],[1],[2],[3]])y = np.ones(5) # [ 1. 1. 1. 1. 1.]yy = np.reshape(y,(1,5)) # [[ 1. 1. 1. 1. 1.]]print(yy)print(xx+y) #boardcasting (4,1)+(5,)= (4,5)print(xx+yy) #boardcasting (4,1)+(1,5)= (4,5) 这两个结果是一样的
[[ 2 2 4] [ 5 5 7] [ 8 8 10] [11 11 13]][[ 1. 1. 1. 1. 1.]][[ 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2.] [ 3. 3. 3. 3. 3.] [ 4. 4. 4. 4. 4.]][[ 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2.] [ 3. 3. 3. 3. 3.] [ 4. 4. 4. 4. 4.]]
# SciPyfrom scipy.misc import imread, imsave, imresizeimg = imread('/home/panxie/图片/aaa.jpg')print(img.dtype,img.shape) # uint8 (1009, 1428, 3) # 像素:宽度1428,高度1009,通道:red,green and blue channelsimg_tinted = img * [1,0.5,0.9]img_tinted = imresize(img_tinted,(500,500))imsave('/home/panxie/图片/bbb.jpg',img_tinted)plt.subplot(1,2,1)plt.imshow(img)plt.subplot(1,2,2)plt.imshow(np.uint8(img_tinted))plt.show()
uint8 (1009, 1428, 3)
# distancesimport numpy as npfrom scipy.spatial.distance import pdist,squareformx = np.array([[0,1],[1,0],[2,0]])print(x)d = pdist(x,'euclidean') #计算(2范数)欧几里德距离#Computes the distance between m points using Euclidean distance (2-norm) as the distance metric between the points. The points are arranged as m n-dimensional row vectors in the matrix X.print(d)
[[0 1] [1 0] [2 0]][ 1.41421356 2.23606798 1. ]
# plottingimport matplotlib.pyplot as pltx = np.arange(0, 8, 0.1)y = np.sin(x)plt.plot(x,y)plt.xlabel('x axis label')plt.ylabel('y axis label')plt.title('Sine')plt.legend(['sine'])plt.show()
阅读全文
0 0
- cs231n notes:Python Numpy Tutorial
- Stanford CS class CS231n Notes(One):Python Numpy Tutorial
- cs231n-python numpy tutorial
- Python Numpy Tutorial——CS231n
- cs231n课程资料Python Numpy Tutorial的Python3版本
- Python-Numpy-Basics-Notes
- CS231n课程Python Numpy教程二:Numpy
- Python Numpy Tutorial
- Python Numpy Tutorial
- Python Numpy Tutorial: 列表
- Python Numpy Tutorial: 字典
- Python / Numpy Tutorial
- CS231n课程笔记翻译:Python Numpy教程
- CS231n学习笔记-1.Python&Numpy学习
- CS231n课程笔记翻译:Python Numpy教程
- CS231n课程Python Numpy教程三:SciPy
- CS231n课程Python Numpy教程四:Matplotlib
- CS231n(1):Python Numpy教程
- 计算机网络 学习摘要(5)
- 字符编码方式
- HDU 4631
- JavaScript 正则表达式和全局对象
- linux 编译安装amqp
- cs231n notes:Python Numpy Tutorial
- 详解zabbix安装部署(Server端篇)
- ISFP职业规划,“做事情想一出是一出,不行了再换”的28岁ISFP女孩
- RxJava
- VTK图形处理之自定义纹理映射Filter
- 设计模式六大原则(3):依赖倒置原则
- 小白成长之路(5)--插入排序(直接插入,二分插入,希尔排序)
- 这样的一个不带头节点的循环双链表哪里出错了呢
- 凛冬将至