下面的代码有什么错误?如何解决错误

时间:2019-09-26 13:17:08

标签: python

import numpy as np
import pandas as pd

dataset=pd.read_csv('iris.csv')
dataset.shape
dataset.head(10)
feature_column = ['sepal_length','sepal_width','petal_length','petal_width']
x=dataset[feature_column].values
=dataset['species'].values
dataset[feature_column].values
dataset['species'].values

from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
y=le.fit_transform(y)
y[:10]

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

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-65776283812c> in <module>
      1 from sklearn.model_selection import train_test_split
----> 2 X_train, y_train,X_test, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

NameError: name 'X' is not defined

2 个答案:

答案 0 :(得分:2)

错误非常具体...它来自此行:

<a [routerLink]="['',{ outlets: { center: ['gi', 10] } }]" > GI Link </a>

您将X_train, y_train,X_test, y_test = train_test_split(X, y, test_size=0.2, random_state=0) (大写)作为第一个参数传递给X 函数,但之前未定义。我相信您打算改用train_test_split()(小写)。

PS:请注意,根据PEP8,大写名称只能用于常量:PEP8 constants

答案 1 :(得分:0)

在您的代码中,您从未定义X,而是定义了x。请记住,python会区分大小写。 因此,您应该尝试将x重新定义为X,或者将x传递给函数。

X_train, y_train,X_test, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

否则:

X=dataset[feature_column].values
X_train, y_train,X_test, y_test = train_test_split(x, y, test_size=0.2, random_state=0)