您的位置:首页 > 编程语言 > Python开发

Chapter4-2 通用函数:快速的元素级数组函数

2016-12-17 14:40 204 查看
'''
Chapter4-2 通用函数:快速的元素级数组函数:

索引:!!!注意是元素级

1.函数列表

2.函数的简单示例

Created on 2016年12月16日

@author: Bigboy
'''

1.函数列表备忘:

一元函数:



二元函数:



2.简单示例内容:

#-*- coding:utf-8 -*-

#1-2-----------------------------------------------------------------------------
import numpy as np
#建两个二维数组
arr1 = np.arange(8).reshape((2,4))
arr2 = np.arange(8,16).reshape((2,4))

print arr1
'''
out:
[[0 1 2 3]
[4 5 6 7]]
'''
print arr2
'''
out:
[[ 8 9 10 11]
[12 13 14 15]]
'''
print np.sqrt(arr1)#开方运算
print np.modf(np.sqrt(arr1))#小数部分与整数部分分开输出
'''
out:
[[ 0. 1. 1.41421356 1.73205081]
[ 2. 2.23606798 2.44948974 2.64575131]]

(array([[ 0. , 0. , 0.41421356, 0.73205081],
[ 0. , 0.23606798, 0.44948974, 0.64575131]]),
array([[ 0., 1., 1., 1.],
[ 2., 2., 2., 2.]]))
'''
print np.square(arr1)#平方
'''
out:
[[ 0 1 4 9]
[16 25 36 49]]

'''
print np.exp(arr1)#指数
'''
out:
[[ 1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01]
[ 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03]]

'''

arr1 = arr1-4#每个元素都减4
print arr1
print np.sign(arr1)#x>0==1 x==0==0 x<0==-1
'''
out:
[[-4 -3 -2 -1]
[ 0 1 2 3]]

[[-1 -1 -1 -1]
[ 0 1 1 1]]
'''
import math
print np.cos(math.pi/4)
'''
out:
0.707106781187#cos(pi/4)=sqrt(2)/2
'''
print arr1
print np.logical_not(arr1)
'''
out:
[[-4 -3 -2 -1]
[ 0 1 2 3]]

[[False False False False]
[ True False False False]]#非零元素认为是1,not 1 为 false
'''
print arr1,arr2
print np.add(arr1,arr1)#等价于arr1+arr1
print np.subtract(arr2,arr1)#等价于arr2-arr1
print np.multiply(arr1,arr1)#等价于arr1*arr1
print
'''
out:
[[-4 -3 -2 -1]
[ 0 1 2 3]]

[[ 8 9 10 11]
[12 13 14 15]]

[[-8 -6 -4 -2]
[ 0 2 4 6]]

[[12 12 12 12]
[12 12 12 12]]

[[16 9 4 1]
[ 0 1 4 9]]
'''
arr1 = np.array([2,3.2,4])
arr2 = np.array([2,2,3])

print np.divide(arr1,arr2)#等价于arr1/arr2
print np.floor_divide(arr1,arr2)#除完取整
'''
out:
[ 1. 1.6 1.33333333]
[ 1. 1. 1.]#
'''

!!注意本节内容是针对的元素级函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python numpy 通用函数