質問編集履歴

1

ソースコードとエラーの追加

2019/10/06 14:57

投稿

rikubon_
rikubon_

スコア39

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,51 @@
8
8
 
9
9
  ```
10
10
 
11
+ ValueError Traceback (most recent call last)
12
+
13
+ <ipython-input-106-badfaf7f9db2> in <module>
14
+
15
+ 2 lr = LogisticRegression()
16
+
17
+ 3 lr.fit(x_train, y_train)
18
+
19
+ ----> 4 lr.predict(x_test)
20
+
21
+
22
+
23
+ /opt/conda/lib/python3.6/site-packages/sklearn/linear_model/base.py in predict(self, X)
24
+
25
+ 287 Predicted class label per sample.
26
+
27
+ 288 """
28
+
29
+ --> 289 scores = self.decision_function(X)
30
+
31
+ 290 if len(scores.shape) == 1:
32
+
33
+ 291 indices = (scores > 0).astype(np.int)
34
+
35
+
36
+
37
+ /opt/conda/lib/python3.6/site-packages/sklearn/linear_model/base.py in decision_function(self, X)
38
+
39
+ 268 if X.shape[1] != n_features:
40
+
41
+ 269 raise ValueError("X has %d features per sample; expecting %d"
42
+
43
+ --> 270 % (X.shape[1], n_features))
44
+
45
+ 271
46
+
47
+ 272 scores = safe_sparse_dot(X, self.coef_.T,
48
+
49
+
50
+
11
- X has 4 features per sample; expecting 5
51
+ ValueError: X has 4 features per sample; expecting 5
52
+
53
+
54
+
55
+
12
56
 
13
57
 
14
58
 
@@ -24,7 +68,107 @@
24
68
 
25
69
  ```ここに言語名を入力
26
70
 
71
+ # 欠損値の補完
72
+
73
+ train_age_mean = train['Age'].mean()
74
+
75
+ train.fillna(value={'Age':train_age_mean}, inplace=True)
76
+
77
+ train['Age'] = train['Age'].astype(int)
78
+
79
+ # 特徴量の削除
80
+
81
+ train.drop('PassengerId', axis=1, inplace=True)
82
+
83
+ train.drop('Name', axis=1, inplace=True)
84
+
85
+ train.drop('Ticket', axis=1, inplace=True)
86
+
87
+ train.drop('Cabin', axis=1, inplace=True)
88
+
89
+ train.drop('Embarked', axis=1, inplace=True)
90
+
91
+ # 特徴量の値の変化
92
+
93
+ train.replace({'male':0, 'female':0}, inplace=True)
94
+
95
+ # 特徴量エンジニアリング
96
+
97
+ train['familysize'] = train['SibSp'] + train['Parch'] + 1
98
+
99
+ train.drop('SibSp', axis=1, inplace=True)
100
+
101
+ train.drop('Parch', axis=1, inplace=True)
102
+
103
+ #train['Fare'] = train['Fare'].astype(int)
104
+
105
+ train.drop(train.columns[np.isnan(train).any()], axis=1, inplace=True)
106
+
107
+
108
+
109
+ # 欠損値の補完
110
+
111
+ test_age_mean = test['Age'].mean()
112
+
113
+ test.fillna(value={'Age':test_age_mean}, inplace=True)
114
+
115
+ test['Age'] = test['Age'].astype(int)
116
+
117
+ # 特徴量の削除
118
+
119
+ test.drop('PassengerId', axis=1, inplace=True)
120
+
121
+ test.drop('Name', axis=1, inplace=True)
122
+
123
+ test.drop('Ticket', axis=1, inplace=True)
124
+
125
+ test.drop('Cabin', axis=1, inplace=True)
126
+
127
+ test.drop('Embarked', axis=1, inplace=True)
128
+
129
+ # 特徴量の値の変化
130
+
131
+ test.replace({'male':0, 'female':0}, inplace=True)
132
+
133
+ # 特徴量エンジニアリング
134
+
135
+ test['familysize'] = test['SibSp'] + test['Parch'] + 1
136
+
137
+ test.drop('SibSp', axis=1, inplace=True)
138
+
139
+ test.drop('Parch', axis=1, inplace=True)
140
+
141
+ #train['Fare'] = train['Fare'].astype(int)
142
+
143
+ test.drop(test.columns[np.isnan(test).any()], axis=1, inplace=True)
144
+
145
+
146
+
147
+ train
148
+
149
+
150
+
151
+ # 説明変数と目的変数の定義
152
+
153
+ train = train[train.columns[::-1]]
154
+
155
+ x_train = train.loc[:, :'Pclass']
156
+
157
+ y_train = train.loc[:, 'Survived']
158
+
159
+ x_test = test
160
+
27
- ソースコード
161
+ x_test
162
+
163
+
164
+
165
+ # モデルの作成
166
+
167
+ lr = LogisticRegression()
168
+
169
+ lr.fit(x_train, y_train)
170
+
171
+ lr.predict(x_test)
28
172
 
29
173
  ```
30
174