解决python错误提示“non-default argument follows default argument”

    今天在训练模型的时候无意间遇到了一个错误,这个可能是自己之前没有多去了解过python的参数相关的知识,这个错误还是第一次遇到。所以查了一下资料,在网上找到几个可以参考的链接明白了怎么回事:

参考:
http://stackoverflow.com/questions/17893820/python-default-argument-syntax-error

http://stackoverflow.com/questions/16932825/why-non-default-arguments-cant-follows-default-argument

http://stackoverflow.com/questions/24719368/syntaxerror-non-default-argument-follows-default-argument

    终于明白了为什么报错:就是说我把含有默认值的参数放在了不含默认值的参数的前面,这样问题就好解决了,调换一下参数的位置就好了

    我原始的函数为:

    

def random_predict(model_file='model/svm_model.pkl', X_train, y_train, X_test, y_test)
    clf = joblib.load("model/svm_model.pkl")
    result = []
    for i in range(10):
        lin = random.randint(0, 150)
        prediction_train = clf.predict(X_train[lin])
        prediction_test = clf.predict(X_test[lin])
        print prediction_train
        print '----------------------------------------------------------------'
        print prediction_test
        for i in prediction_train:
            result.append(i)
        for j in prediction_test:
            result.append(j)
    print result
修改为:

def random_predict(X_train, y_train, X_test, y_test, model_file='model/svm_model.pkl'):
    model = joblib.load(model_file)
    clf = joblib.load("model/svm_model.pkl")
    result = []
    for i in range(10):
        lin = random.randint(0, 150)
        prediction_train = clf.predict(X_train[lin])
        prediction_test = clf.predict(X_test[lin])
        print prediction_train
        print '----------------------------------------------------------------'
        print prediction_test
        for i in prediction_train:
            result.append(i)
        for j in prediction_test:
            result.append(j)
    print result
这样修改之后就没有错误了



你可能感兴趣的:(解决python错误提示“non-default argument follows default argument”)