欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python Numpy Tutorials: 数组中数学函数及变形(reshape)

程序员文章站 2023-11-23 00:02:22
# -*- coding: utf-8 -*- """ python version: 3.5 created on thu may 11...
# -*- coding: utf-8 -*-
"""
python version: 3.5
created on thu may 11 17:00:47 2017
e-mail: eric2014_lv@sjtu.edu.cn
@author: didilv
"""

# 数学的函数
import numpy as np

x = np.array([[1,2],[3,4]])

# 计算所有的数组值
print(np.sum(x))
# 计算矩阵的列  
print(np.sum(x, axis=0))  
# 计算矩阵的行
print(np.sum(x, axis=1))  
# python还是跟matlab有关的,是按列排序的。

# 当然可以有更多的延伸,都是数学的其他函数
#  https://docs.scipy.org/doc/numpy/reference/routines.math.html

x = np.array([[1,2], [3,4]])
print(x)    # 输出    "[[1 2]
           #          [3 4]]"
print(x.t)  # 输出   "[[1 3]
           #          [2 4]]"

# note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print(v)    # 输出 "[1 2 3]"
print(v.t)  # 输出 "[1 2 3]" 数组还是数组没有列的概念啊!

# 更多的延伸,类似reshape之类的,可以详情看以下链接
# https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html