python - sklearn 在尝试预测数字时总是预测 1

标签 python machine-learning scikit-learn blender scikits

我正在尝试编写代码来预测 blender 中曲线的数字。 所以我将曲线转换为矩阵,就像 sklearn 使用的矩阵一样,并尝试预测数字,不幸的是,无论我做什么,预测始终是 1。

二维矩阵(它看起来像我在 blender 中的圆圈):

[[  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)

我不知道我做错了什么。 任何帮助将不胜感激

最佳答案

这可能是您的输入图像或分类器的问题。 要测试问题出在哪里,您可以

1) 尝试使用多个输入图像。尝试为每个数字(0-9)制作一张图像。如果您的分类器对所有这些都预测为“1”,则问题可能出在分类器中。但如果它可以预测其中的一些,那么很可能只是您的单个输入图像造成了麻烦。

2) 尝试使用不同的分类器。几乎任何东西都可以在 digits 数据集上为您提供不错的性能。我尝试使用 RandomForestClassifier,它正确地将您的图像预测为“0”。

概念证明:

import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
my_input = np.array(
 [[  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.]])
iris = datasets.load_iris()
digits = datasets.load_digits()
clf = RandomForestClassifier()
clf.fit(digits.data, digits.target)
clf.predict(my_input.reshape(1, -1))
# Outputs array([0])

关于python - sklearn 在尝试预测数字时总是预测 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47425872/

相关文章:

machine-learning - 向 Keras 中 Flatten() 层的输出添加新功能

python - 如何在 .fit() 方法中对多个标签(trainy)使用一种热编码?

python - 具有显示标准偏差的 n 重交叉验证的精确召回曲线

python - 在Python中生成分布在三个交错半圆形形状的数据集

python - 是否可以为数据框的每一列创建一个额外的 pct_change 列?

python - 在 Mayavi 中以 0 为中心的颜色图

python - PyCharm:Py_Initialize:无法初始化 sys 标准流

python - 获得 100% 的训练准确率,但获得 60% 的测试准确率

python - 如何使用具有不同特征维度的数据集训练 sklearn 分类器?

python - 具有稳健标准误差的 Statsmodels 聚类 Logit 模型