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

时间:2021-07-18 07:00:17

Currently learning a machine learning application and the output by a method has really stumped me, never have I seen an output like this.

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

Code:

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

After printing it I get this output:

打印后我得到这个输出:

[1 0]

Although it the numbers make sense themselves, I ideally would like to the elements to show up as actual list elements like:

虽然数字本身是有意义的,但我理想地希望元素显示为实际的列表元素,如:

['1','0']

I've tried using .join but it's not a string so I can't seem to get it to work, any idea how to format this output?

我尝试过使用.join,但它不是一个字符串,所以我似乎无法让它工作,任何想法如何格式化这个输出?

1 个解决方案

#1


3  

clf.predict returns a Numpy array:

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

To print it as you want, you should first convert the array elements to strings, and then join them; you can perform both operations with a single list comprehension:

要根据需要打印它,首先应该将数组元素转换为字符串,然后加入它们;您可以使用单个列表解析执行这两个操作:

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

#1


3  

clf.predict returns a Numpy array:

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

To print it as you want, you should first convert the array elements to strings, and then join them; you can perform both operations with a single list comprehension:

要根据需要打印它,首先应该将数组元素转换为字符串,然后加入它们;您可以使用单个列表解析执行这两个操作:

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