前提・実現したいこと
KNN法を用いてクラスの予測を行いたいです
https://avinton.com/academy/classification-regression/
上記のURLを参考にしていたのですが、使用しているデータの違いによりエラーが生じたのですが解決方法がいまいちよくわかりません。
発生している問題・エラーメッセージ
ValueError Traceback (most recent call last)
<ipython-input-24-bf143c88e6d6> in <module>
----> 1 predict_label = neigh.predict(test_Xy)
~\anaconda3\lib\site-packages\sklearn\neighbors_classification.py in predict(self, X)
171 X = check_array(X, accept_sparse='csr')
172
--> 173 neigh_dist, neigh_ind = self.kneighbors(X)
174 classes_ = self.classes_
175 _y = self._y
~\anaconda3\lib\site-packages\sklearn\neighbors_base.py in kneighbors(self, X, n_neighbors, return_distance)
661 delayed_query(
662 self._tree, X[s], n_neighbors, return_distance)
--> 663 for s in gen_even_slices(X.shape[0], n_jobs)
664 )
665 else:
~\anaconda3\lib\site-packages\joblib\parallel.py in call(self, iterable)
1002 # remaining jobs.
1003 self._iterating = False
-> 1004 if self.dispatch_one_batch(iterator):
1005 self._iterating = self._original_iterator is not None
1006
~\anaconda3\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, iterator)
833 return False
834 else:
--> 835 self._dispatch(tasks)
836 return True
837
~\anaconda3\lib\site-packages\joblib\parallel.py in _dispatch(self, batch)
752 with self._lock:
753 job_idx = len(self._jobs)
--> 754 job = self._backend.apply_async(batch, callback=cb)
755 # A job can complete so quickly than its callback is
756 # called before we get here, causing self._jobs to
~\anaconda3\lib\site-packages\joblib_parallel_backends.py in apply_async(self, func, callback)
207 def apply_async(self, func, callback=None):
208 """Schedule a func to be run"""
--> 209 result = ImmediateResult(func)
210 if callback:
211 callback(result)
~\anaconda3\lib\site-packages\joblib_parallel_backends.py in init(self, batch)
588 # Don't delay the application, to avoid keeping the input
589 # arguments in memory
--> 590 self.results = batch()
591
592 def get(self):
~\anaconda3\lib\site-packages\joblib\parallel.py in call(self)
254 with parallel_backend(self._backend, n_jobs=self._n_jobs):
255 return [func(*args, **kwargs)
--> 256 for func, args, kwargs in self.items]
257
258 def len(self):
~\anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
254 with parallel_backend(self._backend, n_jobs=self._n_jobs):
255 return [func(*args, **kwargs)
--> 256 for func, args, kwargs in self.items]
257
258 def len(self):
~\anaconda3\lib\site-packages\sklearn\neighbors_base.py in _tree_query_parallel_helper(tree, *args, **kwargs)
488 under PyPy.
489 """
--> 490 return tree.query(*args, **kwargs)
491
492
sklearn\neighbors_binary_tree.pxi in sklearn.neighbors._kd_tree.BinaryTree.query()
ValueError: query data dimension must match training data dimension
該当のソースコード
import keras
keras.version
import os
data_dir = 'C:/Users/user/Documents/EMG_data_for_gestures-master/1_raw_data_13-12_22.03.16.csv'
f = open(data_dir, encoding="utf8")
data = f.read()
f.close()
lines = data.split('\n')
header = lines[0].split(',')
lines = lines[1:63196]
print(header)
print(len(lines))
import numpy as np
float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):
values = [float(x) for x in line.split(',')[1:]]
float_data[i, :] = values
temp = float_data[:, 8]
plt.plot(range(len(temp)), temp)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
from sklearn import svm, metrics, model_selection
csv = pd.read_csv("C:/Users/user/Documents/EMG_data_for_gestures-master/1_raw_data_13-12_22.03.16.csv")
csv_data=csv[["time","channel1","channel2","channel3","channel4","channel5","channel6","channel7","channel8","class"]]
csv_label=csv["class"]
train_data, test_data, train_label, test_label =
model_selection.train_test_split(csv_data, csv_label)
neigh = KNeighborsClassifier().fit(csv_data, csv_label)
test_Xy = np.array([np.random.rand(10) * 6 - 3, np.random.rand(10) * 4 - 2]).T
predict_label = neigh.predict(test_Xy)
補足情報(FW/ツールのバージョンなど)
Windows10で、kerasのバージョンは2.3.1です
エラーは最後の一行で起きてます
あなたの回答
tips
プレビュー