1. 程式人生 > >ValueError: Unknown label type: 'continuous'

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)