Python-Numpy多维数组--位操作, 字符串函数, 算术函数

一.位操作

1.bitwise_and

通过np.bitwise_and()函数对输入数组中的整数的二进制表示的相应位执行位与运算。

例子

import numpy as np
print '13 和 17 的二进制形式:'
a,b = 13,17
print bin(a), bin(b)
print '13 和 17 的位与:'
print np.bitwise_and(13, 17)
输出如下:
13 和 17 的二进制形式:0b1101 0b10001
13 和 17 的位与:1

你可以使用下表验证此输出。 考虑下面的位与真值表。

A B AND
1 1 1
1 0 0
0 1 0
0 0 0

2.bitwise_or

通过np.bitwise_or()函数对输入数组中的整数的二进制表示的相应位执行位或运算。

例子

import numpy as np
a,b = 13,17
print '13 和 17 的二进制形式:'
print bin(a), bin(b)
print '13 和 17 的位或:'
print np.bitwise_or(13, 17)
输出如下:
13 和 17 的二进制形式:0b1101 0b10001
13 和 17 的位或:29

你可以使用下表验证此输出。 考虑下面的位或真值表。

A B OR
1 1 1
1 0 1
0 1 1
0 0 0

3.invert

此函数计算输入数组中整数的位非结果。 对于有符号整数,返回补码。

例子

import numpy as np
print '13 的位反转,其中 ndarray 的 dtype 是 uint8:'
print np.invert(np.array([13], dtype = np.uint8))
# 比较 13 和 242 的二进制表示,我们发现了位的反转
print '13 的二进制表示:'
print np.binary_repr(13, width = 8)
print '242 的二进制表示:'
print np.binary_repr(242, width = 8)
输出如下:
13 的位反转,其中 ndarray 的 dtype 是 uint8:[242]
13 的二进制表示:00001101
242 的二进制表示:11110010
请注意,np.binary_repr()函数返回给定宽度中十进制数的二进制表示。

4.left_shift

numpy.left shift()函数将数组元素的二进制表示中的位向左移动到指定位置,右侧附加相等数量的 0。

import numpy as np
print '将 10 左移两位:'
print np.left_shift(10,2)
print '10 的二进制表示:'
print np.binary_repr(10, width = 8)
print '40 的二进制表示:'
print np.binary_repr(40, width = 8)
# '00001010' 中的两位移动到了左边,并在右边添加了两个 0。
输出如下:
将 10 左移两位:40
10 的二进制表示:00001010
40 的二进制表示:00101000

5.right_shift

numpy.right_shift()函数将数组元素的二进制表示中的位向右移动到指定位置,左侧附加相等数量的 0。

import numpy as np
print '将 40 右移两位:'
print np.right_shift(40,2)
print '40 的二进制表示:'
print np.binary_repr(40, width = 8)
print '10 的二进制表示:'
print np.binary_repr(10, width = 8)
# '00001010' 中的两位移动到了右边,并在左边添加了两个 0。
输出如下:
将 40 右移两位:10
40 的二进制表示:00101000
10 的二进制表示:00001010

二.Numpy - 字符串函数

1.numpy.char.add()函数执行按元素的字符串连接。

import numpy as np
print '连接两个字符串:'
print np.char.add(['hello'],[' xyz'])
print '连接示例:'
print np.char.add(['hello', 'hi'],[' abc', ' xyz'])
输出如下:
连接两个字符串:
['hello xyz']
连接示例:['hello abc' 'hi xyz']

2.numpy.char.multiply()这个函数执行多重连接。

import numpy as np
print np.char.multiply('Hello ',3)
输出如下:Hello Hello Hello 

3.numpy.char.center()此函数返回所需宽度的数组,以便输入字符串位于中心,并使用fillchar在左侧和右侧进行填充。

import numpy as np
# np.char.center(arr, width,fillchar)
print np.char.center('hello', 20,fillchar = '*')
输出如下:
*******hello********

4.numpy.char.capitalize()函数返回字符串的副本,其中第一个字母大写

import numpy as np
print np.char.capitalize('hello world')
输出如下:
Hello world 

5.numpy.char.title()返回输入字符串的按元素标题转换版本,其中每个单词的首字母都大写。

import numpy as np
print np.char.title('hello how are you?')\
输出如下:
Hello How Are You?

6.numpy.char.lower()函数返回一个数组,其元素转换为小写。它对每个元素调用str.lower

import numpy as np
print np.char.lower(['HELLO','WORLD'])
print np.char.lower('HELLO')
输出如下:
['hello' 'world']
hello

7.numpy.char.upper()函数返回一个数组,其元素转换为大写。它对每个元素调用str.upper

import numpy as np
print np.char.upper('hello')
print np.char.upper(['hello','world'])
输出如下:
HELLO
['HELLO' 'WORLD']

8.numpy.char.split()此函数返回输入字符串中的单词列表。 默认情况下,空格用作分隔符。 否则,指定的分隔符字符用于分割字符串。

import numpy as np
print np.char.split ('hello how are you?')
print np.char.split ('TutorialsPoint,Hyderabad,Telangana', sep = ',')
输出如下:
['hello', 'how', 'are', 'you?']
['TutorialsPoint', 'Hyderabad', 'Telangana']

9.numpy.char.splitlines()函数返回数组中元素的单词列表,以换行符分割。

import numpy as np
print np.char.splitlines('hello\nhow are you?')
print np.char.splitlines('hello\rhow are you?')
输出如下:
['hello', 'how are you?']
['hello', 'how are you?']
注意:'\n','\r','\r\n'都会用作换行符。

10.numpy.char.strip()函数返回数组的副本,其中元素移除了开头或结尾处的特定字符。

import numpy as np
print np.char.strip('ashok arora','a')
print np.char.strip(['arora','admin','java'],'a')
输出如下:
shok aror
['ror' 'dmin' 'jav']

11.numpy.char.join()这个函数返回一个字符串,其中单个字符由特定的分隔符连接

import numpy as np
print np.char.join(':','dmy')
print np.char.join([':','-'],['dmy','ymd'])
输出如下:
d:m:y
['d:m:y' 'y-m-d']

12.numpy.char.replace()这个函数返回字符串副本,其中所有字符序列的出现位置都被另一个给定的字符序列取代

import numpy as np
print np.char.replace ('He is a good boy', 'is', 'was')

13.numpy.char.decode()这个函数在给定的字符串中使用特定编码调用str.decode()

import numpy as np
a = np.char.encode('hello', 'cp500')
print a
print np.char.decode(a,'cp500')
输出如下:
\x88\x85\x93\x93\x96
hello

14.numpy.char.encode()此函数对数组中的每个元素调用str.encode函数。 默认编码是utf_8,可以使用标准 Python 库中的编解码器。

import numpy as np
a = np.char.encode('hello', 'cp500')
print a
输出如下:\x88\x85\x93\x93\x96

三.Numpy - 算数函数

1.三角函数

NumPy 拥有标准的三角函数,它为弧度制单位的给定角度返回三角函数比值。

示例

import numpy as np
a = np.array([0,30,45,60,90])
print '不同角度的正弦值:'
# 通过乘 pi/180 转化为弧度
print np.sin(a*np.pi/180)
print '数组中角度的余弦值:'
print np.cos(a*np.pi/180)
print '数组中角度的正切值:'
print np.tan(a*np.pi/180)
输出如下:
不同角度的正弦值:[ 0. 0.5 0.70710678 0.8660254 1. ]
数组中角度的余弦值:[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01.12323400e-17]
数组中角度的正切值:[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+001.63312394e+16]

arcsinarccos,和arctan函数返回给定角度的sincostan的反三角函数。 这些函数的结果可以通过numpy.degrees()函数通过将弧度制转换为角度制来验证。

示例

import numpy as np
a = np.array([0,30,45,60,90])
print '含有正弦值的数组:'
sin = np.sin(a*np.pi/180)
print sin
print '计算角度的反正弦,返回值以弧度为单位:'
inv = np.arcsin(sin)
print inv
print '通过转化为角度制来检查结果:'
print np.degrees(inv)
print 'arccos 和 arctan 函数行为类似:'
cos = np.cos(a*np.pi/180)
print cos
print '反余弦:'
inv = np.arccos(cos)
print inv
print '角度制单位:'
print np.degrees(inv)
print 'tan 函数:'
tan = np.tan(a*np.pi/180)
print tan
print '反正切:'
inv = np.arctan(tan)
print inv
print '角度制单位:'
print np.degrees(inv)
输出如下:
含有正弦值的数组:[ 0. 0.5 0.70710678 0.8660254 1. ]
计算角度的反正弦,返回值以弧度制为单位:[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
通过转化为角度制来检查结果:[ 0. 30. 45. 60. 90.]
arccos 和 arctan 函数行为类似:[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-016.12323400e-17]
反余弦:[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
角度制单位:[ 0. 30. 45. 60. 90.]
tan 函数:[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+001.63312394e+16]
反正切:[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
角度制单位:[ 0. 30. 45. 60. 90.]

2.舍入函数

(1)numpy.around()这个函数返回四舍五入到所需精度的值。 该函数接受以下参数。

numpy.around(a,decimals)

序号 参数及描述
1. a 输入数组
2. decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

示例

import numpy as np
a = np.array([1.0,5.55, 123, 0.567, 25.532])
print '原数组:'
print a
print '舍入后:'
print np.around(a)
print np.around(a, decimals = 1)
print np.around(a, decimals = -1)
输出如下:
原数组:
[ 1. 5.55 123. 0.567 25.532]
舍入后:
[ 1. 6. 123. 1. 26. ]
[ 1. 5.6 123. 0.6 25.5]
[ 0. 10. 120. 0. 30. ]

​​​​​​​(2)numpy.floor()​​​​​​​此函数返回不大于输入参数的最大整数。 即标量x 的下限是最大的整数i ,使得i <= x。 注意在Python中,向下取整总是从 0 舍入。

​​​​​​​示例

import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print '提供的数组:'
print a
print '修改后的数组:'
print np.floor(a)
输出如下:
提供的数组:[ -1.7 1.5 -0.2 0.6 10. ]
修改后的数组:[ -2. 1. -1. 0. 10.]

(3)numpy.ceil():函数返回输入值的上限,即,标量x的上限是最小的整数i ,使得i> = x

示例

import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print '提供的数组:'
print a
print '修改后的数组:'
print np.ceil(a)
输出如下:
提供的数组:[ -1.7 1.5 -0.2 0.6 10. ]
修改后的数组:[ -1. 2. -0. 1. 10.]

 

猜你喜欢

转载自blog.csdn.net/Odyssues_lee/article/details/85261908