機器學習筆記:sklearn.datasets樣本生成器——make_classification、make_blobs、make_regression


一、介紹

scikit-learn 包含各種隨機樣本的生成器,可以用來建立可控制大小和復雜性的人工數據集。

  • make_blob() —— 聚類生成器
  • make_classification() —— 單標簽分類生成器
  • make_multilabel_classification() —— 多標簽生成器
  • make_regression() —— 回歸生成器

二、分類生成器 make_classification

專門通過引入相關的,冗余的和未知的噪音特征,將高斯集群的每個類復雜化。

1.使用語法

sklearn.datasets.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, # 隨機交換樣本的一段 y噪聲值的比重
        class_sep=1.0, # 類與類之間區分清楚的程度
        hypercube=True, # 如果為True,則將簇放置在超立方體的頂點上;如果為False,則將簇放置在隨機多面體的頂點上。
        shift=0.0, # 將各個特征的值移動,即加上或減去某個值
        scale=1.0, # 將各個特征的值乘上某個數,放大或縮小
        shuffle=True, # 是否洗牌樣本
        random_state=None) # 隨機種子
  • n_informative —— 有價值的重要特征
  • n_redundant —— 將重要特征進行線性組合的特征
  • n_clusters_per_class —— 簇的個數,某一個類別由幾個簇構成

2.實操

  • 默認參數
from collections import Counter
from sklearn.datasets import make_classification
X, y = make_classification()
Counter(y) # Counter({0: 50, 1: 50})
  • 修改參數
from collections import Counter
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X, y = make_classification(
        n_samples=1000,
        n_features=25,
        n_informative=3,
        n_redundant=2,
        n_repeated=0,
        n_classes=3,
        n_clusters_per_class=1,
        random_state=42
        )
Counter(y) # Counter({0: 332, 1: 335, 2: 333})

print("原始特征維度:", X.shape)
# 原始特征維度: (1000, 25)

# 數據划分
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.3)

三、聚類生成器 make_blobs

對於中心和各簇的標准偏差提供了更好的控制,可用於演示聚類。

1.使用語法

sklearn.datasets.make_blobs(
        n_samples=100, # 樣本數量
        n_features=2, # 特征數量
        centers=None, # 中心個數 int
        cluster_std=1.0, # 聚簇的標准差
        center_box(-10.0, 10.0), # 聚簇中心的邊界框
        shuffle=True, # 是否洗牌樣本
        random_state=None #隨機種子
        )

2.實操

import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
import numpy as np

X, y = make_blobs(n_samples=1000,
                  n_features=2,
                  centers=2,
                  cluster_std=1.5,
                  random_state=1)

plt.style.use('ggplot')
plt.figure()
plt.title('Data')
plt.scatter(X[:, 0], X[:, 1], marker='o', c=np.squeeze(y), s=30)

X, y = make_blobs(n_samples=[100,300,250,400],
                  n_features=2,
                  centers=[[100,150],[250,400],[600,100],[300,500]],
                  cluster_std=50,
                  random_state=1)

plt.style.use('ggplot')
plt.figure()
plt.title('Data')
plt.scatter(X[:, 0], X[:, 1], marker='o', c=np.squeeze(y), s=30)

生成4個聚簇,數量分別為:100、300、250、400。

中心坐標為[數組]。

四、多標簽生成器 make_multilabel_classification

sklearn.datasets.make_multilabel_classification(
    n_samples=100, 
    n_features=20, 
    n_classes=5, 
    n_labels=2, 
    length=50, 
    allow_unlabeled=True, 
    sparse=False, 
    return_indicator='dense', 
    return_distributions=False, 
    random_state=None)

五、回歸生成器 make_regression

回歸生成器所產生的回歸目標作為一個可選擇的稀疏線性組合的具有噪聲的隨機的特征。

它的信息特征可能是不相關的或低秩(少數特征占大多數的方差),也即用於回歸任務測試的樣本生成器。

sklearn.datasets.make_regression(
    n_samples=100, 
    n_features=100, 
    n_informative=10, 
    n_targets=1, 
    bias=0.0, 
    effective_rank=None, 
    tail_strength=0.5, 
    noise=0.0, 
    shuffle=True, 
    coef=False, 
    random_state=None
)

參考鏈接:sklearn.datasets.make_classification

參考鏈接:生成分類數據集(make_classification)

參考鏈接:請問make_classification的參數怎么解釋?

參考鏈接:機器學習:SKLEARN樣本生成器,MAKE_BLOB(), MAKE_CLASSIFICATION()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM