回答編集履歴
1
補足追加
test
CHANGED
@@ -51,3 +51,87 @@
|
|
51
51
|
```
|
52
52
|
|
53
53
|
となります
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
---
|
58
|
+
|
59
|
+
**補足**
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```Python
|
64
|
+
|
65
|
+
from sklearn import svm
|
66
|
+
|
67
|
+
from sklearn.metrics import accuracy_score
|
68
|
+
|
69
|
+
import pandas as pd
|
70
|
+
|
71
|
+
import numpy as np
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
#学習データとラベルを準備
|
76
|
+
|
77
|
+
train_data=pd.read_csv("train1.csv",index_col=0)
|
78
|
+
|
79
|
+
print(train_data)
|
80
|
+
|
81
|
+
train_label=pd.read_csv("train_label1.csv",index_col=0)
|
82
|
+
|
83
|
+
print(train_label)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
#テストデータを準備
|
88
|
+
|
89
|
+
test_data = pd.read_csv("test1.csv",index_col=0)
|
90
|
+
|
91
|
+
print(test_data)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
#アルゴリズムを指定
|
96
|
+
|
97
|
+
clf = svm.SVC(C=1, gamma=10)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
#学習
|
102
|
+
|
103
|
+
clf.fit(train_data,train_label)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
#テスト
|
108
|
+
|
109
|
+
test_label = clf.predict(test_data)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
#テスト結果の表示
|
114
|
+
|
115
|
+
print("テストデータ:{0},予測ラベル:{1}".format(test_data,test_label))
|
116
|
+
|
117
|
+
print("正解率= {}".format(accuracy_score(train_label, test_label)))
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
#テストデータにテスト結果を結合
|
122
|
+
|
123
|
+
test_data['Survived'] = test_label
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
#提案1:単にCSVに吐き出したいならばこれで良い
|
128
|
+
|
129
|
+
test_data['Survived'].to_csv('out.csv')
|
130
|
+
|
131
|
+
#提案2:Indexと結果を結合した結果の配列を得たいのであればこうなる
|
132
|
+
|
133
|
+
data = test_data['Survived'].reset_index().values
|
134
|
+
|
135
|
+
print(data)
|
136
|
+
|
137
|
+
```
|