numpy函数API(二)

1 linspace

功能:生成指定范围,指定行数的数据;

  • linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
参数描述
start标量(scalar),取值起点
stop标量(scalar),取值起点
num整型,可选;生成的数据量,默认为50,
endpoint布尔,可选;如果为True,最后一个数值为stop数值区间为[start,stop],否则为开区间[start,stop),默认为True
retstep布尔,可选;为True返回一个tuple,内容为生成的数据和步长,默认False,只返回数据,类型为ndarray
dtypedtype可选,输出array的类型,未设置,输出的数据类型参考其他参数

1.0 Demo1

import numpy as np 
def __linspace():
	a = np.linspace(0, 1, 10)
	print(type(a))
	print(a)
	print(a.shape)
__linspace()

1.2 Result

<class 'numpy.ndarray'>
[0.         0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
 0.66666667 0.77777778 0.88888889 1.        ]
(10,)

1.3 Demo2

import numpy as np 
def __linspace():
	a = np.linspace(0, 1, 10, retstep=True)
	print(type(a))
	print(a)
__linspace()

1.4 Result

<class 'tuple'>
(array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
       0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ]), 0.1111111111111111)

2 newaxis

功能:增加一个维度.

2.1 Demo

import numpy as np
def __newaxis():
	a = np.linspace(0, 1, 10)[np.newaxis, :, np.newaxis]
	print(type(a))
	print(a)
	print(a.shape)
__newaxis()

2.2 Result

<class 'numpy.ndarray'>
[[[0.        ]
  [0.11111111]
  [0.22222222]
  [0.33333333]
  [0.44444444]
  [0.55555556]
  [0.66666667]
  [0.77777778]
  [0.88888889]
  [1.        ]]]
(1, 10, 1)

3 arange

功能:生成指定范围及步长的数据;

  • arange([start,] stop[, step,], dtype=None)
参数描述
startnumber,可选;左边界数据
stopnumber,可选;右边界数据
stepnumber,可选;步长
dtypedtype可选,输出array的类型,未设置,输出的数据类型参考其他参数

3.1 Demo1

def __arange():
	a = np.arange(10)
	print(type(a))
	print(a)
	print(a.shape)
__arange()

3.2 Result

# 默认从0开始,输出10个数字,步长为1
<class 'numpy.ndarray'>
[0 1 2 3 4 5 6 7 8 9]
(10,)

3.3 Demo2

def __arange():
	a = np.arange(1,10,2)
	print(type(a))
	print(a)
	print(a.shape)

3.4 Result

<class 'numpy.ndarray'>
[1 3 5 7 9]
(5,)

4 random.rand

功能:生成指定维度的[0, 1)的数据;

  • random.rand(d1, d2, …, dn)
参数描述
d1,…, dnint,可选,表示随机数维度

4.1 Demo1

import numpy as np
def __randomrand():
	a = np.random.rand(10)
	print(type(a))
	print(a)
	print(a.shape)
__randomrand()

4.3 Result

# 生成随机数,维度10x?,给定rand(10)
<class 'numpy.ndarray'>
[0.80185869 0.15838866 0.52466908 0.84533854 0.0618174  0.99462978
 0.71628667 0.75139132 0.75699796 0.59206482]
(10,)

4.3 Demo2

def __randomrand():
	a = np.random.rand(2, 10)
	print(type(a))
	print(a)
	print(a.shape)
__randomrand()

4.4 Result

# 产生随机数,维度为2x10,参数给定rand(2, 10)
<class 'numpy.ndarray'>
[[0.92662945 0.11674728 0.81313493 0.35200275 0.39094832 0.67202393
  0.81128457 0.76201128 0.81080647 0.82904019]
 [0.9918214  0.43914692 0.56131675 0.52378336 0.81830723 0.1900779
  0.43102381 0.41744522 0.69764962 0.15374672]]
(2, 10)

5 random.normal

功能:生成指定维度,均值及标准差的随机数据;

  • random.normal(loc, scale, size)
参数描述
loc均值
scale标准差
size数据维度

5.1 Demo1

生成均值为1,标准差为1的一维数组1x5.

import numpy as np
def random_normal():
	a = np.random.normal(1, 1, 5)
	mean = np.mean(a)
	std = np.std(a, ddof=1)
	# print(a.shape)
	print("a's Value:{}".format(a))
	print("Mean of a :{}".format(mean))
	print("Standard of a: {}".format(std))
random_normal

5.2 Result

a's Value:[0.39995584 2.00385871 1.60713669 1.5594456  0.19042267]
Mean of a :1.1521638995074013
Standard of a: 0.804522501835418

5.3 Demo2

生成均值为1,标准差为1的二维数组2x3.

import numpy as np
def random_normal():
	a = np.random.normal(1, 1, [2, 3])
	mean = np.mean(a)
	std = np.std(a, ddof=1)
	# print(a.shape)
	print("a's Value:{}".format(a))
	print("Mean of a :{}".format(mean))
	print("Standard of a: {}".format(std))
random_normal

5.4 Result

a's Value:[[ 2.18075564  1.38755248  1.54134979]
 [-0.21720212  0.37056487  1.45739034]]
Mean of a :1.1200684998773773
Standard of a: 0.8761183951160901

6 reshape(a, newshape, order=‘C’)

功能:改变原数据维度;

序号参数描述
1a待改变维度的数据
2newshape新维度,新维度和元数据维度匹配,即尺寸相同,若新维度为一个整数,则返回一维数据
3C可选参数,使用索引(C,A,F)顺序读取数据,并按照该顺序存储数据,C即使用类C方式读写数据,最后一个轴索引变化最快,回到第一个轴索引变化最慢;F即类Fortran读写数据,第一个索引变化最快,最后一个索引变化最慢,C和F不考虑基础数据的内存布局,只引用索引顺序,A表示,如果a在内存中是Fortran连续的,则a以类Fortran索引顺序读写数据,否则以类C顺序

6.1 Demo

import numpy as np
def reshapeTest():
	a = np.arange(10)
	b = np.reshape(a, (2, 5))
	print("a value: {}".format(a))
	print("Shape of a: {}".format(a.shape))
	print("b value: {}".format(b))
	print("Shape of b: {}".format(b.shape))
reshapeTest()

6.2 Result

a value: [0 1 2 3 4 5 6 7 8 9]
Shape of a: (10,)
b value: [[0 1 2 3 4]
 [5 6 7 8 9]]
Shape of b: (2, 5)

7 meshgrid

功能:根据输入的坐标向量生成坐标矩阵;
格式:meshgrid(x1, x2, …, xn)
返回:ndarray(N1,N2, …, Nn)其中Ni=len(xi)

序号参数描述
1x1,x2,…xn1维数组
2indexing索引(可选)
3sparsebool(可选),若为True返回稀疏坐标网格以节省内存,默认False
4copybool(可选)
import numpy as np
a = np.arange(0, 7.9, 0.02)
b = np.arange(-0.9, 3.5, 0.02)
print("shape of a: {}".format(a.shape))
print("shape of b: {}".format(b.shape))
aa, bb = np.meshgrid(a,b)
print("shape of aa: {}".format(aa.shape))
print("aa value: {}".format(aa))
print("bb value: {}".format(bb))
  • Result
shape of a: (395,)
shape of b: (220,)
shape of aa: (220, 395)
aa value: [[0.   0.02 0.04 ... 7.84 7.86 7.88]
 [0.   0.02 0.04 ... 7.84 7.86 7.88]
 [0.   0.02 0.04 ... 7.84 7.86 7.88]
 ...
 [0.   0.02 0.04 ... 7.84 7.86 7.88]
 [0.   0.02 0.04 ... 7.84 7.86 7.88]
 [0.   0.02 0.04 ... 7.84 7.86 7.88]]
bb value: [[-0.9  -0.9  -0.9  ... -0.9  -0.9  -0.9 ]
 [-0.88 -0.88 -0.88 ... -0.88 -0.88 -0.88]
 [-0.86 -0.86 -0.86 ... -0.86 -0.86 -0.86]
 ...
 [ 3.44  3.44  3.44 ...  3.44  3.44  3.44]
 [ 3.46  3.46  3.46 ...  3.46  3.46  3.46]
 [ 3.48  3.48  3.48 ...  3.48  3.48  3.48]]
  • Analysis
    (1) 输入为x1和x2,维度:(395,1)和(220,1),输出维度(220, 395),即输入维度为(M, N),输出维度为(N, M);
    (2) 返回的第一个坐标矩阵数据行数据为完整的x1数据,维度为(395,1),共有395列数据;
    (3) 返回的第二个坐标矩阵数据列数据的x2数据,维度为(220,1),共有220行数据;

打印模块说明

import numpy as np
def __functionDesc():
	print(np.linspace.__doc__)
__functionDesc()

更新ing
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值