繁体   English   中英

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

[英]How to format scikit-learn output data?

目前正在学习一个机器学习应用程序和一个方法的输出真的让我难过,从来没有见过像这样的输出。

码:

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但它不是一个字符串所以我似乎无法让它工作,任何想法如何格式化这个输出?

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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM