机器学习

This commit is contained in:
estomm
2020-09-29 10:05:30 +08:00
parent 501c49c1de
commit bb81ba3fb3
63 changed files with 1429 additions and 917 deletions

View File

@@ -1,279 +0,0 @@
# 面向对象的绘图方式
## 配置参数
* axex: 设置坐标轴边界和表面的颜色、坐标刻度值大小和网格的显示
* figure: 控制dpi、边界颜色、图形大小、和子区( subplot)设置
* font: 字体集font family、字体大小和样式设置
* grid: 设置网格颜色和线性
* legend: 设置图例和其中的文本的显示
* line: 设置线条(颜色、线型、宽度等)和标记
* patch: 是填充2D空间的图形对象如多边形和圆。控制线宽、颜色和抗锯齿设置等。
* savefig: 可以对保存的图形进行单独设置。例如,设置渲染的文件的背景为白色。
* verbose: 设置matplotlib在执行期间信息输出如silent、helpful、debug和debug-annoying。
* xticks和yticks: 为x,y轴的主刻度和次刻度设置颜色、大小、方向以及标签大小。
## 线条风格
线条风格linestyle或ls | 描述
----|---
- |实线
: |虚线
|破折线
None, , |什么都不画
-. |点划线
## 线条标记
标记maker | 描述
----|----
o |圆圈
. |点
D |菱形
s |正方形
h |六边形1
* |星号
H |六边形2
d |小菱形
_ | 水平线
v |一角朝下的三角形
8 |八边形
< | 一角朝左的三角形
p |五边形
> |一角朝右的三角形
, |像素
^ | 一角朝上的三角形
+ | 加号
\ |竖线
None,, |无
x | X
## 颜色
别名 | 颜色
---|---
b | 蓝色
g |绿色
r |红色
y |黄色
c |青色
k |黑色
m |洋红色
w |白色
## 绘图步骤
```py
#使用numpy产生数据
x=np.arange(-5,5,0.1)
y=x*3
#创建窗口、子图
#方法1先创建窗口再创建子图。一定绘制
fig = plt.figure(num=1, figsize=(15, 8),dpi=80) #开启一个窗口,同时设置大小,分辨率
ax1 = fig.add_subplot(2,1,1) #通过fig添加子图参数行数列数第几个。
ax2 = fig.add_subplot(2,1,2) #通过fig添加子图参数行数列数第几个。
print(fig,ax1,ax2)
#方法2一次性创建窗口和多个子图。空白不绘制
fig,axarr = plt.subplots(4,1) #开一个新窗口并添加4个子图返回子图数组
ax1 = axarr[0] #通过子图数组获取一个子图
print(fig,ax1)
#方法3一次性创建窗口和一个子图。空白不绘制
ax1 = plt.subplot(1,1,1,facecolor='white') #开一个新窗口创建1个子图。facecolor设置背景颜色
print(ax1)
#获取对窗口的引用,适用于上面三种方法
# fig = plt.gcf() #获得当前figure
# fig=ax1.figure #获得指定子图所属窗口
# fig.subplots_adjust(left=0) #设置窗口左内边距为0即左边留白为0。
#设置子图的基本元素
ax1.set_title('python-drawing') #设置图体plt.title
ax1.set_xlabel('x-name') #设置x轴名称,plt.xlabel
ax1.set_ylabel('y-name') #设置y轴名称,plt.ylabel
plt.axis([-6,6,-10,10]) #设置横纵坐标轴范围,这个在子图中被分解为下面两个函数
ax1.set_xlim(-5,5) #设置横轴范围,会覆盖上面的横坐标,plt.xlim
ax1.set_ylim(-10,10) #设置纵轴范围,会覆盖上面的纵坐标,plt.ylim
xmajorLocator = MultipleLocator(2) #定义横向主刻度标签的刻度差为2的倍数。就是隔几个刻度才显示一个标签文本
ymajorLocator = MultipleLocator(3) #定义纵向主刻度标签的刻度差为3的倍数。就是隔几个刻度才显示一个标签文本
ax1.xaxis.set_major_locator(xmajorLocator) #x轴 应用定义的横向主刻度格式。如果不应用将采用默认刻度格式
ax1.yaxis.set_major_locator(ymajorLocator) #y轴 应用定义的纵向主刻度格式。如果不应用将采用默认刻度格式
ax1.xaxis.grid(True, which='major') #x坐标轴的网格使用定义的主刻度格式
ax1.yaxis.grid(True, which='major') #x坐标轴的网格使用定义的主刻度格式
ax1.set_xticks([]) #去除坐标轴刻度
ax1.set_xticks((-5,-3,-1,1,3,5)) #设置坐标轴刻度
ax1.set_xticklabels(labels=['x1','x2','x3','x4','x5'],rotation=-30,fontsize='small') #设置刻度的显示文本rotation旋转角度fontsize字体大小
plot1=ax1.plot(x,y,marker='o',color='g',label='legend1') #点图marker图标
plot2=ax1.plot(x,y,linestyle='--',alpha=0.5,color='r',label='legend2') #线图linestyle线性alpha透明度color颜色label图例文本
ax1.legend(loc='upper left') #显示图例,plt.legend()
ax1.text(2.8, 7, r'y=3*x') #指定位置显示文字,plt.text()
ax1.annotate('important point', xy=(2, 6), xytext=(3, 1.5), #添加标注,参数:注释文本、指向点、文字位置、箭头属性
arrowprops=dict(facecolor='black', shrink=0.05),
)
#显示网格。which参数的值为major(只绘制大刻度)、minor(只绘制小刻度)、both默认值为major。axis为'x','y','both'
ax1.grid(b=True,which='major',axis='both',alpha= 0.5,color='skyblue',linestyle='--',linewidth=2)
axes1 = plt.axes([.2, .3, .1, .1], facecolor='y') #在当前窗口添加一个子图rect=[左, 下, 宽, 高],是使用的绝对布局,不和以存在窗口挤占空间
axes1.plot(x,y) #在子图上画图
plt.savefig('aa.jpg',dpi=400,bbox_inches='tight') #savefig保存图片dpi分辨率bbox_inches子图周边白色空间的大小
plt.show() #打开窗口对于方法1创建在窗口一定绘制对于方法2方法3创建的窗口若坐标系全部空白则不绘制
```
## plot属性
```py
属性 值类型
alpha 浮点值
animated [True / False]
antialiased or aa [True / False]
clip_box matplotlib.transform.Bbox 实例
clip_on [True / False]
clip_path Path 实例 Transform以及Patch实例
color or c 任何 matplotlib 颜色
contains 命中测试函数
dash_capstyle ['butt' / 'round' / 'projecting']
dash_joinstyle ['miter' / 'round' / 'bevel']
dashes 以点为单位的连接/断开墨水序列
data (np.array xdata, np.array ydata)
figure matplotlib.figure.Figure 实例
label 任何字符串
linestyle or ls [ '-' / '--' / '-.' / ':' / 'steps' / ...]
linewidth or lw 以点为单位的浮点值
lod [True / False]
marker [ '+' / ',' / '.' / '1' / '2' / '3' / '4' ]
markeredgecolor or mec 任何 matplotlib 颜色
markeredgewidth or mew 以点为单位的浮点值
markerfacecolor or mfc 任何 matplotlib 颜色
markersize or ms 浮点值
markevery [ None / 整数值 / (startind, stride) ]
picker 用于交互式线条选择
pickradius 线条的拾取选择半径
solid_capstyle ['butt' / 'round' / 'projecting']
solid_joinstyle ['miter' / 'round' / 'bevel']
transform matplotlib.transforms.Transform 实例
visible [True / False]
xdata np.array
ydata np.array
zorder 任何数值
```
## 多图绘制
```py
#一个窗口,多个图,多条数据
sub1=plt.subplot(211,facecolor=(0.1843,0.3098,0.3098)) #将窗口分成2行1列在第1个作图并设置背景色
sub2=plt.subplot(212) #将窗口分成2行1列在第2个作图
sub1.plot(x,y) #绘制子图
sub2.plot(x,y) #绘制子图
axes1 = plt.axes([.2, .3, .1, .1], facecolor='y') #添加一个子坐标系rect=[左, 下, 宽, 高]
plt.plot(x,y) #绘制子坐标系,
axes2 = plt.axes([0.7, .2, .1, .1], facecolor='y') #添加一个子坐标系rect=[左, 下, 宽, 高]
plt.plot(x,y)
plt.show()
```
## 极坐标
```py
fig = plt.figure(2) #新开一个窗口
ax1 = fig.add_subplot(1,2,1,polar=True) #启动一个极坐标子图
theta=np.arange(0,2*np.pi,0.02) #角度数列值
ax1.plot(theta,2*np.ones_like(theta),lw=2) #画图参数角度半径lw线宽
ax1.plot(theta,theta/6,linestyle='--',lw=2) #画图参数角度半径linestyle样式lw线宽
ax2 = fig.add_subplot(1,2,2,polar=True) #启动一个极坐标子图
ax2.plot(theta,np.cos(5*theta),linestyle='--',lw=2)
ax2.plot(theta,2*np.cos(4*theta),lw=2)
ax2.set_rgrids(np.arange(0.2,2,0.2),angle=45) #距离网格轴,轴线刻度和显示位置
ax2.set_thetagrids([0,45,90]) #角度网格轴范围0-360度
plt.show()
```
## 柱状图
```py
plt.figure(3)
x_index = np.arange(5) #柱的索引
x_data = ('A', 'B', 'C', 'D', 'E')
y1_data = (20, 35, 30, 35, 27)
y2_data = (25, 32, 34, 20, 25)
bar_width = 0.35 #定义一个数字代表每个独立柱的宽度
rects1 = plt.bar(x_index, y1_data, width=bar_width,alpha=0.4, color='b',label='legend1') #参数:左偏移、高度、柱宽、透明度、颜色、图例
rects2 = plt.bar(x_index + bar_width, y2_data, width=bar_width,alpha=0.5,color='r',label='legend2') #参数:左偏移、高度、柱宽、透明度、颜色、图例
#关于左偏移,不用关心每根柱的中心不中心,因为只要把刻度线设置在柱的中间就可以了
plt.xticks(x_index + bar_width/2, x_data) #x轴刻度线
plt.legend() #显示图例
plt.tight_layout() #自动控制图像外部边缘,此方法不能够很好的控制图像间的间隔
plt.show()
```
## 直方图
```py
fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6)) #在窗口上添加2个子图
sigma = 1 #标准差
mean = 0 #均值
x=mean+sigma*np.random.randn(10000) #正态分布随机数
ax0.hist(x,bins=40,normed=False,histtype='bar',facecolor='yellowgreen',alpha=0.75) #normed是否归一化histtype直方图类型facecolor颜色alpha透明度
ax1.hist(x,bins=20,normed=1,histtype='bar',facecolor='pink',alpha=0.75,cumulative=True,rwidth=0.8) #bins柱子的个数,cumulative是否计算累加分布rwidth柱子宽度
plt.show() #所有窗口运行
```
## 散点图
```py
fig = plt.figure(4) #添加一个窗口
ax =fig.add_subplot(1,1,1) #在窗口上添加一个子图
x=np.random.random(100) #产生随机数组
y=np.random.random(100) #产生随机数组
ax.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='none') #x横坐标y纵坐标s图像大小c颜色marker图片lw图像边框宽度
plt.show() #所有窗口运行
```
## 三维图
```py
fig = plt.figure(5)
ax=fig.add_subplot(1,1,1,projection='3d') #绘制三维图
x,y=np.mgrid[-2:2:20j,-2:2:20j] #获取x轴数据y轴数据
z=x*np.exp(-x**2-y**2) #获取z轴数据
ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8) #绘制三维图表面
ax.set_xlabel('x-name') #x轴名称
ax.set_ylabel('y-name') #y轴名称
ax.set_zlabel('z-name') #z轴名称
plt.show()
```
## 集合图形
```py
fig = plt.figure(6) #创建一个窗口
ax=fig.add_subplot(1,1,1) #添加一个子图
rect1 = plt.Rectangle((0.1,0.2),0.2,0.3,color='r') #创建一个矩形,参数:(x,y),width,height
circ1 = plt.Circle((0.7,0.2),0.15,color='r',alpha=0.3) #创建一个椭圆,参数:中心点,半径,默认这个圆形会跟随窗口大小进行长宽压缩
pgon1 = plt.Polygon([[0.45,0.45],[0.65,0.6],[0.2,0.6]]) #创建一个多边形,参数:每个顶点坐标
ax.add_patch(rect1) #将形状添加到子图上
ax.add_patch(circ1) #将形状添加到子图上
ax.add_patch(pgon1) #将形状添加到子图上
fig.canvas.draw() #子图绘制
plt.show()
```

View File

@@ -0,0 +1,359 @@
# 面向对象的绘图方式
> 在这里的实例中,面向对象的方法与命令行的方式进行了混用,不提倡。首先运用好交互式命令行的方式进行绘图。
## 配置参数
* axex: 设置坐标轴边界和表面的颜色、坐标刻度值大小和网格的显示
* figure: 控制dpi、边界颜色、图形大小、和子区( subplot)设置
* font: 字体集font family、字体大小和样式设置
* grid: 设置网格颜色和线性
* legend: 设置图例和其中的文本的显示
* line: 设置线条(颜色、线型、宽度等)和标记
* patch: 是填充2D空间的图形对象如多边形和圆。控制线宽、颜色和抗锯齿设置等。
* savefig: 可以对保存的图形进行单独设置。例如,设置渲染的文件的背景为白色。
* verbose: 设置matplotlib在执行期间信息输出如silent、helpful、debug和debug-annoying。
* xticks和yticks: 为x,y轴的主刻度和次刻度设置颜色、大小、方向以及标签大小。
## 线条风格
线条风格linestyle或ls | 描述
----|---
- |实线
: |虚线
|破折线
None, , |什么都不画
-. |点划线
## 线条标记
标记maker | 描述
----|----
o |圆圈
. |点
D |菱形
s |正方形
h |六边形1
* |星号
H |六边形2
d |小菱形
_ | 水平线
v |一角朝下的三角形
8 |八边形
< | 一角朝左的三角形
p |五边形
> |一角朝右的三角形
, |像素
^ | 一角朝上的三角形
+ | 加号
\ |竖线
None,, |无
x | X
## 颜色
别名 | 颜色
---|---
b | 蓝色
g |绿色
r |红色
y |黄色
c |青色
k |黑色
m |洋红色
w |白色
## 绘图步骤
```py
#使用numpy产生数据
x=np.arange(-5,5,0.1)
y=x*3
#创建窗口、子图
#方法1先创建窗口再创建子图。一定绘制
fig = plt.figure(num=1, figsize=(15, 8),dpi=80)
#开启一个窗口,同时设置大小,分辨率
ax1 = fig.add_subplot(2,1,1)
#通过fig添加子图参数行数列数第几个。
ax2 = fig.add_subplot(2,1,2)
#通过fig添加子图参数行数列数第几个。
print(fig,ax1,ax2)
#方法2一次性创建窗口和多个子图。空白不绘制
fig,axarr = plt.subplots(4,1)
#开一个新窗口并添加4个子图返回子图数组
ax1 = axarr[0]
#通过子图数组获取一个子图
print(fig,ax1)
#方法3一次性创建窗口和一个子图。空白不绘制
ax1 = plt.subplot(1,1,1,facecolor='white')
#开一个新窗口创建1个子图。facecolor设置背景颜色
print(ax1)
#获取对窗口的引用,适用于上面三种方法
# fig = plt.gcf() #获得当前figure
# fig=ax1.figure #获得指定子图所属窗口
# fig.subplots_adjust(left=0)
#设置窗口左内边距为0即左边留白为0。
#设置子图的基本元素
ax1.set_title('python-drawing')
#设置图体plt.title
ax1.set_xlabel('x-name')
#设置x轴名称,plt.xlabel
ax1.set_ylabel('y-name')
#设置y轴名称,plt.ylabel
plt.axis([-6,6,-10,10])
#设置横纵坐标轴范围,这个在子图中被分解为下面两个函数
ax1.set_xlim(-5,5)
#设置横轴范围,会覆盖上面的横坐标,plt.xlim
ax1.set_ylim(-10,10)
#设置纵轴范围,会覆盖上面的纵坐标,plt.ylim
xmajorLocator = MultipleLocator(2)
#定义横向主刻度标签的刻度差为2的倍数。就是隔几个刻度才显示一个标签文本
ymajorLocator = MultipleLocator(3)
#定义纵向主刻度标签的刻度差为3的倍数。就是隔几个刻度才显示一个标签文本
ax1.xaxis.set_major_locator(xmajorLocator)
#x轴 应用定义的横向主刻度格式。如果不应用将采用默认刻度格式
ax1.yaxis.set_major_locator(ymajorLocator)
#y轴 应用定义的纵向主刻度格式。如果不应用将采用默认刻度格式
ax1.xaxis.grid(True, which='major')
#x坐标轴的网格使用定义的主刻度格式
ax1.yaxis.grid(True, which='major')
#x坐标轴的网格使用定义的主刻度格式
ax1.set_xticks([])
#去除坐标轴刻度
ax1.set_xticks((-5,-3,-1,1,3,5))
#设置坐标轴刻度
ax1.set_xticklabels(labels=['x1','x2','x3','x4','x5'],rotation=-30,fontsize='small')
#设置刻度的显示文本rotation旋转角度fontsize字体大小
plot1=ax1.plot(x,y,marker='o',color='g',label='legend1')
#点图marker图标
plot2=ax1.plot(x,y,linestyle='--',alpha=0.5,color='r',label='legend2')
#线图linestyle线性alpha透明度color颜色label图例文本
ax1.legend(loc='upper left')
#显示图例,plt.legend()
ax1.text(2.8, 7, r'y=3*x')
#指定位置显示文字,plt.text()
ax1.annotate('important point', xy=(2, 6), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05),)
#添加标注,参数:注释文本、指向点、文字位置、箭头属性
#显示网格。which参数的值为major(只绘制大刻度)、minor(只绘制小刻度)、both默认值为major。axis为'x','y','both'
ax1.grid(b=True,which='major',axis='both',alpha= 0.5,color='skyblue',linestyle='--',linewidth=2)
axes1 = plt.axes([.2, .3, .1, .1], facecolor='y')
#在当前窗口添加一个子图rect=[左, 下, 宽, 高],是使用的绝对布局,不和以存在窗口挤占空间
axes1.plot(x,y)
#在子图上画图
plt.savefig('aa.jpg',dpi=400,bbox_inches='tight')
#savefig保存图片dpi分辨率bbox_inches子图周边白色空间的大小
plt.show()
#打开窗口对于方法1创建在窗口一定绘制对于方法2方法3创建的窗口若坐标系全部空白则不绘制
```
## plot属性
```py
属性 值类型
alpha 浮点值
animated [True / False]
antialiased or aa [True / False]
clip_box matplotlib.transform.Bbox 实例
clip_on [True / False]
clip_path Path 实例 Transform以及Patch实例
color or c 任何 matplotlib 颜色
contains 命中测试函数
dash_capstyle ['butt' / 'round' / 'projecting']
dash_joinstyle ['miter' / 'round' / 'bevel']
dashes 以点为单位的连接/断开墨水序列
data (np.array xdata, np.array ydata)
figure matplotlib.figure.Figure 实例
label 任何字符串
linestyle or ls [ '-' / '--' / '-.' / ':' / 'steps' / ...]
linewidth or lw 以点为单位的浮点值
lod [True / False]
marker [ '+' / ',' / '.' / '1' / '2' / '3' / '4' ]
markeredgecolor or mec 任何 matplotlib 颜色
markeredgewidth or mew 以点为单位的浮点值
markerfacecolor or mfc 任何 matplotlib 颜色
markersize or ms 浮点值
markevery [ None / 整数值 / (startind, stride) ]
picker 用于交互式线条选择
pickradius 线条的拾取选择半径
solid_capstyle ['butt' / 'round' / 'projecting']
solid_joinstyle ['miter' / 'round' / 'bevel']
transform matplotlib.transforms.Transform 实例
visible [True / False]
xdata np.array
ydata np.array
zorder 任何数值
```
## 多图绘制
```py
#一个窗口,多个图,多条数据
sub1=plt.subplot(211,facecolor=(0.1843,0.3098,0.3098))
#将窗口分成2行1列在第1个作图并设置背景色
sub2=plt.subplot(212)
#将窗口分成2行1列在第2个作图
sub1.plot(x,y)
#绘制子图
sub2.plot(x,y)
#绘制子图
axes1 = plt.axes([.2, .3, .1, .1], facecolor='y')
#添加一个子坐标系rect=[左, 下, 宽, 高]
plt.plot(x,y)
#绘制子坐标系,
axes2 = plt.axes([0.7, .2, .1, .1], facecolor='y')
#添加一个子坐标系rect=[左, 下, 宽, 高]
plt.plot(x,y)
plt.show()
```
## 极坐标
```py
fig = plt.figure(2)
#新开一个窗口
ax1 = fig.add_subplot(1,2,1,polar=True)
#启动一个极坐标子图
theta=np.arange(0,2*np.pi,0.02)
#角度数列值
ax1.plot(theta,2*np.ones_like(theta),lw=2)
#画图参数角度半径lw线宽
ax1.plot(theta,theta/6,linestyle='--',lw=2)
#画图参数角度半径linestyle样式lw线宽
ax2 = fig.add_subplot(1,2,2,polar=True)
#启动一个极坐标子图
ax2.plot(theta,np.cos(5*theta),linestyle='--',lw=2)
ax2.plot(theta,2*np.cos(4*theta),lw=2)
ax2.set_rgrids(np.arange(0.2,2,0.2),angle=45) #距离网格轴,轴线刻度和显示位置
ax2.set_thetagrids([0,45,90]) #角度网格轴范围0-360度
plt.show()
```
## 柱状图
```py
plt.figure(3)
x_index = np.arange(5)
#柱的索引
x_data = ('A', 'B', 'C', 'D', 'E')
y1_data = (20, 35, 30, 35, 27)
y2_data = (25, 32, 34, 20, 25)
bar_width = 0.35
#定义一个数字代表每个独立柱的宽度
rects1 = plt.bar(x_index, y1_data, width=bar_width,alpha=0.4, color='b',label='legend1')
#参数:左偏移、高度、柱宽、透明度、颜色、图例
rects2 = plt.bar(x_index + bar_width, y2_data, width=bar_width,alpha=0.5,color='r',label='legend2')
#参数:左偏移、高度、柱宽、透明度、颜色、图例
#关于左偏移,不用关心每根柱的中心不中心,因为只要把刻度线设置在柱的中间就可以了
plt.xticks(x_index + bar_width/2, x_data)
#x轴刻度线
plt.legend()
#显示图例
plt.tight_layout()
#自动控制图像外部边缘,此方法不能够很好的控制图像间的间隔
plt.show()
```
## 直方图
```py
fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6))
#在窗口上添加2个子图
sigma = 1
#标准差
mean = 0
#均值
x=mean+sigma*np.random.randn(10000)
#正态分布随机数
ax0.hist(x,bins=40,normed=False,histtype='bar',facecolor='yellowgreen',alpha=0.75)
#normed是否归一化histtype直方图类型facecolor颜色alpha透明度
ax1.hist(x,bins=20,normed=1,histtype='bar',facecolor='pink',alpha=0.75,cumulative=True,rwidth=0.8)
#bins柱子的个数,cumulative是否计算累加分布rwidth柱子宽度
plt.show()
#所有窗口运行
```
## 散点图
```py
fig = plt.figure(4)
#添加一个窗口
ax =fig.add_subplot(1,1,1)
#在窗口上添加一个子图
x=np.random.random(100)
#产生随机数组
y=np.random.random(100)
#产生随机数组
ax.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='none')
#x横坐标y纵坐标s图像大小c颜色marker图片lw图像边框宽度
plt.show()
#所有窗口运行
```
## 三维图
```py
fig = plt.figure(5)
ax=fig.add_subplot(1,1,1,projection='3d')
#绘制三维图
x,y=np.mgrid[-2:2:20j,-2:2:20j]
#获取x轴数据y轴数据
z=x*np.exp(-x**2-y**2)
#获取z轴数据
ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8)
#绘制三维图表面
ax.set_xlabel('x-name')
#x轴名称
ax.set_ylabel('y-name')
#y轴名称
ax.set_zlabel('z-name')
#z轴名称
plt.show()
```
## 集合图形
```py
fig = plt.figure(6)
#创建一个窗口
ax=fig.add_subplot(1,1,1)
#添加一个子图
rect1 = plt.Rectangle((0.1,0.2),0.2,0.3,color='r')
#创建一个矩形,参数:(x,y),width,height
circ1 = plt.Circle((0.7,0.2),0.15,color='r',alpha=0.3)
#创建一个椭圆,参数:中心点,半径,默认这个圆形会跟随窗口大小进行长宽压缩
pgon1 = plt.Polygon([[0.45,0.45],[0.65,0.6],[0.2,0.6]])
#创建一个多边形,参数:每个顶点坐标
ax.add_patch(rect1)
#将形状添加到子图上
ax.add_patch(circ1)
#将形状添加到子图上
ax.add_patch(pgon1)
#将形状添加到子图上
fig.canvas.draw()
#子图绘制
plt.show()
```

View File

@@ -0,0 +1,135 @@
# 基本用法
## 本章知识点归纳如下:
* 导入模块import matplotlib.pyplot as plt
* 定义图像窗口plt.figure()
* 画图plt.plot(x, y)
* 定义坐标轴范围plt.xlim()/plt.ylim()
* 定义坐标轴名称plt.xlabel()/plt.ylabel()
* 定义坐标轴刻度及名称plt.xticks()/plt.yticks()
* 设置图像边框颜色ax = plt.gca() ax.spines[].set_color()
* 调整刻度位置ax.xaxis.set_ticks_position()/ax.yaxis.set_ticks_position()
* 调整边框坐标轴位置ax.spines[].set_position()
## 导入模块
* 使用import导入模块matplotlib.pyplot并简写成plt使用import导入模块numpy并简写成np
```py
import matplotlib.pyplot as plt
import numpy as np
```
* 然后创建两组数据使用np.linspace定义x范围是(-3,3)个数是50将产生一组-33内均匀分布的50个数(x,y1)表示曲线1(x,y2)表示曲线2。
```py
x = np.linspace(-3, 3, 50)
y1 = 2*x + 1
y2 = x**2
```
## 定义图像窗口并画图
* 在画图前使用plt.figure()定义一个图像窗口编号为3大小为(8, 5)这两项参数可缺省。其中num参数决定了程序运行后弹出的图像窗口名字但在klab平台下不会显示。接着我们使用plt.plot画出(x ,y2)曲线使用plt.plot画(x ,y1)曲线,曲线的颜色属性(color)为红色;曲线的宽度(linewidth)为1.0;曲线的类型(linestyle)为虚线,除了虚线外,大家还可使用以下线性:'-'、'--'、'-.'、':' 。接着我们使用plt.show()显示图像。
```py
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.show()
```
## 定义坐标轴名称及范围
* 使用plt.xlim设置x坐标轴范围(-1, 2) 使用plt.ylim设置y坐标轴范围(-2, 3) 使用plt.xlabel设置x坐标轴名称I am x 使用plt.ylabel设置y坐标轴名称I am y
```py
plt.figure(num=3, figsize=(8, 5),)
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xlabel('I am x')
plt.ylabel('I am y')
plt.show()
```
## 定义坐标轴刻度及名称
* 有时候我们的坐标轴刻度可能并不是一连串的数字而是一些文字或者我们想要调整坐标轴的刻度的稀疏这时就需要使用plt.xticks()或者plt.yticks()来进行调整首先使用np.linspace定义新刻度范围以及个数范围是(-1,2);个数是5。使用plt.xticks设置x轴刻度范围是(-1,2);个数是5。使用plt.yticks设置y轴刻度以及名称刻度为[-2, -1.8, -1, 1.22, 3];对应刻度的名称为[really bad,bad,normal,good, really good]。使用plt.show()显示图像。
```py
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xlabel('I am x')
plt.ylabel('I am y')
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
plt.show()
[-1. -0.25 0.5 1.25 2. ]
```
## 设置图像边框颜色
* 细心的小伙伴可能会注意到我们的图像坐标轴总是由上下左右四条线组成我们也可以对它们进行修改首先使用plt.gca()获取当前坐标轴信息。使用.spines设置边框使用.set_color设置边框颜色
```py
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xticks(new_ticks)
plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.show()
```
调整刻度及边框位置
使用.xaxis.set_ticks_position设置x坐标刻度数字或名称的位置bottom.所有位置topbottombothdefaultnone使用.spines设置边框x轴使用.set_position设置边框位置y=0的位置位置所有属性outwardaxesdata
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xticks(new_ticks)
plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
plt.show()
使用.yaxis.set_ticks_position设置y坐标刻度数字或名称的位置left.所有位置leftrightbothdefaultnone 使用.spines设置边框y轴使用.set_position设置边框位置x=0的位置位置所有属性outwardaxesdata 使用plt.show显示图像.
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xticks(new_ticks)
plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
plt.show()
练一练
小伙伴们以上就是matplotlib的基本用法是不是比较简单呢现在请根据上述所学内容画出直线 y = x-1, 线型为虚线线宽为1纵坐标范围-21横坐标范围-12横纵坐标在00坐标点相交。横坐标的 [-1,-0.5,1] 分别对应 [bad, normal, good]。请一定自己尝试一番再看下面的答案噢~
#答案
x = np.linspace(-1, 2, 50)
y = x - 1
plt.figure()
plt.plot(x,y, linewidth=1.0, linestyle='--')
plt.xlim((-1,2))
plt.ylim((-2,2))
plt.xticks([-1,-0.5,1],['bad', 'normal', 'good'])
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data',0))
ax.spines['bottom'].set_position(('data',0))
plt.show()

View File

@@ -0,0 +1,33 @@
# changshi
import numpy as np
import matplotlib.pyplot as plt
# 定义数据集
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2
# 绘制图像
plt.figure(num=3,figsize=(10,10))
plt.plot(x,y1)
plt.plot(x,y2,color='red',linewidth=1,linestyle='--')
# 定义坐标轴的范围及名称
plt.xlim((-5,5))
plt.ylim((0,10))
plt.xlabel('i am x')
plt.ylabel('i am y')
# 刻度
ticks = np.linspace(-1,2,5)
plt.xticks(ticks)
plt.yticks([-2,-1.8,-1,1.22,3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
# 设置边框颜色
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 展示图像
plt.show()

703
Python/numpy/20随机数.md Normal file
View File

@@ -0,0 +1,703 @@
# 使用numpy产生随机数
numpy中的random模块包含了很多方法可以用来产生随机数这篇文章将对random中的一些常用方法做一个总结。
## 1、numpy.random.rand(d0, d1, ..., dn)
* 作用产生一个给定形状的数组其实应该是ndarray对象或者是一个单值数组中的值服从[0, 1)之间的均匀分布。
* 参数d0, d, ..., dn : int可选。如果没有参数则返回一个float型的随机数该随机数服从[0, 1)之间的均匀分布。
* 返回值ndarray对象或者一个float型的值
例子:
```py
# [0, 1)之间均匀分布的随机数3行2列
a = np.random.rand(3, 2)
print(a)
# 不提供形状
b = np.random.rand()
print(b)
输出
[[0.26054323 0.28184468]
[0.7783674 0.71733674]
[0.90302256 0.49303252]]
0.6022098740124009
```
## 2、numpy.random.uniform(low=0.0, high=1.0, size=None)
* 作用:返回一个在区间[low, high)中均匀分布的数组size指定形状。
* 参数:
* low, highfloat型或者float型的类数组对象。指定抽样区间为[low, high)low的默认值为0.0hign的默认值为1.0
* sizeint型或int型元组。指定形状如果不提供size则返回一个服从该分布的随机数。
例子:
```py
# 在[1, 10)之间均匀抽样数组形状为3行2列
a = np.random.uniform(1, 10, (3, 2))
print(a)
# 不提供size
b = np.random.uniform(1, 10)
print(b)
输出
[[5.16545387 6.3769087 ]
[9.98964899 7.88833885]
[1.37173855 4.19855075]]
3.899250175275188
```
## 3、numpy.random.randn(d0, d1, ..., dn)
* 作用返回一个指定形状的数组数组中的值服从标准正态分布均值为0方差为1
* 参数d0, d, ..., dn : int可选。如果没有参数则返回一个服从标准正态分布的float型随机数。
* 返回值ndarray对象或者float
例子:
```py
# 3行2列
a = np.random.randn(3, 2)
print(a)
# 不提供形状
b = np.random.randn()
print(b)
输出
[[-1.46605527 0.35434705]
[ 0.43408199 0.02689309]
[ 0.48041554 1.62665755]]
-0.6291254375915813
```
## 4、numpy.random.normal(loc=0.0, scale=1.0, size=None)
* 作用返回一个由size指定形状的数组数组中的值服从 μ=loc,σ=scale 的正态分布。
* 参数:
* loc : float型或者float型的类数组对象指定均值 μ
* scale : float型或者float型的类数组对象指定标准差 σ
* size : int型或者int型的元组指定了数组的形状。如果不提供size且loc和scale为标量不是类数组对象则返回一个服从该分布的随机数。
* 输出ndarray对象或者一个标量
例子:
```py
# 标准正态分布3行2列
a = np.random.normal(0, 1, (3, 2))
print(a)
# 均值为1标准差为3
b = np.random.normal(1, 3)
print(b)
输出
[[ 0.34912031 -0.08757564]
[-0.99753101 0.37441719]
[ 2.68072286 -1.03663963]]
5.770831320998463
```
## 5、numpy.random.randint(low, high=None, size=None, dtype='l')
* 作用:返回一个在区间[low, high)中离散均匀抽样的数组size指定形状dtype指定数据类型。
* 参数:
* low, highint型指定抽样区间[low, high)
* sizeint型或int型的元组指定形状
* dypte可选参数指定数据类型比如int,int64等默认是np.int
* 返回值如果指定了size则返回一个int型的ndarray对象否则返回一个服从该分布的int型随机数。
例子:
```py
# 在[1, 10)之间离散均匀抽样数组形状为3行2列
a = np.random.randint(1, 10, (3, 2))
print(a)
# 不提供size
b = np.random.randint(1, 10)
print(b)
# 指定dtype
c = np.random.randint(1, 10, dtype=np.int64)
print(c)
type(c)
输出
[[3 1]
[3 3]
[5 8]]
6
2
numpy.int64
```
## 6、numpy.random.random(size=None)
* 作用:返回从[0, 1)之间均匀抽样的数组size指定形状。
* 参数:
* sizeint型或int型的元组如果不提供则返回一个服从该分布的随机数
* 返回值float型或者float型的ndarray对象
* 例子:
```py
# [0, 1)之间的均匀抽样3行2列
a = np.random.random((3, 2))
print(a)
# 不指定size
b = np.random.random()
print(b)
输出
[[0.80136714 0.63129059]
[0.04556679 0.04433006]
[0.09643599 0.53312761]]
0.32828505898057136
```
# numpy API
## 简单的随机数据
<div id="cnblogs_post_body" class="blogpost-body">
<h1>随机抽样&nbsp;(<tt class="xref py py-mod docutils literal"><span class="pre">numpy.random</span></tt>)</h1>
<div id="simple-random-data" class="section">
<h2><a name="t1"></a>简单的随机数据</h2>
<table class="longtable docutils" border="1"><colgroup><col width="40%"><col width="60%"></colgroup>
<tbody valign="top">
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.rand" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">rand</span></tt></a>(d0,&nbsp;d1,&nbsp;...,&nbsp;dn)</p>
</td>
<td>
<p>随机值</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.rand(3,2)
array([[ 0.14022471, 0.96360618], #random
[ 0.37601032, 0.25528411], #random
[ 0.49313049, 0.94909878]]) #random</pre>
</div>
</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.randn" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html#numpy.random.randn" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">randn</span></tt></a>(d0,&nbsp;d1,&nbsp;...,&nbsp;dn)</p>
</td>
<td>
<p>返回一个样本,具有标准正态分布。</p>
<p class="rubric">Notes</p>
<p>For random samples from&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/93af1f49bf6bbf05f549f49609becdb5f7039538.png" alt="技术分享">, use:</p>
<div class="cnblogs_code">
<pre>sigma * np.random.randn(...) + mu</pre>
</div>
<p class="rubric">Examples</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.randn()
2.1923875335537315 #random</pre>
</div>
<p>Two-by-four array of samples from N(3, 6.25):</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random</pre>
</div>
</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.randint" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html#numpy.random.randint" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">randint</span></tt></a>(low[,&nbsp;high,&nbsp;size])</p>
</td>
<td>
<p>返回随机的整数,位于半开区间 [low, high)。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
&gt;&gt;&gt; np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])</pre>
</div>
<p>Generate a 2 x 4 array of ints between 0 and 4, inclusive:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])</pre>
</div>
</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.random_integers" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random_integers.html#numpy.random.random_integers" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">random_integers</span></tt></a>(low[,&nbsp;high,&nbsp;size])</p>
</td>
<td>
<p>返回随机的整数,位于闭区间 [low, high]。</p>
<p class="rubric">Notes</p>
<p>To sample from N evenly spaced floating-point numbers between a and b, use:</p>
<div class="cnblogs_code">
<pre>a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.)</pre>
</div>
<p>Examples</p>
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div>
<pre>&gt;&gt;&gt; np.random.random_integers(5)
4
&gt;&gt;&gt; type(np.random.random_integers(5))
&lt;type int&gt;
&gt;&gt;&gt; np.random.random_integers(5, size=(3.,2.))
array([[5, 4],
[3, 3],
[4, 5]])</pre>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div></div>
<p>Choose five random numbers from the set of five evenly-spaced numbers between 0 and 2.5, inclusive (<em>i.e.</em>, from the set&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/260812782a8a4f35a929d637a38520175045eaa2.png" alt="技术分享">):</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4.
array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ])</pre>
</div>
<p>Roll two six sided dice 1000 times and sum the results:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; d1 = np.random.random_integers(1, 6, 1000)
&gt;&gt;&gt; d2 = np.random.random_integers(1, 6, 1000)
&gt;&gt;&gt; dsums = d1 + d2</pre>
</div>
<p>Display results as a histogram:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; import matplotlib.pyplot as plt
&gt;&gt;&gt; count, bins, ignored = plt.hist(dsums, 11, normed=True)
&gt;&gt;&gt; plt.show()</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.random_sample" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random_sample.html#numpy.random.random_sample" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">random_sample</span></tt></a>([size])</p>
</td>
<td>
<p>返回随机的浮点数,在半开区间 [0.0, 1.0)。</p>
<p>To sample&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/e9a05a99f961e8f094b3869fcddd366857d7b0d9.png" alt="技术分享">&nbsp;multiply the output of&nbsp;<a class="reference internal" title="numpy.random.random_sample" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random_sample.html#numpy.random.random_sample" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">random_sample</span></tt></a>&nbsp;by&nbsp;<em class="xref py py-obj">(b-a)</em>&nbsp;and add&nbsp;<em class="xref py py-obj">a</em>:</p>
<div class="cnblogs_code">
<pre>(b - a) * random_sample() + a</pre>
</div>
<p>Examples</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.random_sample()
0.47108547995356098
&gt;&gt;&gt; type(np.random.random_sample())
&lt;type float&gt;
&gt;&gt;&gt; np.random.random_sample((5,))
array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])</pre>
</div>
<p>Three-by-two array of random numbers from [-5, 0):</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; 5 * np.random.random_sample((3, 2)) - 5
array([[-3.99149989, -0.52338984],
[-2.99091858, -0.79479508],
[-1.23204345, -1.75224494]])</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.random" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html#numpy.random.random" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">random</span></tt></a>([size])</p>
</td>
<td>
<p>返回随机的浮点数,在半开区间 [0.0, 1.0)。</p>
<p>官网例子与random_sample完全一样</p>
</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.ranf" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.ranf.html#numpy.random.ranf" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">ranf</span></tt></a>([size])</p>
</td>
<td>
<p>返回随机的浮点数,在半开区间 [0.0, 1.0)。</p>
<p>官网例子与random_sample完全一样</p>
</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.sample" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.sample.html#numpy.random.sample" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">sample</span></tt></a>([size])</p>
</td>
<td>
<p>返回随机的浮点数,在半开区间 [0.0, 1.0)。</p>
<p>官网例子与random_sample完全一样</p>
</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.choice" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html#numpy.random.choice" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">choice</span></tt></a>(a[,&nbsp;size,&nbsp;replace,&nbsp;p])</p>
</td>
<td>
<p>生成一个随机样本,从一个给定的一维数组</p>
<p class="rubric">Examples</p>
<p>Generate a uniform random sample from np.arange(5) of size 3:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.choice(5, 3)
array([0, 3, 4])
&gt;&gt;&gt; #This is equivalent to np.random.randint(0,5,3)</pre>
</div>
<p>Generate a non-uniform random sample from np.arange(5) of size 3:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])</pre>
</div>
<p>Generate a uniform random sample from np.arange(5) of size 3 without replacement:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.choice(5, 3, replace=False)
array([3,1,0])
&gt;&gt;&gt; #This is equivalent to np.random.permutation(np.arange(5))[:3]</pre>
</div>
<p>Generate a non-uniform random sample from np.arange(5) of size 3 without replacement:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0])</pre>
</div>
<p>Any of the above can be repeated with an arbitrary array-like instead of just integers. For instance:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; aa_milne_arr = [pooh, rabbit, piglet, Christopher]
&gt;&gt;&gt; np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array([pooh, pooh, pooh, Christopher, piglet],
dtype=|S11)</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.bytes" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.bytes.html#numpy.random.bytes" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">bytes</span></tt></a>(length)</p>
</td>
<td>
<p>返回随机字节。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.bytes(10)
eh\x85\x022SZ\xbf\xa4 #random</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
</tbody>
</table>
</div>
<div id="permutations" class="section">
<h2><a name="t2"></a>排列</h2>
<table class="longtable docutils" border="1"><colgroup><col width="40%"><col width="60%"></colgroup>
<tbody valign="top">
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.shuffle" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.shuffle.html#numpy.random.shuffle" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">shuffle</span></tt></a>(x)</p>
</td>
<td>
<p>现场修改序列,改变自身内容。(类似洗牌,打乱顺序)</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; arr = np.arange(10)
&gt;&gt;&gt; np.random.shuffle(arr)
&gt;&gt;&gt; arr
[1 7 5 2 9 4 3 6 0 8]</pre>
</div>
<p>&nbsp;</p>
<p>This function only shuffles the array along the first index of a multi-dimensional array:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; arr = np.arange(9).reshape((3, 3))
&gt;&gt;&gt; np.random.shuffle(arr)
&gt;&gt;&gt; arr
array([[3, 4, 5],
[6, 7, 8],
[0, 1, 2]])</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.permutation" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.permutation.html#numpy.random.permutation" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">permutation</span></tt></a>(x)</p>
</td>
<td>
<p>返回一个随机排列</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])</pre>
</div>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; np.random.permutation([1, 4, 9, 12, 15])
array([15, 1, 9, 4, 12])</pre>
</div>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; arr = np.arange(9).reshape((3, 3))
&gt;&gt;&gt; np.random.permutation(arr)
array([[6, 7, 8],
[0, 1, 2],
[3, 4, 5]])</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
</tbody>
</table>
</div>
<div id="distributions" class="section">
<h2><a name="t3"></a><span class="web-item">分布</span></h2>
<table class="longtable docutils" border="1"><colgroup><col width="40%"><col width="60%"></colgroup>
<tbody valign="top">
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.beta" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.beta.html#numpy.random.beta" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">beta</span></tt></a>(a,&nbsp;b[,&nbsp;size])</p>
</td>
<td>贝塔分布样本,在&nbsp;<tt class="docutils literal"><span class="pre">[0,&nbsp;<span class="pre">1]</span></span></tt>内。</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.binomial" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.binomial.html#numpy.random.binomial" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">binomial</span></tt></a>(n,&nbsp;p[,&nbsp;size])</p>
</td>
<td>二项分布的样本。</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.chisquare" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.chisquare.html#numpy.random.chisquare" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">chisquare</span></tt></a>(df[,&nbsp;size])</p>
</td>
<td>卡方分布样本。</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.dirichlet" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.dirichlet.html#numpy.random.dirichlet" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">dirichlet</span></tt></a>(alpha[,&nbsp;size])</p>
</td>
<td>狄利克雷分布样本。</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.exponential" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.exponential.html#numpy.random.exponential" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">exponential</span></tt></a>([scale,&nbsp;size])</p>
</td>
<td>指数分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.f" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.f.html#numpy.random.f" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">f</span></tt></a>(dfnum,&nbsp;dfden[,&nbsp;size])</p>
</td>
<td>F分布样本。</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.gamma" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.gamma.html#numpy.random.gamma" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">gamma</span></tt></a>(shape[,&nbsp;scale,&nbsp;size])</p>
</td>
<td>伽马分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.geometric" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.geometric.html#numpy.random.geometric" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">geometric</span></tt></a>(p[,&nbsp;size])</p>
</td>
<td>几何分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.gumbel" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.gumbel.html#numpy.random.gumbel" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">gumbel</span></tt></a>([loc,&nbsp;scale,&nbsp;size])</p>
</td>
<td>耿贝尔分布。</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.hypergeometric" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.hypergeometric.html#numpy.random.hypergeometric" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">hypergeometric</span></tt></a>(ngood,&nbsp;nbad,&nbsp;nsample[,&nbsp;size])</p>
</td>
<td>超几何分布样本。</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.laplace" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.laplace.html#numpy.random.laplace" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">laplace</span></tt></a>([loc,&nbsp;scale,&nbsp;size])</p>
</td>
<td>拉普拉斯或双指数分布样本</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.logistic" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.logistic.html#numpy.random.logistic" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">logistic</span></tt></a>([loc,&nbsp;scale,&nbsp;size])</p>
</td>
<td>Logistic分布样本</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.lognormal" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.lognormal.html#numpy.random.lognormal" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">lognormal</span></tt></a>([mean,&nbsp;sigma,&nbsp;size])</p>
</td>
<td>对数正态分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.logseries" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.logseries.html#numpy.random.logseries" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">logseries</span></tt></a>(p[,&nbsp;size])</p>
</td>
<td>对数级数分布。</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.multinomial" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multinomial.html#numpy.random.multinomial" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">multinomial</span></tt></a>(n,&nbsp;pvals[,&nbsp;size])</p>
</td>
<td>多项分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.multivariate_normal" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multivariate_normal.html#numpy.random.multivariate_normal" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">multivariate_normal</span></tt></a>(mean,&nbsp;cov[,&nbsp;size])</p>
</td>
<td>
<p>多元正态分布。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; mean = [0,0]
&gt;&gt;&gt; cov = [[1,0],[0,100]] # diagonal covariance, points lie on x or y-axis</pre>
</div>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; import matplotlib.pyplot as plt
&gt;&gt;&gt; x, y = np.random.multivariate_normal(mean, cov, 5000).T
&gt;&gt;&gt; plt.plot(x, y, x); plt.axis(equal); plt.show()</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.negative_binomial" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.negative_binomial.html#numpy.random.negative_binomial" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">negative_binomial</span></tt></a>(n,&nbsp;p[,&nbsp;size])</p>
</td>
<td>负二项分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.noncentral_chisquare" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.noncentral_chisquare.html#numpy.random.noncentral_chisquare" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">noncentral_chisquare</span></tt></a>(df,&nbsp;nonc[,&nbsp;size])</p>
</td>
<td>非中心卡方分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.noncentral_f" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.noncentral_f.html#numpy.random.noncentral_f" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">noncentral_f</span></tt></a>(dfnum,&nbsp;dfden,&nbsp;nonc[,&nbsp;size])</p>
</td>
<td>非中心F分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.normal" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html#numpy.random.normal" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">normal</span></tt></a>([loc,&nbsp;scale,&nbsp;size])</p>
</td>
<td>
<p>正态(高斯)分布</p>
<p class="rubric">Notes</p>
<p>The probability density for the Gaussian distribution is</p>
<div class="math">
<p><img src="http://docs.scipy.org/doc/numpy/_images/math/2f8a02ce155191ed5a4ea8d776aa15fcaef26e1f.png" alt="技术分享"></p>
</div>
<p>where&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/fb6d665bbe0c01fc1af5c5f5fa7df40dc71331d7.png" alt="技术分享">&nbsp;is the mean and&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/bb6e1902efeb0b3704c6191ddce1f02707ab7d4b.png" alt="技术分享">&nbsp;the standard deviation. The square of the standard deviation,&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/dd3f23ceebfef553bff1607f84667d5cc6af7587.png" alt="技术分享">, is called the variance.</p>
<p>The function has its peak at the mean, and its “spread” increases with the standard deviation (the function reaches 0.607 times its maximum at&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/ad6d5400fce43a299a35b53a9661e54a284f1bad.png" alt="技术分享">&nbsp;and&nbsp;<img class="math" src="http://docs.scipy.org/doc/numpy/_images/math/fa3af8846dae3eb1c8913dace238ff110abb64b2.png" alt="技术分享"><a id="id3" class="reference internal" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html#r217" target="_blank">&nbsp;[R217]</a>).</p>
<p>&nbsp;</p>
<p class="rubric">Examples</p>
<p>Draw samples from the distribution:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; mu, sigma = 0, 0.1 # mean and standard deviation
&gt;&gt;&gt; s = np.random.normal(mu, sigma, 1000)</pre>
</div>
<p>Verify the mean and the variance:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; abs(mu - np.mean(s)) &lt; 0.01
True
&gt;&gt;&gt; abs(sigma - np.std(s, ddof=1)) &lt; 0.01
True</pre>
</div>
<p>Display the histogram of the samples, along with the probability density function:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; import matplotlib.pyplot as plt
&gt;&gt;&gt; count, bins, ignored = plt.hist(s, 30, normed=True)
&gt;&gt;&gt; plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
... np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
... linewidth=2, color=r)
&gt;&gt;&gt; plt.show()</pre>
</div>
<p>&nbsp;</p>
</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.pareto" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.pareto.html#numpy.random.pareto" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">pareto</span></tt></a>(a[,&nbsp;size])</p>
</td>
<td>帕累托Lomax分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.poisson" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.poisson.html#numpy.random.poisson" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">poisson</span></tt></a>([lam,&nbsp;size])</p>
</td>
<td>泊松分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.power" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.power.html#numpy.random.power" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">power</span></tt></a>(a[,&nbsp;size])</p>
</td>
<td>Draws samples in [0, 1] from a power distribution with positive exponent a - 1.</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.rayleigh" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rayleigh.html#numpy.random.rayleigh" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">rayleigh</span></tt></a>([scale,&nbsp;size])</p>
</td>
<td>Rayleigh&nbsp;分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.standard_cauchy" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.standard_cauchy.html#numpy.random.standard_cauchy" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">standard_cauchy</span></tt></a>([size])</p>
</td>
<td>标准柯西分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.standard_exponential" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.standard_exponential.html#numpy.random.standard_exponential" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">standard_exponential</span></tt></a>([size])</p>
</td>
<td>标准的指数分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.standard_gamma" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.standard_gamma.html#numpy.random.standard_gamma" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">standard_gamma</span></tt></a>(shape[,&nbsp;size])</p>
</td>
<td>标准伽马分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.standard_normal" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.standard_normal.html#numpy.random.standard_normal" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">standard_normal</span></tt></a>([size])</p>
</td>
<td>标准正态分布&nbsp;(mean=0, stdev=1).</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.standard_t" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.standard_t.html#numpy.random.standard_t" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">standard_t</span></tt></a>(df[,&nbsp;size])</p>
</td>
<td>Standard Students t distribution with df degrees of freedom.</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.triangular" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html#numpy.random.triangular" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">triangular</span></tt></a>(left,&nbsp;mode,&nbsp;right[,&nbsp;size])</p>
</td>
<td>三角形分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.uniform" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.uniform.html#numpy.random.uniform" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">uniform</span></tt></a>([low,&nbsp;high,&nbsp;size])</p>
</td>
<td>均匀分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.vonmises" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.vonmises.html#numpy.random.vonmises" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">vonmises</span></tt></a>(mu,&nbsp;kappa[,&nbsp;size])</p>
</td>
<td>von Mises分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.wald" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.wald.html#numpy.random.wald" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">wald</span></tt></a>(mean,&nbsp;scale[,&nbsp;size])</p>
</td>
<td>瓦尔德(逆高斯)分布</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.weibull" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.weibull.html#numpy.random.weibull" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">weibull</span></tt></a>(a[,&nbsp;size])</p>
</td>
<td>Weibull&nbsp;分布</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.zipf" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.zipf.html#numpy.random.zipf" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">zipf</span></tt></a>(a[,&nbsp;size])</p>
</td>
<td>齐普夫分布</td>
</tr>
</tbody>
</table>
</div>
<h2><a name="t4"></a>随机数生成器</h2>
<table class="longtable docutils" border="1"><colgroup><col width="40%"><col width="60%"></colgroup>
<tbody valign="top">
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.RandomState" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.RandomState.html#numpy.random.RandomState" target="_blank"><tt class="xref py py-obj docutils literal">RandomState</tt></a></p>
</td>
<td>Container for the Mersenne Twister pseudo-random number generator.</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.seed" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.seed.html#numpy.random.seed" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">seed</span></tt></a>([seed])</p>
</td>
<td>Seed the generator.</td>
</tr>
<tr class="row-odd">
<td>
<p><a class="reference internal" title="numpy.random.get_state" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.get_state.html#numpy.random.get_state" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">get_state</span></tt></a>()</p>
</td>
<td>Return a tuple representing the internal state of the generator.</td>
</tr>
<tr class="row-even">
<td>
<p><a class="reference internal" title="numpy.random.set_state" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.set_state.html#numpy.random.set_state" target="_blank"><tt class="xref py py-obj docutils literal"><span class="pre">set_state</span></tt></a>(state)</p>
</td>
<td>Set the internal state of the generator from a tuple.</td>
</tr>
</tbody>
</table>
</div>