如何格式化scikit-learn输出数据?

时间:2018-08-13 02:53:11

标签: python arrays machine-learning scikit-learn

当前正在学习机器学习应用程序,并且通过方法输出的结果确实使我感到困惑,从未见过这样的输出。

代码:

def IsCloseTogether(data):
    amount_of_data = len(data) #i have an array loaded with examples
    local_feature = np.reshape(data, (amount_of_data,-1)) #changes the array so it would work with the clf.fit
    labels = [1, 0, 0, 0, 1, 1] # 1 means it matches, 0 means it doesn't (supervised learning)
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(local_feature, labels)
    prediction = clf.predict([["111011101"], ["101"]]) #these number strings are the strings im making the machine predict whether they are similar enough to be deemed "similar" or "different"
    return prediction

打印后,我得到以下输出:

[1 0]

尽管数字本身有意义,但理想情况下,我希望这些元素显示为实际的列表元素,例如:

['1','0']

我尝试使用.join,但是它不是字符串,因此似乎无法使其正常工作,不知道如何格式化此输出?

1 个答案:

答案 0 :(得分:3)

clf.predict返回一个Numpy数组:

from sklearn import tree
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)

print(clf.predict(X))
# [0 1]

type(clf.predict(X))
# numpy.ndarray

要根据需要进行打印,应首先将数组元素转换为字符串,然后将它们连接;您可以通过单个列表理解来执行这两种操作:

pred = clf.predict(X)
[",".join(item) for item in pred.astype(str)]
# ['0', '1']