蓝桥云课 楼赛 第22期 简单手写字符识别分类器 参考答案(没通过???)

def predict(sample):
    """
    参数:
    sample -- 手写字符像素值,列表

    返回:
    pred -- 手写字符预测标签,整型
    """
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import datasets, svm, metrics
    from sklearn.model_selection import train_test_split

    digits = datasets.load_digits()

    ### 补充代码 ###
    #display sample picture
    # sample: img_vector=>img
    sample_img=np.array(sample).reshape(8, 8)
    # plt.gray()
    # plt.matshow(sample_img)
    # plt.show()
    # _, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))
    #     # for ax, image, label in zip(axes, digits.images, digits.target):
    #     #     ax.set_axis_off()
    #     #     ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    #     #     ax.set_title('Training: %i' % label)
    #     # plt.show()
    #training
    # flatten the images
    n_samples = len(digits.images)
    data = digits.images.reshape((n_samples, -1))

    # Create a classifier: a support vector classifier
    clf = svm.SVC(gamma=0.001)

    # Split data into 50% train and 50% test subsets
    X_train, X_test, y_train, y_test = train_test_split(
        data, digits.target, test_size=0.5, shuffle=False)
    #print(sample_img.reshape(1,64).shape)
    # Learn the digits on the train subset
    clf.fit(X_train, y_train)
    pred= clf.predict(sample_img.reshape(1,64))
    return pred

if __name__=="__main__":
    # 示例测试代码
    sample = [0., 0., 6., 14., 4., 0., 0., 0.,
              0., 0., 11., 16., 10., 0., 0., 0.,
              0., 0., 8., 14., 16., 2., 0., 0.,
              0., 0., 1., 12., 12., 11., 0., 0.,
              0., 0., 0., 0., 0., 11., 3., 0.,
              0., 0., 0., 0., 0., 5., 11., 0.,
              0., 0., 1., 4., 4., 7., 16., 2.,
              0., 0., 7., 16., 16., 13., 11., 1.]
    print(predict(sample))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值