我应该如何为 sklearn.naive_bayes.GaussianNB 重新格式化我的数据
How should I reformat my data for sklearn.naive_bayes.GaussianNB
我有一个数据集users
。每个用户都有性别和颜色属性(最喜欢的颜色),等等。我将每种颜色和喜欢该颜色的一种性别的用户总数划分为一个列表:
features_train = [['indigo', 2341], ['yellow', 856], ['lavender', 690], ['yellowgreen', 1208], ['indigo', 565], ['yellow', 103], ['lavender', 571], ['yellowgreen', 234] ...]
在第一个列表中的每个元素的第二个列表中,我说哪个性别代表这个元素:
labels_train = [0, 0, 0, 0, 1, 1, 1, 1, ...]
现在我有了第三个颜色列表:features_test = ['yellow', 'red', ...]
,我需要预测性别。
我必须使用 sklearn
中的 naive_bayes.GaussianNB
函数,我将有更多 users
的属性,但为了解释我的问题,我只使用颜色和性别。所以,我找到了一个官方示例,但我不明白我应该如何重新格式化我的数据集才能使用它们。我应该将我的颜色转换为某种数字表示形式,例如:[[0, 2341], [1, 856]]
或者我应该使用 sklearn
中的其他函数来做到这一点?
import numpy as np
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(features_train, labels_train)
print(clf.predict(features_test))
为了使用scikit-learn对文本文档进行'machine learning',首先需要将文本内容转化为数字特征向量。
最直观的方法是词袋表示——你可以通过像你提到的那样重新格式化你的数据集来解决这个问题。
鉴于您的 'X' 和 'y' 都是一维的,我建议您使用 scikit-learn 中的 LabelEnconder 将您的文本 类 转换为一组数字特征向量。
见下文:
import numpy as np
from sklearn import preprocessing
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
le = preprocessing.LabelEncoder()
#Fit label encoder and return encoded features
features_train_num = le.fit_transform(features_train)
features_test_num = le.transform(features_test)
#Fit label encoder and return encoded labels
labels_train_num = le.fit_transform(labels_train)
labels_test_num = le.transform(labels_test)
clf.fit(features_train_num, labels_train_num)
print(clf.predict(features_test_num))
我有一个数据集users
。每个用户都有性别和颜色属性(最喜欢的颜色),等等。我将每种颜色和喜欢该颜色的一种性别的用户总数划分为一个列表:
features_train = [['indigo', 2341], ['yellow', 856], ['lavender', 690], ['yellowgreen', 1208], ['indigo', 565], ['yellow', 103], ['lavender', 571], ['yellowgreen', 234] ...]
在第一个列表中的每个元素的第二个列表中,我说哪个性别代表这个元素:
labels_train = [0, 0, 0, 0, 1, 1, 1, 1, ...]
现在我有了第三个颜色列表:features_test = ['yellow', 'red', ...]
,我需要预测性别。
我必须使用 sklearn
中的 naive_bayes.GaussianNB
函数,我将有更多 users
的属性,但为了解释我的问题,我只使用颜色和性别。所以,我找到了一个官方示例,但我不明白我应该如何重新格式化我的数据集才能使用它们。我应该将我的颜色转换为某种数字表示形式,例如:[[0, 2341], [1, 856]]
或者我应该使用 sklearn
中的其他函数来做到这一点?
import numpy as np
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(features_train, labels_train)
print(clf.predict(features_test))
为了使用scikit-learn对文本文档进行'machine learning',首先需要将文本内容转化为数字特征向量。
最直观的方法是词袋表示——你可以通过像你提到的那样重新格式化你的数据集来解决这个问题。
鉴于您的 'X' 和 'y' 都是一维的,我建议您使用 scikit-learn 中的 LabelEnconder 将您的文本 类 转换为一组数字特征向量。
见下文:
import numpy as np
from sklearn import preprocessing
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
le = preprocessing.LabelEncoder()
#Fit label encoder and return encoded features
features_train_num = le.fit_transform(features_train)
features_test_num = le.transform(features_test)
#Fit label encoder and return encoded labels
labels_train_num = le.fit_transform(labels_train)
labels_test_num = le.transform(labels_test)
clf.fit(features_train_num, labels_train_num)
print(clf.predict(features_test_num))