numpy第二章索引课时八
第一题
import numpy as np
arr = np.arange(9).reshape(3, 3)
arr[:,[0,2]]=arr[:,[2,0]]
print(arr)
第二题
import numpy as np
arr = np.arange(9).reshape(3, 3)
arr[[0,1],:]=arr[[1,0],:]
print(arr)
第三题
import numpy as np
arr = np.arange(9).reshape(3, 3)
arr[[0,1,2],:]=arr[[2,1,0],:]
print(arr)
第四题
import numpy as np
arr = np.arange(9).reshape(3, 3)
arr[:,[0,1,2]]=arr[:,[2,1,0]]
print(arr)
Matplotlib课时三作业
第二题
- 散点图
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname='C:WindowsFontsmsyh.ttc')
fig = plt.figure(figsize=(10, 5))
spec = fig.add_gridspec(nrows=2, ncols=5,width_ratios=[1,1,1,1,1],height_ratios=[1,2])
fig.suptitle("散点图",size=15,fontproperties=my_font)
for i in range(2):
for j in range(5):
ax = fig.add_subplot(spec[i, j])
ax.scatter(np.random.randn(30), np.random.randn(30))
ax.set_title('第%d行,第%d列'%(i+1,j+1),fontproperties=my_font)
if i==1: ax.set_xlabel('横坐标',fontproperties=my_font)
if j==0: ax.set_ylabel('纵坐标',fontproperties=my_font)
plt.savefig("22")
fig.tight_layout()
plt.show()
2.边际分布图
Pandas
第一章习题
第一题
import numpy as np
M1 = np.random.rand(2,3)
M2 = np.random.rand(3,4)
res =[[sum(M1[i][k] * M2[k][j] for k in range(M1.shape[1]))
for j in range(M2.shape[1])] for i in range(M1.shape[0])]
print(res)
print((np.abs((M1@M2 - res) < 1e-15)).all() )
第二题
import numpy as np
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
B=np.empty((A.shape[0],A.shape[1]))
print('*'*100)
for i in range(3):
for j in range(3):
B[i][j]=A[i][j]*sum(1/A[i,0:])
print(B)
第三题
import numpy as np
np.random.seed(0)
A = np.random.randint(10, 20, (8, 5))
B= np.empty((A.shape[0],A.shape[1]))
A_sum=A.sum()
for i in range(8):
for j in range(5):
B[i][j]=(sum(A[:,j])*sum(A[i,:]))/A_sum
print('*'*100)
print(B)
x2=0
for i in range(8):
for j in range(5):
x2+=(((A[i][j]-B[i][j])**2)/B[i][j]).sum()
print(x2)
第四题
第五题
import numpy as np
a=list(input('请输入一个整数的Numpy数组:'))
count=1
b=np.array(a).astype("int")
print(b,type(b))
print('*'*100)
d=np.diff(b)
e=d[d==1]
print(e)
for i in range(len(e)):
if e[i]==1:
count+=1
else:
continue
print(count)



