You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

拆分鸢尾花(Iris)数据集时出现‘name 'y' is not defined’错误的技术问询

Fixing the 'name 'y' is not defined' Error When Splitting the Iris Dataset

Hey there, let's break down why you're hitting this error and how to fix it quickly!

The Root Cause

That error pops up because the variable y you're passing to train_test_split hasn't been created or assigned any value yet. When working with the Iris dataset, y should represent the target labels (the flower species we want to predict), but right now your code doesn't define what y actually is.

Step-by-Step Fix

Here's how to properly load the dataset and define your X (features) and y (labels) before splitting:

  1. First, load the built-in Iris dataset from scikit-learn
    If you're using the standard Iris dataset included with sklearn, start by importing and loading it:

    from sklearn.datasets import load_iris
    iris = load_iris()
    
  2. Define your feature matrix X and target variable y
    The loaded dataset object has pre-defined attributes for features and labels:

    X = iris.data  # Contains all the numerical features (sepal/petal length/width)
    y = iris.target  # Contains the 3 flower species labels (0, 1, 2)
    
  3. Run your train-test split code again
    Now that both X and y are properly defined, your original split code will work without errors:

    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=1)
    

If You're Using a Custom Iris CSV File

If you're loading the dataset from a CSV instead of sklearn's built-in version, use pandas to extract features and labels correctly:

import pandas as pd
# Load the CSV file
iris_df = pd.read_csv('your_iris_file.csv')
# Separate features (drop the label column) and labels
X = iris_df.drop('species', axis=1)  # Replace 'species' with your actual label column name
y = iris_df['species']

After this, you can run the train_test_split function as usual.

内容的提问来源于stack exchange,提问作者Poonam Narkhede

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:52:24