make_classification函数详解
时间: 2024-06-09 10:09:33 浏览: 17
`make_classification` 函数是 Scikit-learn 中的一个函数,用于生成随机分类数据集。该函数一般用于机器学习模型的测试和演示。
函数原型:
```python
make_classification(n_samples=100, n_features=20, *, n_informative=2, n_redundant=2, n_repeated=0, n_classes=2, n_clusters_per_class=2, weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0, scale=1.0, shuffle=True, random_state=None)
```
参数说明:
- `n_samples`:生成的样本数,默认为 100。
- `n_features`:生成的特征数,默认为 20。
- `n_informative`:相关特征的数量,这些特征被用来生成类别,默认为 2。
- `n_redundant`:冗余特征数量,被随机生成并添加到相关特征中,默认为 2。
- `n_repeated`:重复特征数量,被随机生成并添加到相关特征中,默认为 0。
- `n_classes`:输出的类别数,默认为 2。
- `n_clusters_per_class`:每个类别的簇数量,默认为 2。
- `weights`:类别权重,默认为 None,即每个类别的权重相等。
- `flip_y`:标签翻转概率,默认为 0.01。
- `class_sep`:类别间的距离,默认为 1.0。
- `hypercube`:是否在超立方体中生成数据,默认为 True。
- `shift`:数据位移,默认为 0.0。
- `scale`:数据缩放,默认为 1.0。
- `shuffle`:是否将生成的数据打乱,默认为 True。
- `random_state`:随机数生成器的种子。
返回值:
- `X`:生成的特征矩阵,形状为 `(n_samples, n_features)`。
- `y`:生成的标签数组,形状为 `(n_samples,)`。
示例代码:
```python
from sklearn.datasets import make_classification
# 生成一个包含 100 个样本和 5 个特征的数据集,其中 2 个特征是相关的
X, y = make_classification(n_samples=100, n_features=5, n_informative=2, random_state=42)
print(X.shape) # (100, 5)
print(y[:10]) # [1 1 1 0 1 0 1 0 1 0]
```
参考资料:
- [Scikit-learn 官方文档](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_classification.html)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)