【发布时间】:2017-11-22 02:29:30
【问题描述】:
我正在尝试编写从搅拌机中的曲线预测数字的代码。 所以我将曲线转换为 sklearn 使用的矩阵,并尝试预测数字,不幸的是,无论我做什么,预测总是 1。
二维矩阵(它看起来像我在搅拌机中的圆圈):
[[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 25. 25. 0. 0. 0.]
[ 0. 25. 25. 25. 0. 25. 25. 0.]
[ 0. 25. 0. 0. 0. 0. 25. 0.]
[ 0. 25. 0. 0. 0. 0. 25. 0.]
[ 0. 25. 0. 0. 0. 0. 25. 0.]
[ 0. 0. 25. 25. 25. 25. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0.]]
代码:
import bpy
import numpy as np
from sklearn import datasets
from sklearn import svm
import scipy.misc
ob = bpy.context.object
assert ob.type == 'CURVE' # throw error if it's not a curve
curve = ob.data
spline = curve.splines.active # let's assume there's only one
assert spline.type == 'BEZIER' # throw error if it's not a bezier
shortest = None
shortestDist = 10000
shortest_x = None
shortestDist_x = 10000
result = []
for point in spline.bezier_points:
dist = point.co.y
dist_x = point.co.x
if dist < shortestDist : #test if better so far
shortest = point
shortestDist = dist
if dist_x < shortestDist_x : #test if better so far
shortest_x = point
shortestDist_x = dist
print(1 / abs(shortest.co.y))
result.append([shortest, shortestDist, dist, dist_x])
mult_y = 1 / abs(shortest.co.y)
mult_x = 1 / abs(shortest_x.co.x)
point_pos = []
for point in spline.bezier_points:
loc = point.co.y
loc_x = point.co.x
max_y = loc * mult_y
max_x = loc_x * mult_x
point_pos.append([loc, loc_x])
matrix = np.zeros((8, 8))
pixel = []
for index in enumerate(matrix):
matrix_to_co_y = 1 / len(matrix) * index[0]
for index_y in enumerate(matrix[index[0]]):
matrix_to_co_x = 1 / len(matrix) * index_y[0]
#print(matrix_to_co_y)
for point in point_pos:
if matrix_to_co_y > point[0] > matrix_to_co_y - 1 / len(matrix):
if matrix_to_co_x > point[1] > matrix_to_co_x - 1 / len(matrix):
pixel.append([index[0], index_y[0]])
for p in enumerate(pixel):
matrix[p[1][0]][p[1][1]] = 25
flat = np.ravel(matrix)
digits = datasets.load_digits()
clf = svm.SVC(gamma=0.001, C=100)
x,y = digits.data[:-1], digits.target[:-1]
clf.fit(x,y)
print('Prediction:',clf.predict([flat]))
print(matrix)
我不知道我做错了什么。 任何帮助将不胜感激
【问题讨论】:
-
我建议你看看 scikit-learn 教程。在那里,他们展示了手写数字的分类。 scikit-learn.org/stable/tutorial/basic/…如果你能把你的输入变成与数字数据集相同的格式,你应该能够按照教程点对点
-
@KPLauritzen 我试过了,矩阵看起来与训练数据中的矩阵相同
-
如果您尝试预测来自
digits数据集的一些输入会发生什么?另外,您能否尝试使用np.reshape或np.flatten而不是np.ravel。我认为您从列而不是图像矩阵中的行中获取平面向量 -
@KPLauritzen 数据集中的数字工作得很好,我的数组与数字的格式完全相同,但它只返回 1 :(
标签: python machine-learning scikit-learn blender scikits