Iris.data.csv - ValueError: x and y must be the same size

Iris.data.csv - ValueError: x and y must be the same size

我正在尝试 运行 在 Google colab 上使用此代码,我收到了 ValueError:x 和 y 的大小必须相同,我尝试了多种方法,但 none 他们工作了。

from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width','class']
df = pd.read_csv('Iris.data.csv', header=None, names=columns)
X = np.array(df.iloc[:, 0:4])   
y = np.array(df['class'])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

plt.scatter(X,y)
plt.show() 

我认为您的问题可能是您对要制作的散点图的类型考虑不够。 X 数组包含 4 个参数的 150 个样本。 y 数组包含每个数据样本的 classes。您希望散点图看起来如何?请记住,散点图只能绘制二维数据,而不是 4 维数据!

我不知道你的 'Iris.data.csv' 文件包含什么,但我使用了来自 scikit-learn as shown in this example.

的鸢尾花数据集的副本

通常,虹膜数据集的散点图 select 四个维度中的两个,并使用不同颜色的点为每个 class 绘制这些维度中的点。

像这样:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets

# Load data
iris = datasets.load_iris()
X = iris.data[:, :4]  # take the first 4 features
assert(X.shape == (150, 4))
y = iris.target
assert(y.shape == (150,))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

classes = np.unique(y)
assert(classes.shape == (3, ))

# Select dimensions to plot
dim1 = X[:, 0]  
dim2 = X[:, 1]

# Make a scatter plot
fig, ax = plt.subplots()
for c in classes:
    pts = (y == c)
    ax.scatter(dim1[pts], dim2[pts])

ax.grid()
plt.show()