I am trying to make a simple scatterplot and get a KeyError
.
I have tried to see if it is a matter of the feature "group" that contains the four classes, but it is not so I am not sure what is the issue here.
from sklearn.datasets import load_iris
iris = load_iris()
iris_nparray = iris.data
iris_dataframe = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_dataframe["group"] = pd.Series([iris.target_names[k] for k in iris.target], dtype = "category")
colors_palete = {0:"red", 1:"yellow", 2:"blue"}
colors = [colors_palete[c] for c in iris_dataframe["group"]]
simple_scatterplot = iris_dataframe.plot(kind = "scatter",x="petal length (cm)", y="petal width (cm)", c =colors)
Expected:
A nice colorful scatterplot
Actual result:
KeyError Traceback (most recent call last)
<ipython-input-128-818f07044064> in <module>
1 colors_palete = {0:"red", 1:"yellow", 2:"blue"}
----> 2 colors = [colors_palete[c] for c in iris_dataframe["group"]]
3 simple_scatterplot = iris_dataframe.plot(kind = "scatter",x="petal length (cm)", y="petal width (cm)", c =colors)
<ipython-input-128-818f07044064> in <listcomp>(.0)
1 colors_palete = {0:"red", 1:"yellow", 2:"blue"}
----> 2 colors = [colors_palete[c] for c in iris_dataframe["group"]]
3 simple_scatterplot = iris_dataframe.plot(kind = "scatter",x="petal length (cm)", y="petal width (cm)", c =colors)
KeyError: 'setosa'```
The answer is simple: your colour mapping is wrongly defined.
iris_dataframe["group"]
contains ['setosa', 'versicolor', 'virginica']
.
Accordingly, colors_palete
(did you mean "palette"?) should be:
colors_palete = {'setosa': "red", 'versicolor': "yellow", 'virginica': "blue"}