ndarray的创建

使用np.array()创建ndarray

使用np.array()由python list创建,参数列表为:[1,2,3,4,5]。
注意:numpy默认ndarray的所有元素的类型是相同的,如果传递进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int。

import numpy as np

n1 = np.array([1, 2, 3, 4, 5])
print(n1)          # [1 2 3 4 5]

n2 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print("\n", n2)   
print(n2.shape)    # shape显示是几维的数  (3, 4)

n3 = np.array(list("ABC"))
print("\n", n3)    # ['A' 'B' 'C']

n4 = np.array([1, 2, 3.45,"python"])
print("\n", n4)    # ['1' '2' '3.45' 'python']

使用np.routines函数创建ndarray

导入相关的包:

import numpy as np
import matplotlib.pyplot as plt

shape : 整数或者整型元组定义返回数组的形状;
dtype : 数据类型,可选定义返回数组的类型。
order : {‘C’, ‘F’}, 可选规定返回数组元素在内存的存储顺序:
C(C语言)-rowmajor;F(Fortran)column-major。

'''np.ones(shape,dtype=None,order='c')'''
# 依据给定形状和类型(shape[, dtype, order])返回一个新的元素全部为1的数组
n1 = np.ones(shape=(4, 5), dtype=float)    # 返回一个4行5列的全部是1的二维数组
print("\nn1 =", n1)

img = np.ones(shape=(100, 80, 3), dtype=float)   # 返回一个3维数组,这是一个图片
plt.imshow(img)
plt.show()    # 因为都是1,所以颜色是白色的


'''np.zeros(shape,dtype=None,order='c')'''
# 依据给定形状和类型(shape[, dtype, order])返回一个新的元素全部为0的数组
n2 = np.zeros((4, 4))    # 返回4行4列元素全部为0的数组
print("\nn2 =", n2)


'''np.full(shape,fill_value,dtype=None,order='c')'''
n3 = np.full((10, 10), fill_value=1024)       # 返回10行10列,元素全部为2014的数组
print("\nn3 =", n3)

输出结果为:

n1 = [[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]

n2 = [[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

n3 = [[1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]
 [1024 1024 1024 1024 1024 1024 1024 1024 1024 1024]]

np.eye(N,M=None,k=0,dtype=float) 返回一个对角线元素为1,其他元素为0的二维数组’’’’’'参数:
N : 整数返回数组的行数;
M : 整数,可选返回数组的列数。如果不赋值的话,默认等于N;
k : 整数, 可选对角线序列号: 0 对应主对角线;,整数对应upper diagonal,负数对应lower diagonal。

'''np.eye(N,M=None,k=0,dtype=float) 返回一个对角线元素为1,其他元素为0的二维数组'''
n4 = np.eye(N=5)  # 一元5次方程,满秩   返回5行5列的对角线为1的数组
print("\nn4 =", n4)


'''np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)'''
# lin = liner线性
n5 = np.linspace(0, 50, 20)   # 返回0-50之间的数字,返回20个,平均分配0-50之间的数
print("\nn5 =", n5)


'''np.arange([start,],stop,[step,]dtype=None)'''
# 使用该方法创造ndarray的时候,是左闭右开
n6 = np.arange(0, 50, 2)              # 返回的是0-50之间的偶数
print("\nn6 =", n6)

输出结果为:

n4 = [[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

n5 = [ 0.          2.63157895  5.26315789  7.89473684 10.52631579 13.15789474
 15.78947368 18.42105263 21.05263158 23.68421053 26.31578947 28.94736842
 31.57894737 34.21052632 36.84210526 39.47368421 42.10526316 44.73684211
 47.36842105 50.        ]

n6 = [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46
 48]

np.random.randint(low,high=None,size=None,dtype=‘I’),输入:
low—–为最小值
high—-为最大值
size—–为数组维度大小
dtype—为数据类型,默认的数据类型是np.int。
返回值:返回随机整数或整型数组,范围区间为[low,high),包含low,不包含high; high没有填写时,默认生成随机数的范围是[0,low)。

'''np.random.randint(low,high=None,size=None,dtype='I')'''
n7 = np.random.randint(0, 50, size=3)    # 返回3个0-50之间的随机整数
print("\nn7 =", n7)

输出结果为:

n7 = [39 27  1]

np.random.randn(d0,d1,…dn) 标准的正态分布。
1)当函数括号内没有参数时,则返回一个浮点数;
2)当函数括号内有一个参数时,则返回秩为1的数组,不能表示向量和矩阵;
3)当函数括号内有两个及以上参数时,则返回对应维度的数组,能表示向量或矩阵;
输入通常为整数,但是如果为浮点数,则会自动直接截断转换为整数。’’’

n8 = np.random.randn(20)
print("\nn8 =", n8)


'''np.random.normal(loc=0.0,scale=1.0,size=None)'''
'''与np.random.randn()类似,但是np.random.standard_normal()的输入参数为元组(tuple)'''
n9 = np.random.normal(loc=175, scale=50, size=20)  # scale波动系数
print("\nn9 =", n9)


'''np.random.random(size=None) 生成0到1的随机数,左闭右开'''
n10 = np.random.random(size=10)   # 返回10个0-1之间的随机数
print("\nn10 =", n10)


# 使用随机数生成一张图片
n11 = np.random.random(size=(200, 300, 3))
plt.imshow(n11)
plt.show()

输出结果为:

n8 = [-0.27440919 -0.68108835 -0.44168708 -0.34827678 -0.79587066  0.82399601
 -1.02957989 -0.50607827 -1.66887802  0.41300325  2.02868262  0.22089797
  0.32912052  0.24563119 -0.83716368 -0.17191253 -0.32006167  1.7287281
 -0.12071231  0.05592785]

n9 = [193.45750083 259.919853   131.26818902 223.72504748 212.00150608
 165.15091226 179.78330709 202.33905436 154.22501573 164.14031127
 185.95357957 217.9513257   84.54093591 218.55366612 139.97299938
 214.24660044 146.15426398 219.53567957 191.53593494 227.41053122]

n10 = [0.27245741 0.42652646 0.17084201 0.34527144 0.86600467 0.77875667
 0.4523481  0.86345259 0.63797741 0.75817112]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值