Python 如何使用Scikit-learn创建一个示例数据集
在本教程中,我们将学习如何使用Python Scikit-learn创建一个示例数据集。
Scikit-learn库中有各种内置的数据集,我们可以轻松地用于我们的机器学习模型,但有时我们需要一些玩具数据集。为此,Scikit-learn Python库为我们提供了一个很棒的示例数据集生成器。
使用Scikit-Learn创建示例Blob数据集
要创建示例Blob数据集,我们需要导入 sklearn.datasets.make_blobs ,它非常快速和易于使用。
示例
在下面的示例中,让我们看看我们如何使用这个库创建示例Blob数据集。
# Importing libraries
from sklearn.datasets import make_blobs
# Matplotlib for plotting the dataset blobs
from matplotlib import pyplot as plt
from matplotlib import style
# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Creating Blob Test Datasets using sklearn.datasets.make_blobs
style.use("Solarize_Light2")
X, y = make_blobs(n_samples = 500, centers = 3,
cluster_std = 1, n_features = 2)
plt.scatter(X[:, 0], X[:, 1], s = 20, color = 'red')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
输出
它将产生以下输出−
上面的输出显示它从500个样本中创建了3个blob。
使用Scikit-Learn创建示例Moon数据集
要创建示例moon数据集,我们需要导入 sklearn.datasets.make_moons ,它非常快速和易于使用。
示例
在下面给出的示例中,让我们看看如何使用这个库来创建示例moon数据集。
# Importing libraries
from sklearn.datasets import make_moons
# Matplotlib for plotting the moon dataset
from matplotlib import pyplot as plt
from matplotlib import style
# Set the figure size
plt.rcParams["figure.figsize"] = [7.16, 3.50]
plt.rcParams["figure.autolayout"] = True
# Creating Moon Test Datasets using sklearn.datasets.make_moon
style.use("fivethirtyeight")
X, y = make_moons(n_samples = 1500, noise = 0.1)
plt.scatter(X[:, 0], X[:, 1], s = 15, color ='red')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
输出
它将产生以下输出
使用Scikit-Learn创建示例圆形数据集
要创建示例圆形数据集,我们需要导入sklearn.datsets.make_circles,这是非常快速和易于使用的。
示例
在下面的示例中,让我们看看如何使用这个库来创建示例圆形数据集。
# Importing libraries
from sklearn.datasets import make_circles
# Matplotlib for plotting the circle dataset
from matplotlib import pyplot as plt
from matplotlib import style
# Set the figure size
plt.rcParams["figure.figsize"] = [7.16, 3.50]
plt.rcParams["figure.autolayout"] = True
# Creating the circle Test Datasets using sklearn.datasets.make_circles
style.use("ggplot")
X, y = make_circles(n_samples = 500, noise = 0.02)
plt.scatter(X[:, 0], X[:, 1], s = 20, color ='red')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
输出
将会产生以下输出 –