ValueError: Unknown label type: 'continuous'

原码

def test_KNeighborsRegressor_k_w(*data):
    X_train,X_test,y_train,y_test=data
    Ks=np.linspace(1,y_train.size,num=100,endpoint=False,dtype='int')
    weights=['uniform','distance']
    #绘图
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    for weight in weights:
        training_scores=[]
        testing_scores=[]
        for K in Ks:
            regr=neighbors.KNeighborsRegressor(weights=weight,
                n_neighbors=K)
            regr.fit(X_train,y_train)
            testing_scores.append(regr.score(X_test,y_test))
            training_scores.append(regr.score(X_train,
                y_train))
        ax.plot(Ks,testing_scores,label='testing score:weight=%s'%weight)
        ax.plot(Ks,training_scores,label='training score:weight=%s'%weight)
        ax.legend(loc='best')
        ax.set_xlabel('K')
        ax.set_ylabel('score')
        ax.set_ylim(0,1.05)
        ax.set_title('KNeighborsRegressor')
        plt.show()

test_KNeighborsRegressor_k_w(X_train,X_test,y_train,y_test)

纠正(添加”.astype(‘int’)“)

def test_KNeighborsRegressor_k_w(*data):
    X_train,X_test,y_train,y_test=data
    Ks=np.linspace(1,y_train.size,num=100,endpoint=False,dtype='int')
    weights=['uniform','distance']
    #绘图
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    for weight in weights:
        training_scores=[]
        testing_scores=[]
        for K in Ks:
            regr=neighbors.KNeighborsRegressor(weights=weight,
                n_neighbors=K)
            regr.fit(X_train,y_train.astype('int'))
            testing_scores.append(regr.score(X_test,y_test.astype('int')))
            training_scores.append(regr.score(X_train,
                y_train.astype('int')))
        ax.plot(Ks,testing_scores,label='testing score:weight=%s'%weight)
        ax.plot(Ks,training_scores,label='training score:weight=%s'%weight)
        ax.legend(loc='best')
        ax.set_xlabel('K')
        ax.set_ylabel('score')
        ax.set_ylim(0,1.05)
        ax.set_title('KNeighborsRegressor')
        plt.show()

test_KNeighborsRegressor_k_w(X_train,X_test,y_train,y_test)

版权声明:本文为weixin_39777626原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。