質問編集履歴
1
codeを全部貼りました。All Runしてみましたが、未だにわかりません。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,14 +1,232 @@
|
|
1
|
-
ロジスティック回帰のcode実装をしたくてyoutubeに上がっているものをそのまま写しているのですがYouTubeではできていて自分のPCではエラーが出てしまい意味が分かりません。どなたか分かる方ご教授お願いします。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
ロジスティック回帰のcode実装をしたくてyoutubeに上がっているものをそのまま写しているのですがYouTubeではできていて自分のPCではエラーが出てしまい意味が分かりません。どなたか分かる方ご教授お願いします。codeはipynbファイルをHTMLに書き直してerror箇所まですべて貼りました。
|
2
|
+
|
3
|
+
参考にしたyoutubeはhttps://youtu.be/mMMzDFttZ8Aです。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
```python
|
8
|
+
|
9
|
+
import numpy as np
|
10
|
+
|
11
|
+
import pandas as pd
|
12
|
+
|
13
|
+
from sklearn.datasets import load_iris
|
14
|
+
|
15
|
+
iris=load_iris()
|
16
|
+
|
17
|
+
#load_iris関数でロードしている
|
18
|
+
|
19
|
+
iris
|
20
|
+
|
21
|
+
'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
22
|
+
|
23
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
24
|
+
|
25
|
+
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
26
|
+
|
27
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
28
|
+
|
29
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
30
|
+
|
31
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
32
|
+
|
33
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
|
34
|
+
|
35
|
+
'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),
|
36
|
+
|
37
|
+
'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 150 (50 in each of three classes)\n :Number of Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n \n :Summary Statistics:\n\n ============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n ============== ==== ==== ======= ===== ====================\n sepal length: 4.3 7.9 5.84 0.83 0.7826\n sepal width: 2.0 4.4 3.05 0.43 -0.4194\n petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\n petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n ============== ==== ==== ======= ===== ====================\n\n :Missing Attribute Values: None\n :Class Distribution: 33.3% for each of 3 classes.\n :Creator: R.A. Fisher\n :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n - Many, many more ...',
|
38
|
+
|
39
|
+
'feature_names': ['sepal length (cm)',
|
40
|
+
|
41
|
+
'sepal width (cm)',
|
42
|
+
|
43
|
+
'petal length (cm)',
|
44
|
+
|
45
|
+
'petal width (cm)'],
|
46
|
+
|
47
|
+
'filename': 'C:\Users\mkou0\Anaconda3\lib\site-packages\sklearn\datasets\data\iris.csv'}
|
48
|
+
|
49
|
+
print(iris.target_names)
|
50
|
+
|
51
|
+
#花の種類が格納されている
|
52
|
+
|
53
|
+
['setosa' 'versicolor' 'virginica']
|
54
|
+
|
55
|
+
for data,target in zip(iris.data[:5],iris.target[:5]):
|
56
|
+
|
57
|
+
print(data,target)
|
58
|
+
|
59
|
+
#がくの長さ、幅、花弁の長さ、幅の特徴量
|
60
|
+
|
61
|
+
[5.1 3.5 1.4 0.2] 0
|
62
|
+
|
63
|
+
[4.9 3. 1.4 0.2] 0
|
64
|
+
|
65
|
+
[4.7 3.2 1.3 0.2] 0
|
66
|
+
|
67
|
+
[4.6 3.1 1.5 0.2] 0
|
68
|
+
|
69
|
+
[5. 3.6 1.4 0.2] 0
|
70
|
+
|
71
|
+
df =pd.DataFrame(iris.data,columns=iris.feature_names)
|
72
|
+
|
73
|
+
df['target']=iris.target
|
74
|
+
|
75
|
+
df
|
76
|
+
|
77
|
+
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
|
78
|
+
|
79
|
+
0 5.1 3.5 1.4 0.2 0
|
80
|
+
|
81
|
+
1 4.9 3.0 1.4 0.2 0
|
82
|
+
|
83
|
+
2 4.7 3.2 1.3 0.2 0
|
84
|
+
|
85
|
+
3 4.6 3.1 1.5 0.2 0
|
86
|
+
|
87
|
+
4 5.0 3.6 1.4 0.2 0
|
88
|
+
|
89
|
+
5 5.4 3.9 1.7 0.4 0
|
90
|
+
|
91
|
+
6 4.6 3.4 1.4 0.3 0
|
92
|
+
|
93
|
+
7 5.0 3.4 1.5 0.2 0
|
94
|
+
|
95
|
+
8 4.4 2.9 1.4 0.2 0
|
96
|
+
|
97
|
+
9 4.9 3.1 1.5 0.1 0
|
98
|
+
|
99
|
+
10 5.4 3.7 1.5 0.2 0
|
100
|
+
|
101
|
+
11 4.8 3.4 1.6 0.2 0
|
102
|
+
|
103
|
+
12 4.8 3.0 1.4 0.1 0
|
104
|
+
|
105
|
+
13 4.3 3.0 1.1 0.1 0
|
106
|
+
|
107
|
+
14 5.8 4.0 1.2 0.2 0
|
108
|
+
|
109
|
+
15 5.7 4.4 1.5 0.4 0
|
110
|
+
|
111
|
+
16 5.4 3.9 1.3 0.4 0
|
112
|
+
|
113
|
+
17 5.1 3.5 1.4 0.3 0
|
114
|
+
|
115
|
+
18 5.7 3.8 1.7 0.3 0
|
116
|
+
|
117
|
+
19 5.1 3.8 1.5 0.3 0
|
118
|
+
|
119
|
+
20 5.4 3.4 1.7 0.2 0
|
120
|
+
|
121
|
+
21 5.1 3.7 1.5 0.4 0
|
122
|
+
|
123
|
+
22 4.6 3.6 1.0 0.2 0
|
124
|
+
|
125
|
+
23 5.1 3.3 1.7 0.5 0
|
126
|
+
|
127
|
+
24 4.8 3.4 1.9 0.2 0
|
128
|
+
|
129
|
+
25 5.0 3.0 1.6 0.2 0
|
130
|
+
|
131
|
+
26 5.0 3.4 1.6 0.4 0
|
132
|
+
|
133
|
+
27 5.2 3.5 1.5 0.2 0
|
134
|
+
|
135
|
+
28 5.2 3.4 1.4 0.2 0
|
136
|
+
|
137
|
+
29 4.7 3.2 1.6 0.2 0
|
138
|
+
|
139
|
+
... ... ... ... ... ...
|
140
|
+
|
141
|
+
120 6.9 3.2 5.7 2.3 2
|
142
|
+
|
143
|
+
121 5.6 2.8 4.9 2.0 2
|
144
|
+
|
145
|
+
122 7.7 2.8 6.7 2.0 2
|
146
|
+
|
147
|
+
123 6.3 2.7 4.9 1.8 2
|
148
|
+
|
149
|
+
124 6.7 3.3 5.7 2.1 2
|
150
|
+
|
151
|
+
125 7.2 3.2 6.0 1.8 2
|
152
|
+
|
153
|
+
126 6.2 2.8 4.8 1.8 2
|
154
|
+
|
155
|
+
127 6.1 3.0 4.9 1.8 2
|
156
|
+
|
157
|
+
128 6.4 2.8 5.6 2.1 2
|
158
|
+
|
159
|
+
129 7.2 3.0 5.8 1.6 2
|
160
|
+
|
161
|
+
130 7.4 2.8 6.1 1.9 2
|
162
|
+
|
163
|
+
131 7.9 3.8 6.4 2.0 2
|
164
|
+
|
165
|
+
132 6.4 2.8 5.6 2.2 2
|
166
|
+
|
167
|
+
133 6.3 2.8 5.1 1.5 2
|
168
|
+
|
169
|
+
134 6.1 2.6 5.6 1.4 2
|
170
|
+
|
171
|
+
135 7.7 3.0 6.1 2.3 2
|
172
|
+
|
173
|
+
136 6.3 3.4 5.6 2.4 2
|
174
|
+
|
175
|
+
137 6.4 3.1 5.5 1.8 2
|
176
|
+
|
177
|
+
138 6.0 3.0 4.8 1.8 2
|
178
|
+
|
179
|
+
139 6.9 3.1 5.4 2.1 2
|
180
|
+
|
181
|
+
140 6.7 3.1 5.6 2.4 2
|
182
|
+
|
183
|
+
141 6.9 3.1 5.1 2.3 2
|
184
|
+
|
185
|
+
142 5.8 2.7 5.1 1.9 2
|
186
|
+
|
187
|
+
143 6.8 3.2 5.9 2.3 2
|
188
|
+
|
189
|
+
144 6.7 3.3 5.7 2.5 2
|
190
|
+
|
191
|
+
145 6.7 3.0 5.2 2.3 2
|
192
|
+
|
193
|
+
146 6.3 2.5 5.0 1.9 2
|
194
|
+
|
195
|
+
147 6.5 3.0 5.2 2.0 2
|
196
|
+
|
197
|
+
148 6.2 3.4 5.4 2.3 2
|
198
|
+
|
199
|
+
149 5.9 3.0 5.1 1.8 2
|
200
|
+
|
201
|
+
150 rows × 5 columns
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
x=iris.data[50:,2].reshape(-1,1)
|
206
|
+
|
207
|
+
y=iris.data[50:]-1
|
208
|
+
|
209
|
+
#x,yの50行から二列取り出す
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
from sklearn.linear_model import LogisticRegression
|
214
|
+
|
215
|
+
from sklearn.preprocessing import StandardScaler
|
216
|
+
|
217
|
+
from sklearn.model_selection import train_test_split
|
218
|
+
|
219
|
+
#標準化を行う関数StandardScaler
|
220
|
+
|
221
|
+
scaler=StandardScaler()
|
222
|
+
|
223
|
+
x_scaled=scaler.fit_transform(x)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
X_train,x_test,Y_train,y_test=train_test_split(x_scaled,y,random_state=0)
|
228
|
+
|
229
|
+
log_reg=LogisticRegression().fit(X_train,Y_train)
|
12
230
|
|
13
231
|
C:\Users\mkou0\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
|
14
232
|
|
@@ -18,9 +236,9 @@
|
|
18
236
|
|
19
237
|
ValueError Traceback (most recent call last)
|
20
238
|
|
21
|
-
<ipython-input-1
|
239
|
+
<ipython-input-28-106621ea5937> in <module>
|
22
|
-
|
240
|
+
|
23
|
-
----> 1 log_reg=LogisticRegression().fit(
|
241
|
+
----> 1 log_reg=LogisticRegression().fit(X_train,Y_train)
|
24
242
|
|
25
243
|
|
26
244
|
|
@@ -67,3 +285,5 @@
|
|
67
285
|
|
68
286
|
|
69
287
|
ValueError: bad input shape (75, 4)
|
288
|
+
|
289
|
+
```
|