hayataka2049 score 25454
2018/10/15 13:23 投稿
最初からnumpy配列として取り扱った方がスマートなコードになると思います。 |
```python |
import pandas as pd |
from sklearn.datasets import load_iris |
from sklearn.model_selection import train_test_split |
from sklearn.svm import SVC |
from sklearn.metrics import accuracy_score |
# irisデータ読み込み |
iris = load_iris() |
X = iris.data |
y = iris.target |
# hold-out データ分割 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4,shuffle = True, |
random_state=1) |
# モデル宣言 |
clf = SVC() |
clf.fit(X_train, y_train) |
# データ予測 |
y_pred = clf.predict(X_test) |
print("正解率 = " , accuracy_score(y_test, y_pred)) |
# 予測データと正解データの比較 |
result = pd.DataFrame({"true":y_test, "pred":y_pred}) |
print(result) |
``` |
``` |
### 追記 |
あとから気づいたのですが、元のコードで |
```python |
y_pred = pd.Series(y_pred) |
``` |
の行を |
```python |
y_pred = pd.Series(y_pred, index=y_test.index) |
``` |
としてindexを揃えてやれば、あとは特に変更なく比較できそうですね・・・。 |