python - 如何格式化 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
,但它不是一个字符串,所以我似乎无法让它工作,知道如何格式化这个输出吗?
解决方案
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
想打印就打印,应该先把数组元素转换成字符串,然后join;您可以使用单个列表推导执行这两个操作:
pred = clf.predict(X)
[",".join(item) for item in pred.astype(str)]
# ['0', '1']
推荐阅读
- java - 如何让 IntelliJ IDEA 识别 Java Gradle 多项目中的 Python 目录?
- keras - 时间序列的 keras LSTM 模型不起作用
- php - 如何在 PHP 中选择表名?
- java - 如何将应用程序上下文从主要活动传递给实现异步任务的类,以在异步任务中从 onPostExecute() 附加适配器?
- vue.js - 如何在 vue.js 中的特定表格行悬停时显示不同的值?
- html - 如何在 Angular 9 中使用 2 向绑定
- pdf - pdf文件中的“另存为”框
- talend - Talend 开放工作室中的数据服务用于数据集成
- javascript - 在 ReactJS 中更改状态的子属性
- reactjs - setState 函数(功能组件)不会在控制台中更新状态