numpy
库并简写为np
import numpy as np
numpy
的版本和配置说明import numpy as np
print(np.__version__)
np.show_config()
输出:
1.21.3
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
...
import numpy as np
np.empty(10)
import numpy as np
data = np.random.randn(2, 2)
print("%d bytes" % (data.size * data.itemsize))
输出:
32 bytes
numpy
中add
函数的说明文档import numpy as np
np.info(np.add)
import numpy as np
data = np.zeros(10)
data[4] = 5
print(data)
import numpy as np
data = np.arange(10,50)
print(data)
import numpy as np
data = np.arange(1, 11)
data = data[::-1]
print(data)
import numpy as np
data = np.arange(9).reshape(3,3)
print(data)
[1,2,0,0,4,0]
中非0元素的位置索引import numpy as np
data = np.array([1, 2, 0, 0, 4, 0])
nz = np.nonzero(data)
print(nz)
import numpy as np
data = np.array([1, 2, 0, 0, 4, 0])
for x in np.arange(0, len(data)):
if data[x] != 0:
print(x)
import numpy as np
data = np.eye(3)
import numpy as np
data = np.random.random((3, 3, 3))
print(data)
import numpy as np
data = np.random.random((10, 10))
print(np.max(data))
print(np.min(data))
import numpy as np
data = np.random.random(10)
print(np.mean(data))
import numpy as np
data = np.zeros((3,3))
data[0, :] = 1 #第1行
data[-1,:] = 1 #最后1行
data[:, 0] = 1 #第1列
data[:,-1] = 1 #最后1列
print(data)
import numpy as np
data = np.ones((5, 5))
data = np.pad(data, pad_width=1, mode='constant', constant_values=0)
0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
0.3 == 3 * 0.1
输出:
#NaN = not a number, inf = infinity
nan
False
False
nan
False
import numpy as np
data = np.diag(1+np.arange(4), k=-1)
print(data)
import numpy as np
data = np.zeros((8, 8), dtype=int)
data[1::2, ::2] = 1
data[::2, 1::2] = 1
print(data)
import numpy as np
print(np.unravel_index(100,(6,7,8)))
tile
函数去创建一个
8
×
8
8 \times 8
8×8的棋盘样式矩阵import numpy as np
data = np.tile(np.array([[0, 1], [1, 0]]), (4, 4))
print(data)
import numpy as np
data = np.random.random((5,5))
data_max, data_min = data.max(), data.min();
data = (data-data_min)/(data_max-data_min);
print(data)
dtype
import numpy as np
color = np.dtype([("r", np.ubyte, (1,)),
("g", np.ubyte, (1,)),
("b", np.ubyte, (1,)),
("a", np.ubyte, (1,))])
print(color)
import numpy as np
data_1 = np.random.randn(5, 3)
data_2 = np.random.randn(3, 2)
data = np.dot(data_1, data_2)
print(data)
import numpy as np
data = np.arange(11)
data[(data>3) & (data<8)] *= -1
print(data)
26.下面脚本运行后的结果是什么?
print(sum(range(5),-1)) #对提供的可迭代对象进行迭代,对值求和,然后加-1
输出:
9
from numpy import *
print(sum(range(5),-1)) #将提供的列表所有值求和
输出
10
import numpy as np
Z = np.arange(1, 6);
print(Z**Z)
print(2 << Z >> 2)
print(Z <- Z)
print(1j*Z)
print(Z/1/1)
print(Z<Z>Z)
import numpy as np
print(np.array(0) / np.array(0))
print(np.array(0) // np.array(0))
print(np.array([np.nan]).astype(int).astype(float))
import numpy as np
# 从均匀[0,1)分布中抽取样本
data = np.random.uniform(-10,+10,10)
print(np.copysign(np.ceil(np.abs(data)), data))
import numpy as np
data_1 = np.arange(1,6)
data_2 = np.arange(3,8)
print(np.intersect1d(data_1, data_2))
import numpy as np
defaults = np.seterr(all="ignore")
data = np.ones(1) / 0
import numpy as np
print(np.sqrt(-1) == np.emath.sqrt(-1))
import numpy as np
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
today = np.datetime64('today', 'D')
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
print("Yesterday is " + str(yesterday))
print("Today is " + str(today))
print("Tomorrow is "+ str(tomorrow))
import numpy as np
data = np.arange('2016-07', '2016-08', dtype='datetime64[D]')
print(data)
import numpy as np
A = np.ones(3)*1
B = np.ones(3)*2
C = np.ones(3)*3
np.add(A,B,out=B)
np.divide(A,2,out=A)
np.negative(A,out=A)
np.multiply(A,B,out=A)
import numpy as np
data = np.random.uniform(0, 10, 10)
# 减去小数位
print(data-data % 1)
# 向下取整
print(np.floor(data))
# 向上取整后减1
print(np.ceil(data)-1)
# 将数据格式变为int
print(data.astype(int))
# 截断函数trunc,丢弃带符号数的小数部分
print(np.trunc(data))
import numpy as np
data = np.zeros((5, 5))
data += np.arange(0, 5)
print(data)
import numpy as np
def temp():
return np.arange(0,10)
data = temp()
print(data)
import numpy as np
# np.linspace()在指定的间隔内返回均匀间隔的数字
# endpoint设置将不包括1
# [1:]将0剔除
data = np.linspace(0, 1, 11, endpoint=False)[1:]
print(data)
import numpy as np
data= np.random.randn(10)
data.sort()
print(data)
np.sum
更快的方式对其求和?import numpy as np
data = np.arange(10)
print(np.add.reduce(data))
import numpy as np
A = np.arange(1, 5)
B = np.arange(3, 7)
# np.allclose比较两个array是不是每一元素都相等
equal = np.allclose(A, B)
print(equal)
import numpy as np
Z = np.zeros(10)
Z.flags.writeable = False
Z[0] = 1
import numpy as np
Z = np.random.random((10,2))
X,Y = Z[:,0], Z[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)
import numpy as np
data = np.arange(0, 8)
data_max = data.max()
data[data == data_max] = 1
print(data)
或
import numpy as np
data = np.arange(0, 8)
data[data.argmax()] = 0
print(data)
import numpy as np
data = np.zeros((5, 5), [('x', float), ('y', float)])
data['x'], data['y'] = np.meshgrid(np.linspace(0, 1, 5),
np.linspace(0, 1, 5))
print(data)
import numpy as np
X = np.arange(8)
Y = X + 0.5
C = 1.0 / np.subtract.outer(X, Y)
print(np.linalg.det(C))
import numpy as np
for dtype in [np.int8, np.int32, np.int64]:
print(np.iinfo(dtype).min)
print(np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
print(np.finfo(dtype).min)
print(np.finfo(dtype).max)
print(np.finfo(dtype).eps)
np.set_printoptions(threshold=np.nan)
data = np.zeros((16,16))
print(data)
data = np.arange(100)
v = np.random.uniform(0,100)
index = (np.abs(data-v)).argmin()
print (data[index])