plt中subplot分格展示

plt.subplot2grid函数

plt.subplot2grid 是 Matplotlib 库中用于创建基于网格布局的子图的函数。它允许我们在网格中指定子图的位置和大小。其基本用法如下:

plt.subplot2grid((nrows, ncols), (row, col), rowspan, colspan)

其中:

  1. (nrows, ncols) 指定了网格的行数和列数。
  2. (row, col) 指定了子图的起始位置。
  3. rowspan 和 colspan分别指定了子图所占的行数和列数。

method 1:subplot2grid

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method 1:subplot2grid
plt.figure()

ax1=plt.subplot2grid((3,3),(0,0),colspan =3,rowspan=1)
ax1.plot([1,2],[1,2])
ax1.set_title('ax1_title')

ax2=plt.subplot2grid((3,3),(1,0),colspan =2,rowspan=1)
ax3=plt.subplot2grid((3,3),(1,2),rowspan=2)
ax4=plt.subplot2grid((3,3),(2,0),colspan =1,rowspan=1)
ax5=plt.subplot2grid((3,3),(2,1),colspan =1,rowspan=1)

plt.tight_layout()
plt.show()

在这里插入图片描述

gridspec.GridSpec函数

gridspec.GridSpec 是 Matplotlib 库中用于创建复杂网格布局的函数。它允许我们更灵活地指定子图的位置和大小,以及子图之间的间距。

method 2:gridspec

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method 2:gridspec
plt.figure()
gs =gridspec.GridSpec(3,3)

ax1=plt.subplot(gs[0,:])

ax1=plt.subplot(gs[1,:2])

ax1=plt.subplot(gs[1:,2])

ax1=plt.subplot(gs[-1,0])

ax1=plt.subplot(gs[-1,-2])

plt.tight_layout()
plt.show()

在这里插入图片描述

method3:easy to define structure

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method3:easy to define structure
f,((ax11,ax12),(ax21,ax22)) =plt.subplots(2,2,sharex=True,sharey=True)
ax11.scatter([1,2],[1,2])
ax12.scatter([1.5,2],[1,2])
ax21.scatter([0.5,2],[1,2])
ax22.scatter([1.3,1.8],[1,2])
plt.show()

在这里插入图片描述

(1) 使用GridSearchCV搜索单个DecisionTreeClassifiermax_samples,max_features,max_depth的最优值。 ```python from sklearn.tree import DecisionTreeClassifier from sklearn.model_selection import GridSearchCV # 数据预处理 sc = StandardScaler() X_std = sc.fit_transform(X) # 定义模型 tree = DecisionTreeClassifier() # 定义参数空间 param_grid = {'max_samples': [0.4, 0.6, 0.8, 1.0], 'max_features': [0.4, 0.6, 0.8, 1.0], 'max_depth': [3, 5, 7, 9, None]} # 定义网格搜索对象 clf = GridSearchCV(tree, param_grid=param_grid, cv=5) # 训练模型 clf.fit(X_std, y) # 输出最优参数 print("Best parameters:", clf.best_params_) ``` (2) 使用GridSearchCV搜索BaggingClassifiern_estimators的最佳值。 ```python from sklearn.ensemble import BaggingClassifier # 定义模型 tree = DecisionTreeClassifier() bagging = BaggingClassifier(tree) # 定义参数空间 param_grid = {'n_estimators': [10, 50, 100, 200, 500]} # 定义网格搜索对象 clf = GridSearchCV(bagging, param_grid=param_grid, cv=5) # 训练模型 clf.fit(X_std, y) # 输出最优参数 print("Best parameters:", clf.best_params_) ``` (3) 考虑BaggingClassifier的弱分类器使用SVC(可以考虑是否使用核函数),类似步骤(1),(2),自己调参(比如高斯核函数的gamma参数,C参数),寻找最优分类结果。 ```python from sklearn.svm import SVC from sklearn.ensemble import BaggingClassifier # 定义模型 svc = SVC() bagging = BaggingClassifier(svc) # 定义参数空间 param_grid = {'base_estimator__C': [0.1, 1, 10], 'base_estimator__gamma': [0.1, 1, 10], 'n_estimators': [10, 50, 100, 200, 500]} # 定义网格搜索对象 clf = GridSearchCV(bagging, param_grid=param_grid, cv=5) # 训练模型 clf.fit(X_std, y) # 输出最优参数 print("Best parameters:", clf.best_params_) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值