Unknown label type: 'continuous' error when training kNN
I'm teaching the nearest neighbor method.
On a regular DataFrame of attributes, everything works well. But, when I scale the signs, an error occurs.
Here is the code:
y = data['Sort'].astype(float)
X = data.drop('Sort', axis=1)
X = data.astype(float)
X_scale = sk.preprocessing.scale(X)
y_scale = sk.preprocessing.scale(y)
I output the optimal number of neighbors and the evaluation of the algorithm:
kMeans_scale = []
for k in range(1,51):
kn = KNeighborsClassifier(n_neighbors=k)
kn.fit(X_scale, y_scale)
array = cross_val_score(estimator = kn, X=X_scale, y=y_scale, cv=kf, scoring = 'accuracy')
kMeans_scale.append(m)
m = max(kMeans_scale)
print(np.round(m,decimals = 2))
s = kMeans_scale.index(m)
print(s+1)
Mistake:
ValueError Traceback (most recent call last)
<ipython-input-134-af5b3598c259> in <module>
2 for k in range(1,51):
3 kn = KNeighborsClassifier(n_neighbors=k)
----> 4 kn.fit(X_scale, y_scale)
5 array = cross_val_score(estimator = kn, X=X_scale, y=y_scale, cv=kf, scoring = 'accuracy')
6 kMeans_scale.append(m)
~\Anaconda3\lib\site-packages\sklearn\neighbors\base.py in fit(self, X, y)
903 self.outputs_2d_ = True
904
--> 905 check_classification_targets(y)
906 self.classes_ = []
907 self._y = np.empty(y.shape, dtype=np.int)
~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
169 if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
170 'multilabel-indicator', 'multilabel-sequences']:
--> 171 raise ValueError("Unknown label type: %r" % y_type)
172
173
What can this be related to?
ValueError: Unknown label type: 'continuous'
1
2 answers
The desired column y
does not need to be scaled. By doing this, you turn classes (discrete integers) into real numbers. This does not give any gain and in addition you will have to decode the scaled labels back to the original classes.
1
Author: MaxU, 2019-06-20 15:14:38
Delete it:
y_scale = sk.preprocessing.scale(y)
1
Author: Alexandr Groshev, 2020-02-02 09:11:27