gpt4 book ai didi

python - SciKit-learn (python)--创建我的数据集

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:15 25 4
gpt4 key购买 nike

我已经安装了 scikit-learn,但我不知道如何使用它。我有一些看起来像这样的数据:

{"Tiempo": 2.1,  "Brazos": "der", "Puntuacion ": 112, "Nombre": "Alguien1"},
{"Tiempo": 4.1, "Brazos": "izq", "Puntuacion ": 11, "Nombre": "Alguien2"},
{"Tiempo": 3.211, "Brazos": "ambos","Puntuacion ": 1442, "Nombre": "Alguien3"}

我想对它们使用一些分类器(如 SVM)。对于我在示例中看到的内容,我需要创建一个数据集。在示例中,他们总是使用一些预先确定的数据集作为“iris”。就我而言,我想我需要使用我的数据创建自己的。为了做到这一点,我进行了搜索,发现我应该使用下一个函数来获取我的数据集的“特征”:

measurements = [
{'city': 'Dubai', 'temperature': 33.},
{'city': 'London', 'temperature': 12.},
{'city': 'San Fransisco', 'temperature': 18.},
]

from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer()

vec.fit_transform(measurements).toarray()
array([[ 1., 0., 0., 33.],
[ 0., 1., 0., 12.],
[ 0., 0., 1., 18.]])

>>> vec.get_feature_names()
['city=Dubai', 'city=London', 'city=San Fransisco', 'temperature']

在我的例子中,在你对我的数据使用该函数后,我得到了这个: enter image description here

一旦我有了这个,我想我需要获得我的“样本”,但是,我不知道该怎么做。请问你能帮帮我吗?你能告诉我我的假设是否正确吗?

最佳答案

您走在正确的轨道上。以您的数据为例。

from sklearn.feature_extraction import DictVectorizer

# your data
data = [{"Tiempo": 2.1, "Brazos": "der", "Puntuacion ": 112, "Nombre": "Alguien1"}, {"Tiempo": 4.1, "Brazos": "izq", "Puntuacion ": 11, "Nombre": "Alguien2"}, {"Tiempo": 3.211, "Brazos": "ambos","Puntuacion ": 1442, "Nombre": "Alguien3"}]

# make dummy for categorical variables
transformer = DictVectorizer()
transformer.fit_transform(data).toarray()

Out[168]:
array([[ 0.0000e+00, 1.0000e+00, 0.0000e+00, 1.0000e+00, 0.0000e+00,
0.0000e+00, 1.1200e+02, 2.1000e+00],
[ 0.0000e+00, 0.0000e+00, 1.0000e+00, 0.0000e+00, 1.0000e+00,
0.0000e+00, 1.1000e+01, 4.1000e+00],
[ 1.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
1.0000e+00, 1.4420e+03, 3.2110e+00]])

transformer.get_feature_names()

Out[170]:
['Brazos=ambos',
'Brazos=der',
'Brazos=izq',
'Nombre=Alguien1',
'Nombre=Alguien2',
'Nombre=Alguien3',
'Puntuacion ',
'Tiempo']

所以你看,Out[168] 中的每条记录都有 8 列,前 3 列是 Brazos 的分类虚拟(查看 中的特征名称Out[170]), 接下来的三个是Nombre 的虚拟值,最后两个是连续数值PuntuacionTiempo (它不需要任何转换并保持原样。

# to continue to fit the model, transform your raw JSON data to numeric value
X = transformer.fit_transform(data)
# import your estimator
from sklearn.naive_bayes import BernoulliNB
estimator = BernoulliNB()
# then start to fit and predict
# NOTE! require your y labels
estimator.fit(X, y)

关于python - SciKit-learn (python)--创建我的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31055384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com