首页 > 其他 > 详细

numpy学习笔记

时间:2019-11-11 00:46:43      阅读:119      评论:0      收藏:0      [点我收藏+]

1、

numpy求均值、方差、标准差

1 import numpy as np 
2  
3 arr = [1,2,3,4,5,6]
4 #求均值
5 arr_mean = np.mean(arr)
6 #求方差
7 arr_var = np.var(arr)
8 #求标准差
9 arr_std = np.std(arr,ddof=1)

2、

arange函数

 

np.arange([start, ]stop, [step, ]dtype=None)
start:可忽略不写,默认从0开始;起始值
stop:结束值;生成的元素不包括结束值
step:可忽略不写,默认步长为1;步长
dtype:默认为None,设置显示元素的数据类型

举例:

import numpy as np
nd1 = np.arange(5)#array([0, 1, 2, 3, 4])
nd2 = np.arange(1,5)#array([1, 2, 3, 4])
nd3 = np.arange(1,5,2)#nd3 = np.arange(1,5,2)#array([1, 3])

 

3、

dot函数

 

1、如果处理的是一维数组,则得到的是两数组的內积。
import numpy as np
x=np.array([0,1,2,3,4])#等价于:x=np.arange(0,5)
y=x[::-1]
print x
print y
print np.dot(x,y)
输出:
[0 1 2 3 4]
[4 3 2 1 0]
10

2、向量点积
import numpy as np
x=np.arange(0,5)
y=np.random.randint(0,10,5)
print x
print y
print np.dot(x,y)
输出:
[0 1 2 3 4]
[5 1 0 9 2]
36

3、矩阵的乘法
import numpy as np
x=np.arange(0,5)
y=np.random.randint(0,10,size=(5,1))
print x
print y
print "x.shape:"+str(x.shape)
print "y.shape"+str(y.shape)
print np.dot(x,y)
输出:
[0 1 2 3 4]
[[3]
 [7]
 [2]
 [8]
 [1]]
x.shape:(5,)
y.shape(5, 1)
[39]

4、reshape用法
import numpy as np
x=np.arange(0,6).reshape(2,3)
y=np.random.randint(0,10,size=(3,2))
print x
print y
print "x.shape:"+str(x.shape)
print "y.shape"+str(y.shape)
print np.dot(x,y)

结果:
[[0 1 2]
 [3 4 5]]
[[7 5]
 [0 7]
 [6 2]]
x.shape:(2, 3)
y.shape(3, 2)
[[12 11]
 [51 53]]

 

 

 

 

numpy学习笔记

原文:https://www.cnblogs.com/liuwenhan/p/11832516.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!