arr0 = np.arange(10)
arr0.reshape(2,5)
arr1 = arr0[np.newaxis, :]
print(arr1)
print(arr1.shape)
arr2 = arr0[:, np.newaxis]
print(arr2)
print(arr2.shape)
arr3 = arr0[:, np.newaxis, np.newaxis]
print(arr3)
print(arr3.shape)
arr4 = arr3.squeeze()
arr4.shape
执行后,shape由原来的(10,1,1)变为了(10,),删除了无效的维度
a = np.array([[1,2,0],
[3,4,2]])
b = np.array([[6,7,4],
[8,0,7]])
# 别少括号,最外面括号是concatenate方法,里面的括号表示第一个参数是元组形式(axis默认值为0)
c = np.concatenate((a,b), axis = 0)
print('concat with axis 0: ', c)
d = np.concatenate((a,b), axis = 1)
print('concat with axis 1: ', d)
a.flatten()
np.arange(10)
np.arange(2,20,2)
np.arange(2,20,2,dtype=np.float32)
np.linspace(0,10,20)
np.logspace(0, 1, 5)
print('行向量:',np.r_[0:10:1])
print('列向量:',np.c_[0:10:1])
arr1 = np.zeros(3)
print(arr1)
print(arr1.shape)
arr2 = np.ones(3)
print(arr2)
print(arr2.shape)
arr3 = np.zeros((3,3))
print(arr3)
print(arr3.shape)
arr4 = np.ones((5,5))*8
print(arr4)
print(arr4.shape)
arr5 = np.ones((5,5),dtype=np.float32)*8
b = np.array([1,2,3,4])
c = np.zeros_like(b)
print(c)
d = np.ones_like(b)
print(d)
注:zeros_like产生的ndarray的shape和原来的shape一致
e = np.identity(5)
x = np.array([5, 5])
y = np.array([2, 2])
print('multiply: \n',np.multiply(x,y))
print('dot: \n',np.dot(x,y))
注:multiply是对应位置相乘,dot是矩阵运算(内积)。因此要求计算时x,y的维度相同
# 由于x,y维度不一样,dot会报错
x = np.array([5, 5])
x.shape=2,1
y = np.array([2, 2])
print('multiply: \n',np.multiply(x,y))
# print('dot: \n',np.dot(x,y))
x = np.array([5, 5])
x.shape=2, 1
y = np.array([2, 2])
y.shape=1, 2
print('multiply: \n',np.multiply(x,y))
print('dot(x,y): \n',np.dot(x,y))
print('dot(y,x): \n',np.dot(y,x))
a = np.array([1, 1, 1])
b = np.array([[1, 2, 3],
[4, 5, 6]])
print('a*b=',a*b)
注:这里自动进行了广播操作,将a的形状reshape成适当的shape
np.random.rand(10)
bb = np.random.randint(10, size=(5,4))
print(bb)
cc = np.random.randint(0, 10, 20)
print(cc)
np.random.rand()
miu = 0
sigma = 1
np.random.normal(miu,sigma,10)
# 可以人为设定保留小数的精度
np.set_printoptions(precision=2)
miu = 0
sigma = 1
np.random.normal(miu,sigma,10)
# 洗牌(每次shuffle的结果是不同的)
ddd = np.arange(10)
print(ddd)
np.random.shuffle(ddd)
print('after shuffle: ',ddd)
可以通过指定随机数种子来固定shuffle,使得每次shuffle结果相同
np.random.seed(1)
np.random.randint(0,10,20)
%%writefile test_doc.txt
1 2 3 4 6
5 8 3 2 9
# 使用python逐行读取
data = []
with open('test_doc.txt') as f:
for content in f:
tmp = content.split()
cur_data = [float(x) for x in tmp]
data.append(cur_data)
data = np.array(data)
print(data)
data1 = np.loadtxt('test_doc.txt')
print(data1)
loadtxt默认逗号为分隔符,因此如果文件分隔符为其他,需要在参数中指定
%%writefile test_doc2.txt
1,2,3,4,6
5,8,3,2,9
data2 = np.loadtxt('test_doc2.txt', delimiter = ',')
print(data2)
%%writefile test_doc2.txt
x,y,s,f,g
1,2,3,4,6
5,8,3,2,9
# skiprows表示去掉前几行,不是第几行(usecol函数略)
data3 = np.loadtxt('test_doc2.txt', delimiter = ',',skiprows=1)
print(data3)
# 自己写一个ndarray,保存成txt,指定分隔符,小数点(注意:下面这两个delimiter要一致)
ldk = np.array([[1,2,3],
[4,5,6]])
np.savetxt('test_doc2.txt',ldk,fmt='%.2f',delimiter=',')
tmp = np.loadtxt('test_doc2.txt',delimiter = ',')
tmp
# 自己写一个ndarray,保存成npy,然后读出来
ldk = np.array([[1,2,3],
[4,5,6]])
np.save('test1.npy',ldk)
data_content = np.load('test1.npy')
data_content
# 自己写一个ndarray,保存成npz,然后读出来
a = np.array([[1,2,3],
[4,5,6]])
b = np.arange(5)
np.savez('ldk.npz',cc = a, dd = b)
data = np.load('ldk.npz')
print(data.keys())
print('\n\n\n')
print(data['cc'])
print('\n\n\n')
print(data['dd'])