Numpy - 知识点总结(四)
程序员文章站
2022-03-10 22:50:27
...
一 、 位操作
bitwise_and:对数组元素执行位与操作
bitwise_or:对数组元素执行位或操作
invert:计算位非
left_shift:向左移动二进制表示的位
right_shift:向右移动二进制表示的位
import numpy as np
a = 3
b = 6
print('a的二进制表示为',bin(a))
print('b的二进制表示为',bin(b))
print('按位与',np.bitwise_and(a,b))
print('按位或',np.bitwise_or(a,b))
print('计算位非',np.invert(a))
print('a向左移动两位',np.left_shift(a,2))
print('b向右移动一位',np.right_shift(b,1))
a = np.arange(0,10)
b = np.arange(1,11)
print(np.bitwise_and(a,b))
二、字符串函数
add():返回两个str或Unicode数组的逐个字符串连接
import numpy as np
a = np.array('Hello')
b = np.array('World')
c = np.char.add(a,b)
print(c)
c = np.add([1,2],[3,4])
print(c)
c = np.char.add(['1','2'],['3','4'])
print(c)
multiply():返回按元素多重连接后的字符串
import numpy as np
a = np.array('Hello')
b = np.array('World')
c = np.char.multiply(a,3)
print(c)
c = np.multiply([1,2],[3,4])
print(c)
c = np.char.multiply(['1','2'],[3,4])
print(c)
center():返回给定字符串的副本,其中元素位于特定字符串的*
import numpy as np
a = np.array('Hello')
b = np.array('World')
c = np.char.center(a,10,'-')
print(c)
c = np.char.center(['1','2'],10,'-')
print(c)
capitalize():返回给定字符串的副本,其中只有第一个字符串第一个字母大写
import numpy as np
a = np.array('Hello')
b = np.array('World')
c = np.char.capitalize(a)
print(c)
c = np.char.capitalize(['1','2'])
print(c)
title():返回字符串的标题格式的副本
import numpy as np
a = np.array('Hello ')
b = np.array('world')
c = np.char.add(a,b)
print(c)
c = np.char.title(c)
print(c)
lower():返回一个数组,将数组中的字符转换为小写
upper():返回一个数组,将数组中的字符转换为大写
import numpy as np
a = np.array('Hello ')
b = np.array('world')
c = np.char.add(a,b)
print(c)
c = np.char.upper(c)
print(c)
c = np.char.lower(c)
print(c)
split():返回一个数组,将数组中的字符串按照分隔符进行分割
import numpy as np
a = np.array('Hello ')
b = np.array('world')
c = np.char.add(a,b)
print(c)
c = np.char.split(c,' ')
print(c)
splitlines():返回一个数组,按照换行符进行分割
strip():返回一个数组,去掉元素中开头个结尾的空格
join():返回一个字符串,他是序列中字符串的连接
replace():返回字符串的副本,其中所有字符串的出现位置都会被新字符串取代
decode():元素按照指定编码解码;
encode():元素按照指定的编码进行编码
三、算数运算
三角函数
sin()/cos()/tan():三角函数,使用的是弧度进行计算
import numpy as np
A = np.array([0,30,45,60,90])
b = np.sin(A*np.pi/180)
print('sinA',b)
b = np.cos(A*np.pi/180)
print('cosA',b)
b = np.tan(A*np.pi/180)
print('tanA',b)
arcsin()/arccos()/arctan():反三角函数,计算出来的是弧度
import numpy as np
A = np.array([0,30,45,60,90])
b = np.sin(A*np.pi/180)
print('sinA',b)
B = np.arcsin(b)
print('arcsinB',B*180/np.pi)
b = np.cos(A*np.pi/180)
print('cosA',b)
B = np.arccos(b)
print('arccosB',B*180/np.pi)
b = np.tan(A*np.pi/180)
print('tanA',b)
B = np.arctan(b)
print('arctanB',B*180/np.pi)
numpy.degrees():可以将弧度转换为角度
import numpy as np
A = np.array([0,30,45,60,90])
b = np.sin(A*np.pi/180)
print('sinA',b)
B = np.arcsin(b)
print('arcsinB',np.degrees(B))
b = np.cos(A*np.pi/180)
print('cosA',b)
B = np.arccos(b)
print('arccosB',np.degrees(B))
b = np.tan(A*np.pi/180)
print('tanA',b)
B = np.arctan(b)
print('arctanB',np.degrees(B))
numpy.around():将函数四舍五入到所需要的精度值
import numpy as np
A = np.array([0.55,30.051,45.004,60.0005,90.00004])
B = np.round(A,decimals=0)
print(B)
B = np.round(A,decimals=1)
print(B)
B = np.round(A,decimals=2)
print(B)
B = np.round(A,decimals=3)
print(B)
B = np.round(A,decimals=4)
print(B)
B = np.around(A,decimals=0)
print(B)
B = np.around(A,decimals=1)
print(B)
B = np.around(A,decimals=2)
print(B)
B = np.around(A,decimals=3)
print(B)
B = np.around(A,decimals=4)
print(B)
numpy.floor():向下取整
numpy.ceil():向上取整
import numpy as np
A = np.array([0.55,30.051,45.004,60.0005,90.00004])
B = np.floor(A)
print(B)
B = np.ceil(A)
print(B)
上一篇: swift中类和结构体的区别