scikit-learn - SciKit 的 make_blobs 中的 "n_features"和 "centers"参数是什么意思?

标签 scikit-learn

我浏览了关于n_features的文档和 centers make_blobs 中的参数函数在 SciKit .但是,由于我是 SciKit 的新手,所以我所看到的每一个解释对我来说都不是很清楚。和数学。我想知道这两个参数是做什么的:n_features , centers做在 make_blobs功能如下。

make_blobs(n_samples=50, n_features=2, centers=2, random_state=75)

先感谢您。

最佳答案

make_blobs函数是 sklearn.datasets.samples_generator 的一部分.包中的所有方法,帮助我们生成数据样本或数据集。在 scikit-learn 的机器学习中,数据集用于评估机器学习模型的性能。这是一个关于如何评估 KNN classifier 的示例:

from sklearn.datasets.samples_generator import make_blobs
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

X, y = make_blobs(n_features=2, centers=3)
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = KNeighborsClassifier()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred) * 100
print('accuracy: {}%'.format(acc))

现在,正如你提到的,n_features确定生成的数据集将具有多少列或特征。在机器学习中,特征对应于数值特征数据。例如,在 Iris Dataset ,有 4 个特征(萼片长度、萼片宽度、花瓣长度和花瓣宽度),因此数据集中有 4 个数字列。所以通过增加 n_featuresmake_blobs ,我们正在添加更多功能,因此增加了生成数据集的复杂性。

至于centers ,通过可视化生成的数据集更容易理解。我用 matplotlib帮助我们:
from sklearn.datasets.samples_generator import make_blobs
import matplot

# plot 1
X, y = make_blobs(n_features=2, centers=1)
plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.savefig('centers_1.png')
plt.title('centers = 1')

# plot 2    
X, y = make_blobs(n_features=2, centers=2)
plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.title('centers = 2')

# plot 3
X, y = make_blobs(n_features=2, centers=3)
plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.title('centers = 3')

plt.show()

centers=1

centers=2

centers=3

如果你运行上面的代码,你可以很容易地看到 centers对应于数据中生成的类数。它使用中心作为术语,因为属于同一类的样本倾向于聚集在中心(坐标)附近。

关于scikit-learn - SciKit 的 make_blobs 中的 "n_features"和 "centers"参数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51709878/

相关文章:

python - 带有 SVM 的 GridSearch 产生 IndexError

python - xgboost 中的多类分类(python)

python - scikit学习中的线性回归

python - 如何在 scikit-learn 中正确地将数字特征与文本(词袋)结合起来?

python - 不同版本的 sklearn 给出了截然不同的训练结果

pandas - 如何让 scikit 学会找到简单的非线性关系

python - 将概率与 scikit-learn 中的标签联系起来

vector - 转换为 TFIDF 值向量的相似文档在向量空间中看起来如何

python - LabelEncoder().fit_transform 与用于分类编码的 pd.get_dummies

python - 使用管道作为估计器的 VotingClassifier