Python--numpy库

Numpy:用于处理和储存大型矩阵的库

安装:

yum install numpy

conda install numpy

#import numpy as np
# from numpy import random
#
# #np.arange类似range
# a=np.arange(32).reshape((2,4,4))
# '''
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]
#   [12 13 14 15]]
#
#  [[16 17 18 19]
#   [20 21 22 23]
#   [24 25 26 27]
#   [28 29 30 31]]]'''
# #索引
# b=a[1][2][1] #显示25
# #切片
# '''[[ 8  9 10 11]
#  [24 25 26 27]]
# '''
# c=a[:,2,:]
#
# '''[[ 1  5  9 13]
#  [17 21 25 29]]'''
# d=a[:,:,1]
#
#
# '''[[[ 5  6]
#   [ 9 10]
#   [13 14]]
#
#  [[21 22]
#   [25 26]
#   [29 30]]]'''
# f=a[:,1:,1:-1]


# 平均分成三个数组  np.split(array,x)    if type(x)=int 平均分配

# [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8,  9, 10, 11])]

# g=np.split(np.arange(12),3)


# 按下标位置分成三组
# [a,b]   array[0:a] array[a:x] array[x:]      len[x:]==b
# h = np.split(np.arange(9), [3, -5])


# 拼接   拼接的array写到一个元组里
# '''[[0 1 2]
#  [3 4 5]]
# [[ 6  7  8]
#  [ 9 10 11]]'''
# l0 = np.arange(6).reshape(2, 3)
# l1 = np.arange(6,12).reshape((2, 3))

# #沿纵轴拼接
# '''[[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]]'''
# m=np.vstack((l0,l1))  #q = np.concatenate((l0, l1))
#
# #沿横轴拼接
# '''[[ 0  1  2  6  7  8]
#  [ 3  4  5  9 10 11]]'''
#
# n=np.hstack((l0,l1))   #r = np.concatenate((l0, l1), axis=-1)


# 增加一个纬度

# '''[[[ 0  1  2]
#   [ 3  4  5]]
#
#  [[ 6  7  8]
#   [ 9 10 11]]]'''
# s=np.stack((l0,l1))


# 转置
# 二维array的转置   transpose方法  .T属性  swapaxes方法

# '''[[0 3]
#  [1 4]
#  [2 5]]
# [[0 3]
#  [1 4]
#  [2 5]]'''
#ar=np.arange(6).reshape(2,3)

# print(ar.T)
# print(ar.transpose())

#多维array的转置暂时还没搞懂


# ##基础数学运算
# '''绝对值:[6 7 2]'''
# print(np.abs([-6,7,-2]))
# '''sin函数:1.0'''
# print(np.sin(np.pi/2))
# '''次方:3*3=27'''
# print(np.power(3,3))
# '''开方:6.0'''
# print(np.sqrt(36))
# '''求和:15'''
# print(np.sum(np.arange(6)))
# '''求平均值:6.0'''
# print(np.mean([4,5,6,7,8]))
# '''标准差:np.std()'''

#array之间的运算+-*/ **  <如果纬度相同,则对应纬度进行运算>
# '''[[ 1  4  9]
#  [ 4 10 18]]'''
# a=np.array([[1,2,3],[1,2,3]])
# b=np.array([[1,2,3],[4,5,6]])
# print(a*b)

# <如果某array与另一个array子纬度相同,则分别进行运算>
# '''[[2 4]
#  [4 6]
#  [6 8]]'''
# a=np.array([1,2])
# b=np.array([[1,2],[3,4],[5,6]])
# print(a+b)


#############random 模块##############


#随机数种子
# random.seed(42)
# 生成a*b*c 的array  每个数为[0,1)里的随机浮点数
# print(random.rand(2,2,4))
#  random.random((3, 3))
#  random.sample((3, 3))
#  random.random_sample((3, 3))
#  random.ranf((3, 3))

#随机生成[1,4)的随机数
# print(random.uniform(1,4,3))   #浮点型
# print(random.randint(1,4,3))   #整数型

#a = np.arange(10)
#
# # 从a中有回放的随机采样7个
# print(random.choice(a, 7))
#
# # 从a中无回放的随机采样7个
# print(random.choice(a, 7, replace=False))
#
# # 对a进行乱序并返回一个新的array
# print(random.permutation(a))

# np.random.shuffle(a)#对序列打乱 无返回值




######### numpy.random.normal()  高斯分布随机数#########
#API: normal(loc=0.0, scale=1.0, size=None)
#loc:均值,scale:标准差,size:抽取样本的size
# '''[[ 0.42041141 -0.5067259  -1.75613624]
#  [-0.6653468   1.53706222 -1.67955704]]'''
# n = np.random.normal(loc=0.0, scale=1, size=[2,3])
# print(n)

######### numpy.random.randn() 标准正态分布随机数#########

#numpy.random.randn(d0, d1, …, dn)函数:
#从标准正态分布中返回一个(d0*d1* …* dn)维样本值


# '''[[[ 0.73337647  1.09972846  1.23195031]
#   [-0.57164433  0.78799456  0.19965629]]
#
#  [[-0.79699024 -0.63573993  1.43197953]
#   [-0.36167178 -1.3263573   1.03402977]]
#
#  [[ 0.67319901  1.69376297 -1.78061542]
#   [ 0.54296244 -0.06592913  1.20112623]]
#
#  [[ 0.46980367  0.28776892  0.35763454]
#   [-1.74770128  0.57341897 -0.53370068]]]
# '''
# print(np.random.randn(4, 2, 3))
未完待续.....

猜你喜欢

转载自blog.csdn.net/wl_python/article/details/80626784