Python numpy的简单操作(一)
2018-02-23 23:45
176 查看
一直想把学习Python的过程记录下来,现在就是关于numpy的简单操作
首先调取numpy库生成矩阵
B = np.array([2,3,4])
d = np.subtract(A,B)
print(c,d)
[3 5 7] [-1 -1 -1]从而得到结果,现在生成一个二纬的矩阵有两种方法生成首先用array生成
print(np.dot(Q,E))
[[29 35 41]
[41 50 59]]
print(np.matmul(Q,E))
[[29 35 41]
[41 50 59]]查看矩阵元素vec = np.array([1, 2, 3, 4])
print(vec.shape)
(4,)
matrix = np.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
(2, 3)查看矩阵类型number = np.array([1, 2, 3, 4])
number.dtype
dtype('int32')索引,切片与步长
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[0:1,0:3:2])
首先调取numpy库生成矩阵
import numpy as np
A = np.array([1,2,3])
vec = numpy.array([1, 2, 3, 4]) print(vec.shape) (4,) matrix = numpy.array([[5, 10, 15], [20, 25, 30]]) print(matrix.shape) (2, 3)
B = np.array([2,3,4])
print(A,B) 生成两个一维矩阵
[1 2 3] [2 3 4]现在另两个矩阵进行加减法c = np.add(A,B)
d = np.subtract(A,B)
print(c,d)
[3 5 7] [-1 -1 -1]从而得到结果,现在生成一个二纬的矩阵有两种方法生成首先用array生成
H = np.array([[1,2,3],[2,3,4]]) J = np.array([[2,3,4],[4,5,6]]) print(H) [[1 2 3] [2 3 4]] print(J) [[2 3 4] [4 5 6]]然后用matrix生成
Q = np.mat('1,2,3;2,3,4') W = np.mat('2,3,4;4,5,6') print(Q) [[1 2 3] [2 3 4]] print(W) [[2 3 4] [4 5 6]]把生成的矩阵进行乘法矩阵乘法有两种,首先有元素之间的乘法
print(H*J) [[ 2 6 12] [ 8 15 24]] np.multiply(H,J) [[ 2 6 12] [ 8 15 24]]然后是向量乘法E = np.array([[2,3,4],[3,4,5],[7,8,9]])
print(np.dot(Q,E))
[[29 35 41]
[41 50 59]]
print(np.matmul(Q,E))
[[29 35 41]
[41 50 59]]查看矩阵元素vec = np.array([1, 2, 3, 4])
print(vec.shape)
(4,)
matrix = np.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
(2, 3)查看矩阵类型number = np.array([1, 2, 3, 4])
number.dtype
dtype('int32')索引,切片与步长
number = np.array([1, 2, 3, 4, 5, 6]) number[2] 3 number[0:2] array([1, 2]) number[0:5:2] array([1, 3, 5])矩阵的索引,切片与步长matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[0:1,0:3:2])
[[ 5 15]]
其中0:1,代表了按行数取,0:3:2代表了按列取并且步长为2
相关文章推荐
- Python 线程和 redis 简单读写操作应用
- python简单操作redis
- Python - 二叉树简单操作
- python django简单操作
- 基于Python实现一个简单的银行转账操作
- python-igraph安装及简单操作
- python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查
- 【Python】Numpy简单入门
- 简单的python线程操作代码
- python之scipy库简单操作
- python3.3 简单操作sqlite
- Python3——简单的了解和简单操作
- Python操作json数据的一个简单例子
- python科学计算_numpy_简单的多项式应用
- 关于mysql-python的简单事务操作
- python 参数及简单文件操作
- Python中的错误和异常处理简单操作示例【try-except用法】
- Python脚本控制的WebDriver 常用操作 <八> 简单的对象定位
- windows 平台下,运用 Python 进行简单的文件操作需要用到的函数
- Python3中简单的文件操作及两个简单小实例分享