numpy 的简单学习(矩阵的创建,乘法运算,转置,随机生成矩阵的方法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27492735/article/details/82702157

numpy的一些简单小常识

#coding = 'utf-8'
import numpy as np
a = np.array([2,1,4,6,3])#创建一个数组a
print(a)
print('=================================')
b = np.zeros((3,4))#创建一个3*4的0元素矩阵
print(b)
print('=================================')
c = np.ones((4,3))#创建一个4*3的1元素矩阵
print(c)
print('=================================')
d = np.array([[1,2,3],
              [2,3,4],
              [3,4,5],
              [4,5,6]])#利用array创建一个矩阵
print(d)
print('=================================')
e = np.random.rand(2,4)#随机生成一个2*4的矩阵,值在(0,1)之间
e1 = np.random.rand(4,3,2)#随机生成4个3*2的矩阵
print(e1)
print('**********************************')
print(e)
print('=================================')
f = np.random.randn(2,4)#随机生成一个2*4的矩阵,值存在负数,具有标准正态分布的特性
f1 = np.random.randn(5,3,2)
print(f1)
print('**********************************')
print(f)
print('=================================')
g = np.random.randint(1,5)#生成一个1-5的随机整数
g1 = np.random.randint(1,10,size=(10,8))#生成一个元素值位1-10之间的大小为10*8的矩阵
print(g1)
print('**********************************')
print(g)
print('随机生成[0,1)之间的浮点数')
print('-----------random_sample--------------')
h = np.random.random_sample(size=(3,2))
print(h)
print('-----------random--------------')
h1 = np.random.random(size=(2,2))
print(h1)
print('-----------ranf--------------')
h2 = np.random.ranf(size=(2,2))
print(h2)
print('-----------sample--------------')
h3 = np.random.sample(size=(2,2))
print(h3)
print(h3.shape)
print('矩阵的点乘')
print('--------------------------')
x = np.dot(h,h1)#矩阵的点乘,按元素进行相乘
print(x)
print('--------矩阵的转置--------')
print(x.T)

print('')
print('------广播-------')
print('矩阵的shape不一致,通过reshape进行调整')
x = np.random.randint(2,8,size=(3,4))
y = np.random.randint(1,9,size=(4,3))
print('y:',y)
y = y.reshape(3,4)
print('x:',x)
print('y:',y)
print('x+y=',x+y)

print('')
print('------np.arange()------')
a = np.arange(20,step = 2).reshape(2,5)#按排列的顺序生成start=0,end < 20,step = 2的整数,再利用reshape组成一个2*5的矩阵
print(a)
print('')

运行结果如下:

"D:/python project/deep learing/numpy/1.1.py"
[2 1 4 6 3]
=================================
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
=================================
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
=================================
[[1 2 3]
 [2 3 4]
 [3 4 5]
 [4 5 6]]
=================================
[[[0.07269259 0.10318425]
  [0.63483835 0.57930749]
  [0.45847482 0.96380657]]

 [[0.19184909 0.40354845]
  [0.71109631 0.37428877]
  [0.45283132 0.47235885]]

 [[0.87023493 0.21834932]
  [0.44186986 0.24913898]
  [0.87391932 0.61050419]]

 [[0.05497517 0.12890712]
  [0.28894985 0.51151779]
  [0.37588991 0.93231969]]]
**********************************
[[0.24929256 0.19597898 0.80631295 0.7228318 ]
 [0.71598807 0.10716338 0.9645689  0.0378334 ]]
=================================
[[[ 0.6093495  -1.35627677]
  [-0.66191887 -1.09727734]
  [ 1.29890737  0.73013595]]

 [[ 0.01984872 -0.5046301 ]
  [ 0.66322497 -0.18515963]
  [ 0.25616161 -0.67217041]]

 [[ 2.5878127  -0.23893   ]
  [-1.46914138  0.16057052]
  [-0.5969543  -2.06847457]]

 [[-0.13046129 -0.29135587]
  [-1.13931236 -1.69595818]
  [ 0.56978455 -0.50754035]]

 [[ 0.55880953  1.85532563]
  [-0.86423249  0.41589772]
  [ 0.26275627  0.57950653]]]
**********************************
[[-1.86412133 -0.30360421  0.88771507 -2.25272266]
 [ 1.41712964  0.47065414  0.35724498 -0.25150316]]
=================================
[[6 9 6 8 4 3 6 9]
 [5 4 7 2 4 5 4 2]
 [1 3 1 6 5 5 7 1]
 [6 1 5 8 6 6 3 9]
 [2 4 6 9 4 1 1 6]
 [6 9 5 3 9 8 7 6]
 [6 4 7 4 9 5 6 8]
 [4 4 4 9 5 7 3 7]
 [3 3 4 7 7 7 6 4]
 [4 6 3 3 4 9 3 1]]
**********************************
4
随机生成[01)之间的浮点数
-----------random_sample--------------
[[0.70630401 0.23234587]
 [0.05806361 0.57765543]
 [0.13450884 0.74544632]]
-----------random--------------
[[0.36338596 0.82962207]
 [0.04111808 0.55551702]]
-----------ranf--------------
[[0.9477004  0.51604987]
 [0.82221483 0.40770065]]
-----------sample--------------
[[0.21015343 0.9623032 ]
 [0.83385894 0.15097825]]
(2, 2)
矩阵的点乘
--------------------------
[[0.26621458 0.71503748]
 [0.04485158 0.36906828]
 [0.07952995 0.52569963]]
--------矩阵的转置--------
[[0.26621458 0.04485158 0.07952995]
 [0.71503748 0.36906828 0.52569963]]

------广播-------
矩阵的shape不一致,通过reshape进行调整
y: [[8 6 7]
 [7 4 7]
 [7 4 5]
 [1 8 4]]
x: [[6 4 7 6]
 [3 7 6 5]
 [4 5 6 3]]
y: [[8 6 7 7]
 [4 7 7 4]
 [5 1 8 4]]
x+y= [[14 10 14 13]
 [ 7 14 13  9]
 [ 9  6 14  7]]

------np.arange()------
[[ 0  2  4  6  8]
 [10 12 14 16 18]]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_27492735/article/details/82702157
今日推荐