码迷,mamicode.com
首页 > 其他好文 > 详细

(4)Numpy+矩阵计算+和生成

时间:2018-01-14 21:26:37      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:bbb   rgb   license   basic   tco   login   universal   issues   canvas   

(4)Numpy 矩阵计算 和生成

 

 

In [1]:
import numpy
 

1.创建一个矩阵

In [3]:
vector=numpy.array([1,2,3,4,5,6])
print(vector)
 
[1 2 3 4 5 6]
 

2.创建多维矩阵——常用最大值最小值函数

In [5]:
vector=numpy.array([
    [1,2,3,4],
    [1,2,3,4],
    [1,2,3,5]
])
print(vector)
print("----最值------")
print(vector.min())
print(vector.max())
print(vector.sum())
 
[[1 2 3 4]
 [1 2 3 4]
 [1 2 3 5]]
----最值------
1
5
31
In [7]:
print(vector.sum(axis=1))
print(vector.sum(axis=0))
 
[10 10 11]
[ 3  6  9 13]
In [10]:
ary=numpy.arange(15)   #生产矩阵
print(ary)
ary=numpy.arange(1,50,2)   #生产矩阵
print(ary)
 
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[ 1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49]
In [9]:
ary.reshape(3,5)
 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-576d1d57b3f2> in <module>()
----> 1ary.reshape(3,5)

ValueError: cannot reshape array of size 25 into shape (3,5)
In [23]:
ary.reshape(5,3)
Out[23]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])
In [26]:
ary.size
Out[26]:
15
In [32]:
numpy.zeros((3,5))
Out[32]:
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
In [43]:
numpy.ones((2,3,2),dtype=numpy.int32)
Out[43]:
array([[[1, 1],
        [1, 1],
        [1, 1]],

       [[1, 1],
        [1, 1],
        [1, 1]]])
In [55]:
#随机数
numpy.random.random()
Out[55]:
0.4937280824938821
In [65]:
#随机矩阵
numpy.random.random((2,3))
Out[65]:
array([[ 0.32794465,  0.38478974,  0.84989759],
       [ 0.43801213,  0.43802513,  0.9928669 ]])
 

这里是markdown

 

d

(4)Numpy+矩阵计算+和生成

标签:bbb   rgb   license   basic   tco   login   universal   issues   canvas   

原文地址:https://www.cnblogs.com/zhuimengzhe/p/8284082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!