【问题标题】:Python Train Test Split [closed]Python火车测试拆分[关闭]
【发布时间】:2020-05-21 02:08:31
【问题描述】:

下面是代码,下面是错误。

from sklearn.model_selection import train_test_split
X-train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)

错误:

runfile('C:/Users/HP/Desktop/Python/P14-Machine-Learning-AZ-Template-Folder/Machine Learning A-Z Template Folder/Part 1 - Data Preprocessing/data_preprocessing_template.py', wdir='C:/Users/HP/Desktop/Python/P14-Machine-Learning-AZ-Template-Folder/Machine Learning A-Z Template Folder/Part 1 - Data Preprocessing')
Traceback (most recent call last):

  File "C:\Users\HP\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-44-136d41c04781>", line 1, in <module>
    runfile('C:/Users/HP/Desktop/Python/P14-Machine-Learning-AZ-Template-Folder/Machine Learning A-Z Template Folder/Part 1 - Data Preprocessing/data_preprocessing_template.py', wdir='C:/Users/HP/Desktop/Python/P14-Machine-Learning-AZ-Template-Folder/Machine Learning A-Z Template Folder/Part 1 - Data Preprocessing')

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/HP/Desktop/Python/P14-Machine-Learning-AZ-Template-Folder/Machine Learning A-Z Template Folder/Part 1 - Data Preprocessing/data_preprocessing_template.py", line 35
    X-train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)
                                                                                           ^
SyntaxError: can't assign to operator

【问题讨论】:

  • 你为什么使用 X-train ?不应该是 X_train 吗?
  • 请添加您的案例、问题、您尝试过的解决方案以及您搜索的解决方案的描述

标签: python train-test-split


【解决方案1】:

阅读https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions。命名 Python 变量时不得使用破折号。命名时,只能使用字母数字和下划线字符。从

更改您的代码
from sklearn.model_selection import train_test_split
X-train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)

到

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)

这将解决您的问题。

【讨论】:

    【解决方案2】:

    问题是您试图在 var 名称处使用破折号 (-) 分配给变量。 在 python 中,变量名不能有破折号。 Python 将它们作为算术运算符联系起来。

    从以下位置更改您的代码:

    from sklearn.model_selection import train_test_split
    X-train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)
    

    到

    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-21
      • 2017-09-05
      • 2020-08-03
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2020-11-29
      • 2018-12-29
      相关资源
      最近更新 更多