#実現したいこと
4次元データ(iris_learn2)を2次元データ×6にわけ、プロットしました。
その2次元データを用いてSVMをして決定境界を可視化したいと思ってます。
それぞれのグラフ6つ全てにSVMを利用するのはできないのでしょうか。
#データ
SepalLength SepalWidth PetalLength PetalWidth Name
0 7.0 3.2 4.7 1.4 Iris-versicolor
1 6.4 3.2 4.5 1.5 Iris-versicolor
2 6.9 3.1 4.9 1.5 Iris-versicolor
3 5.5 2.3 4.0 1.3 Iris-versicolor
4 6.5 2.8 4.6 1.5 Iris-versicolor
5 6.3 3.3 6.0 2.5 Iris-virginica
6 5.8 2.7 5.1 1.9 Iris-virginica
7 7.1 3.0 5.9 2.1 Iris-virginica
8 6.3 2.9 5.6 1.8 Iris-virginica
9 6.5 3.0 5.8 2.2 Iris-virginica
#テキストエディタのデータ
SepalLength,SepalWidth,PetalLength,PetalWidth,Name
7,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
6.3,3.3,6,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3,5.8,2.2,Iris-virginica
#コード
python
1import pandas as pd 2import matplotlib.pyplot as plt 3import itertools 4from sklearn.svm import LinearSVC 5from sklearn.svm import SVC 6import mglearn 7import numpy as np 8import matplotlib.pyplot as plt 9import pandas as pd 10from IPython.display import display 11 12def main(): 13 iris = pd.read_csv("iris_learn2.csv") 14 print(iris) 15 plt.figure(figsize=(12, 8)) 16 17 for i, (x, y) in enumerate(itertools.combinations(range(4), 2)): 18 plt.subplot(2, 3, i + 1) 19 20 for (name, d), marker, c in zip(iris.groupby('Name'), '>ox', 'rgb'): 21 plt.scatter( 22 d.iloc[:, x], 23 d.iloc[:, y], 24 marker=marker, 25 c=c 26 ) 27 28 linear_svm = LinearSVC().fit(x, y) 29 mglearn.plots.plot_2d_separator(linear_svm, x) 30 mglearn.discrete_scatter(X[:, 0], X[:, 1], y) 31 32 plt.xlabel(iris.columns[x]) 33 plt.ylabel(iris.columns[y]) 34 plt.autoscale() 35 plt.grid() 36 37 38 plt.tight_layout() 39 plt.show() 40 41if __name__ == '__main__': 42 main() 43
#発生している問題・エラーメッセージ
python
1ValueError Traceback (most recent call last) 2<ipython-input-30-78b8e7553d68> in <module> 3 38 4 39 if __name__ == '__main__': 5---> 40 main() 6 7<ipython-input-30-78b8e7553d68> in main() 8 25 c=c 9 26 ) 10---> 27 linear_svm = LinearSVC().fit(x, y) 11 28 mglearn.plots.plot_2d_separator(linear_svm, x) 12 29 mglearn.discrete_scatter(X[:, 0], X[:, 1], y) 13 14C:\ProgramData\Anaconda3\lib\site-packages\sklearn\svm\classes.py in fit(self, X, y, sample_weight) 15 227 X, y = check_X_y(X, y, accept_sparse='csr', 16 228 dtype=np.float64, order="C", 17--> 229 accept_large_sparse=False) 18 230 check_classification_targets(y) 19 231 self.classes_ = np.unique(y) 20 21C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator) 22 754 ensure_min_features=ensure_min_features, 23 755 warn_on_dtype=warn_on_dtype, 24--> 756 estimator=estimator) 25 757 if multi_output: 26 758 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False, 27 28C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 29 543 "Reshape your data either using array.reshape(-1, 1) if " 30 544 "your data has a single feature or array.reshape(1, -1) " 31--> 545 "if it contains a single sample.".format(array)) 32 546 # If input is 1D raise error 33 547 if array.ndim == 1: 34 35ValueError: Expected 2D array, got scalar array instead: 36array=0.0. 37Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
#試したこと
linear_svm = LinearSVC().fit(x, y) mglearn.plots.plot_2d_separator(linear_svm, x) mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
上記のコードを付け加えたところエラーがおきました。
回答1件
あなたの回答
tips
プレビュー