我正在尝试为我的数据框中的每个类分配颜色,这是我的代码:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
knn = KNeighborsClassifier(n_neighbors=7)
# fitting the model
knn.fit(X_train, y_train)
# predict the response
pred = knn.predict(X_test)
dfp = pd.DataFrame(X_test)
dfp.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']
dfp["PClass"] = pred
pyo.init_notebook_mode()
data = [go.Scatter(x=dfp['SepalLengthCm'], y=dfp['SepalWidthCm'],
text=dfp['PClass'],
mode='markers',
marker=dict(
color=dfp['PClass']))]
layout = go.Layout(title='Chart', hovermode='closest')
fig = go.Figure(data=data, layout=layout)
pyo.iplot(data)
在这里我的 df 看起来像:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm PClass
6.1 2.8 4.7 1.2 Iris-versicolor
5.7 3.8 1.7 0.3 Iris-setosa
7.7 2.6 6.9 2.3 Iris-virginica
所以问题是它没有根据dfp['PClass']
列分配颜色,并且图上的每个点都是相同的颜色:黑色。即使悬停时每个点都根据其类别正确标记。任何想法为什么它不能正常工作?