首页 > 解决方案 > 如何将 csv 数据集 100% 作为 sklearn 包的 make_blobs() 的输出

问题描述

我是 Python 的新手,只想检查、编辑、调整一些基本聚类功能的代码。

我有一个 csv 文件,其中大约 1000 个值分为两列,我想在 python 环境中导入它并让它 100% 兼容作为 make_blobs() 的输出

我导入了数据并使用以下方法创建了一个数组:

import pandas as pd
import numpy as np
X = pd.read_csv("/data.csv")
X = X.as_matrix()

我可以看到成功地将数据视为数组:

> array([[3.094545, 2.106678],
>        [2.886592, 2.029667],
>        [3.016772, 2.23431 ],
>        [2.739621, 1.883947],
>        [3.202192, 2.009674],
>        [3.295198, 1.346389],
>        [2.769266, 2.041654],
>        [2.867035, 2.222251],
>        [2.963459, 2.22474 ],
>        [3.187592, 2.155406],
>        [2.889698, 1.973654],
>        [3.079113, 2.219817],
>        [3.20275 , 1.833527],
>        [3.008534, 2.005787],
>        [3.16477 , 2.050318],
>        [2.7942  , 1.685328],
>        [3.159495, 2.02407 ],
>        [3.058299, 1.428027],
>        [3.092592, 1.923008],

. 问题是使用时:

from sklearn.datasets.samples_generator import make_blobs
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples = 300, centers=centers, cluster_std=0.5,
                            random_state=0)

名为 labels_true 的第二个变量包含从 0 到 2 不等的值。

我想知道我能做什么,让“data.csv”文件具有与 X 相同的输出,并且 labels_true

标签: pythonarrayscluster-analysis

解决方案


您的数据未标记。

如果您有标签,则不需要聚类。

make_blobs是合成的,所以它也可以生成“正确”的标签,但你将不得不这样做。


推荐阅读