导入相关库
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1
s1=np.zeros(shape=10)
s1
![](https://img-blog.csdnimg.cn/47b041a1f1e143a698c2e7ba20154aca.png)
s1[4]=1
s1
![](https://img-blog.csdnimg.cn/0aa31da3c20c4d468bb2556c7cc3ab23.png)
创建一个元素为从10到49的ndarray对象
np.random.randint(10,50,size=10)
![](https://img-blog.csdnimg.cn/05bb4c50f339437591e564c40169b63d.png)
np.linspace(10,49,10)
![](https://img-blog.csdnimg.cn/837beb51bec54e72af3eac8e72d666fe.png)
a=np.arange(10,50)
a
![](https://img-blog.csdnimg.cn/c3243173bd6d46de89ce6e6ce8f6493e.png)
使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素
a[::-1]
![](https://img-blog.csdnimg.cn/43e8021b0b2a4a2994149bd04f1269da.png)
a4=np.random.random(size=(10,10))
a4
![](https://img-blog.csdnimg.cn/b2b6ad4b0d0a48deb5839d82868cffb4.png)
zmin,zmax=a4.min(),a4.max()
zmin,zmax
![](https://img-blog.csdnimg.cn/c4b52a46ad574779949f3a72deef63c7.png)
创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0
import numpy as np
nd=np.zeros(shape = (10,10),dtype = np.int8)
nd[[0,9]] = 1
nd[:,[0,9]] = 1
nd
![](https://img-blog.csdnimg.cn/7f7427ecb7c1481caaaf40073768ebfe.png)
a5=np.ones((10,10))
a5
![](https://img-blog.csdnimg.cn/6851a9a995754715a8c0790420d8b264.png)
a5[1:-1,1:-1]=0
a5
![在这里插入图片描述](https://img-blog.csdnimg.cn/33225973dd1a4ae7b41370603f0ef823.png)
创建一个每一行都是从0到4的5*5矩阵
l=[0,1,2,3,4]
nd=np.array(l*5)
nd.reshape(5,5)
![](https://img-blog.csdnimg.cn/f8e99112d1274951b9b299cbf7c62f2a.png)
nd6_1=np.arange(0,5,1)
nd6_1
![](https://img-blog.csdnimg.cn/d4b28dfe144a4a8ab91f3144c965dc18.png)
nd6=np.arange(0,25).reshape(5,5)
nd6
![](https://img-blog.csdnimg.cn/2d578f0324094377b2ec5eb22dc91209.png)
nd6[0:5]=nd6_1
nd6
![在这里插入图片描述](https://img-blog.csdnimg.cn/a8f28996bd26498ba21bef0ef6749403.png)
创建一个范围在(0,1)之间的长度为12的等差数列
np.linspace(0,1,12)
![](https://img-blog.csdnimg.cn/7c6f24b2fb4046dc9d4551195894aa17.png)
创建一个长度为10的随机数组并排序
a8=np.random.random(10)
a8
![在这里插入图片描述](https://img-blog.csdnimg.cn/af341ef70cad42bcbb039ef1bcfb7bab.png)
np.sort(a8)
![](https://img-blog.csdnimg.cn/76ff26623bd9457ca14b13b9e2ea9e3b.png)
a8.argsort()
![](https://img-blog.csdnimg.cn/1d606a1c29404e4994b69853dcd04fc7.png)
a8[a8.argsort()]
![](https://img-blog.csdnimg.cn/8e9ea48312ff4ff08d5097ff5c46869d.png)
创建一个长度为10的随机数组并将最大值替换为0
nd=np.random.randint(0,10,size=10)
display(nd)
index_max=nd.argmax()
![](https://img-blog.csdnimg.cn/411516bf207b41dca682699f27a3825d.png)
nd[index_max]
![](https://img-blog.csdnimg.cn/193c3c4711474fc9bf0b26c8e813078b.png)
all_index_max=np.argwhere(nd==nd[index_max]).reshape(-1)
all_index_max
![](https://img-blog.csdnimg.cn/194901315072409c8112ca03debcfa55.png)
nd[all_index_max]=-100
nd
![](https://img-blog.csdnimg.cn/69301ac5fc92475a81aa399541479644.png)
根据第3列来对一个5*5矩阵排序
n10=np.random.randint(0,100,size=(5,5))
n10
![](https://img-blog.csdnimg.cn/36b48bb2028e4bd2b9a7bf6e537150e3.png)
给定数组[1, 2, 3, 4, 5],如何得到在这个数组的每个元素之间插入3个0后的新数组
nd1=np.arange(1,6)
nd2=np.zeros(shape=17,dtype=int)
nd2[::4]=nd1
nd2
![](https://img-blog.csdnimg.cn/e988b158c9334be1a3e32bba70693f0c.png)
n12=np.array([1,2,3,4,5]).reshape(5,1)
n12
![](https://img-blog.csdnimg.cn/e07fbb1838d143bab42a5087fcfb0f39.png)
n12_1=np.zeros((5,3))
n12_1
![](https://img-blog.csdnimg.cn/4b52b82b6624468e867d38e1537b2c2a.png)
n12=np.concatenate([n12,n12_1],axis=1)
n12
![](https://img-blog.csdnimg.cn/d96dd6b7d22747f6a69f465e69e9e523.png)