sklearn SyntaxError:无法分配给运算符

时间:2018-12-05 16:58:56

标签: python machine-learning scikit-learn cross-validation

我正在尝试使用sklearn拆分数据集。但是,我收到语法错误。

import numpy as np
import pandas as pd
from sklearn import cross_validation
X_train, X-test, y_train, y_test=cross_validation.train_test_Split(X_features,y_target,test_size=0.4,random_state=0)

错误:

File "<ipython-input-16-0e0c9ad420f6>", line 4
            X_train, X-test, y_train, y_test=cross_validation.train_test_Split(X_features,y_target,test_size=0.4,random_state=0)

^
SyntaxError: can't assign to operator

3 个答案:

答案 0 :(得分:0)

您不能在此处使用'-'作为变量名。那是python中的运算符。 将其更改为X_test

X_train, X_test, y_train, y_test=cross_validation.train_test_Split(X_features,y_target,test_size=0.4,random_state=0)

答案 1 :(得分:0)

完整的正确版本如下。

@Pradeep提出的那个会出错,因为没有train_test_Split,只有train_test_split带有小写字母s

在您的代码中,您有两个错误 1)大写的s和2的X-test将被识别为减法运算。


使用此功能:

import numpy as np
import pandas as pd
from sklearn import cross_validation

X_train, X_test, y_train, y_test=cross_validation.train_test_split(X_features,y_target,test_size=0.4,random_state=0)

答案 2 :(得分:-1)

from sklearn.model_selection import cross_validate
from sklearn.model_selection import train_test_split

我尝试了一下,它对我很有用,就像魅力一样!!