如何使用 Python Scikit-learn 生成和繪製分類資料集?


Scikit-learn 提供了 make_classification() 函式,藉助該函式,我們可以繪製具有不同資訊特徵數量、每個類別的叢集數量和類別的隨機生成的分類資料集。在本教程中,我們將學習如何使用 Python Scikit-learn 生成和繪製分類資料集。

具有一個資訊特徵和每個類別一個叢集的資料集

要生成和繪製具有一個資訊特徵和一個叢集的分類資料集,我們可以採取以下步驟:

步驟 1 − 匯入執行程式所需的庫 sklearn.datasets.make_classification 和 matplotlib。

步驟 2 − 建立名為 X 和 y 的資料點,其中資訊特徵數量和每個類別叢集數量引數都等於 1。

步驟 3 − 使用 matplotlib 庫繪製資料集。

示例

在下面的示例中,我們生成並列印一個具有一個資訊特徵和每個類別一個叢集的分類資料集。

# Importing libraries from sklearn.datasets import make_classification import matplotlib.pyplot as plt # Creating the classification dataset with one informative feature and one cluster per class X, y = make_classification(n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1) # Plotting the dataset plt.figure(figsize=(7.50, 3.50)) plt.subplots_adjust(bottom=0.05, top=0.9, left=0.05, right=0.95) plt.subplot(111) plt.title("Classification dataset with one informative feature and one cluster per class", fontsize="12") plt.scatter(X[:, 0], X[:, 1], marker="o", c=y, s=40, edgecolor="k") plt.show()

輸出

它將產生以下輸出:


具有兩個資訊特徵和每個類別一個叢集的資料集

要生成和繪製具有兩個資訊特徵和每個類別一個叢集的分類資料集,我們可以採取以下步驟:

步驟 1 − 匯入執行程式所需的庫 sklearn.datasets.make_classification 和 matplotlib。

步驟 2 − 建立名為 X 和 y 的資料點,其中資訊特徵數量等於 2,每個類別叢集數量引數等於 1。

步驟 3 − 使用 matplotlib 庫繪製資料集。

示例

在下面的示例中,我們生成並列印一個具有兩個資訊特徵和每個類別一個叢集的分類資料集。

# Importing libraries from sklearn.datasets import make_classification import matplotlib.pyplot as plt # Creating the classification dataset with two informative feature and one cluster per class X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1) # Plotting the dataset plt.figure(figsize=(7.50, 3.50)) plt.subplots_adjust(bottom=0.05, top=0.9, left=0.05, right=0.95) plt.subplot(111) plt.title("Classification dataset with two informative feature and one cluster per class", fontsize="12") plt.scatter(X[:, 0], X[:, 1], marker="o", c=y, s=40, edgecolor="k") plt.show()

輸出

它將產生以下輸出:


具有兩個資訊特徵和每個類別兩個叢集的資料集

要生成和繪製具有兩個資訊特徵和每個類別兩個叢集的分類資料集,我們可以採取以下步驟:

步驟 1 − 匯入執行程式所需的庫 sklearn.datasets.make_classification 和 matplotlib。

步驟 2 − 建立名為 X 和 y 的資料點,其中資訊特徵數量和每個類別叢集數量引數都等於 2。

步驟 3 − 使用 matplotlib 庫繪製資料集。

示例

在下面的示例中,我們生成並列印一個具有兩個資訊特徵和每個類別兩個叢集的分類資料集。

# Importing libraries from sklearn.datasets import make_classification import matplotlib.pyplot as plt # Creating the classification dataset with two informative feature and two cluster per class X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=2) # Plotting the dataset plt.figure(figsize=(7.50, 3.50)) plt.subplots_adjust(bottom=0.05, top=0.9, left=0.05, right=0.95) plt.subplot(111) plt.title("Classification dataset with two informative feature and two cluster per class", fontsize="12") plt.scatter(X[:, 0], X[:, 1], marker="o", c=y, s=40, edgecolor="k") plt.show()

輸出

它將產生以下輸出:


多類分類資料集

要生成和繪製具有兩個資訊特徵和每個類別一個叢集的多類分類資料集,我們可以採取以下步驟:

步驟 1 − 匯入執行程式所需的庫 sklearn.datasets.make_classification 和 matplotlib。

步驟 2 − 建立名為 X 和 y 的資料點,其中資訊特徵數量等於 2,每個類別叢集數量引數等於 1,類別數量引數等於 3。

步驟 3 − 使用 matplotlib 庫繪製資料集。

示例

在下面的示例中,我們生成並列印一個具有兩個資訊特徵和每個類別一個叢集的多類分類資料集。

# Importing libraries from sklearn.datasets import make_classification import matplotlib.pyplot as plt # Creating the multi-class classification dataset with two informative feature and one cluster per class X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1, n_classes=3) # Plotting the dataset plt.figure(figsize=(7.50, 3.50)) plt.subplots_adjust(bottom=0.05, top=0.9, left=0.05, right=0.95) plt.subplot(111) plt.title("Multi-class classification dataset with two informative feature and one cluster per class", fontsize="12") plt.scatter(X[:, 0], X[:, 1], marker="o", c=y, s=40, edgecolor="k") plt.show()

輸出

它將產生以下輸出:


更新於: 2022年10月4日

3K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告