我的功能之一是一个分类变量,它可以采用 29 种不同的状态。我正在尝试使用一种热编码来转换它,以便我可以使用此功能构建预测模型。以下是我的代码:
enc = preprocessing.OneHotEncoder()
enc.fit([[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28]])
subredditCategory = []
if row[1] == 'Art':
subredditCategory.append(0)
elif row[1] == 'AskReddit':
subredditCategory.append(1)
elif row[1] == 'askscience':
subredditCategory.append(2)
elif row[1] == 'aww':
subredditCategory.append(3)
elif row[1] == 'books':
subredditCategory.append(4)
elif row[1] == 'creepy':
subredditCategory.append(5)
elif row[1] == 'dataisbeautiful':
subredditCategory.append(6)
elif row[1] == 'DIY':
subredditCategory.append(7)
elif row[1] == 'Documentaries':
subredditCategory.append(8)
elif row[1] == 'EarthPorn':
subredditCategory.append(9)
elif row[1] == 'explainlikeimfive':
subredditCategory.append(10)
elif row[1] == 'food':
subredditCategory.append(11)
elif row[1] == 'funny':
subredditCategory.append(12)
elif row[1] == 'gaming':
subredditCategory.append(13)
elif row[1] == 'gifs':
subredditCategory.append(14)
elif row[1] == 'history':
subredditCategory.append(15)
elif row[1] == 'jokes':
subredditCategory.append(16)
elif row[1] == 'LifeProTips':
subredditCategory.append(17)
elif row[1] == 'movies':
subredditCategory.append(18)
elif row[1] == 'music':
subredditCategory.append(19)
elif row[1] == 'pics':
subredditCategory.append(20)
elif row[1] == 'science':
subredditCategory.append(21)
elif row[1] == 'ShowerThoughts':
subredditCategory.append(22)
elif row[1] == 'space':
subredditCategory.append(23)
elif row[1] == 'sports':
subredditCategory.append(24)
elif row[1] == 'tifu':
subredditCategory.append(25)
elif row[1] == 'todayilearned':
subredditCategory.append(26)
elif row[1] == 'videos':
subredditCategory.append(27)
elif row[1] == 'worldnews':
subredditCategory.append(28)
sub = enc.transform([subredditCategory]).toarray()
features.append([row[2], row[3], row[6], row[8], sub])
labels.append(row[9])
但是当我尝试使用特征和标签来训练模型时,如下所示:
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
我收到以下运行时崩溃错误:
ValueError: setting an array element with a sequence.
即生成 clf.fit 线。不知道我做错了什么 - 有什么想法吗?