teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

文章表現

2021/04/13 21:25

投稿

shishi_maru440
shishi_maru440

スコア38

title CHANGED
@@ -1,1 +1,1 @@
1
- 画像データをnumpy配列にし機械学習用のデータセットを作りたい
1
+ 画像データの特徴量抽出し機械学習用のデータセットを作りたい
body CHANGED
@@ -1,8 +1,8 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  自前の画像データから機械学習用のデータセットを作成し学習させたいのですが、
4
- 下記エラーにより実行できません。画像データを配列にするときの方法が不適切では
4
+ モデルをfitさせた時に下記エラーにより実行できません。画像データを配列にする
5
- と考えているのですが、解決方法をご存知の方ご教示ください。
5
+ きの方法が不適切ではと考えているのですが、解決方法をご存知の方ご教示ください。
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
@@ -25,7 +25,6 @@
25
25
  import glob
26
26
  import cv2
27
27
 
28
- #1.anotation_results_form_check.ipynbで作成したdata2.csvを読み込み
29
28
  df2 = pd.read_csv("data2.csv")
30
29
  df2.shape
31
30
 
@@ -53,14 +52,14 @@
53
52
 
54
53
  df_join.to_csv("df_join.csv")
55
54
 
56
- #targetにsmileのカテゴリを代入
55
+ **#targetにsmileのカテゴリを代入
57
56
  smiles =df_join["smile"]
58
57
  target = []
59
58
 
60
59
  for smile in smiles:
61
60
  data = np.asarray(smile)
62
61
  target.append(data)
63
- y = np.array(target)
62
+ y = np.array(target)**
64
63
  y
65
64
  ```
66
65
  ```OUT
@@ -80,8 +79,18 @@
80
79
  bgr = np.asarray(bgr)
81
80
  photo_array.append(bgr)
82
81
  x = np.array(photo_array)
82
+ x[0]
83
83
  ```
84
+ ```OUT
85
+ array([[217, 217, 217, ..., 188, 196, 203],
86
+ [217, 217, 217, ..., 188, 196, 203],
87
+ [217, 217, 217, ..., 188, 196, 204],
88
+ ...,
89
+ [ 4, 4, 4, ..., 44, 43, 42],
90
+ [ 4, 4, 4, ..., 44, 43, 42],
91
+ [ 4, 4, 4, ..., 44, 43, 41]], dtype=uint8)
84
92
  ```
93
+ ```
85
94
  from sklearn import linear_model
86
95
  clf = linear_model.LogisticRegression()
87
96
 

2

文法の修正

2021/04/13 21:25

投稿

shishi_maru440
shishi_maru440

スコア38

title CHANGED
@@ -1,1 +1,1 @@
1
- 画像データから機械学習用のデータセットを作る時にエラーが発生(setting an array element with a sequence.)
1
+ 画像データをnumpy配列にし機械学習用のデータセットを作りたい
body CHANGED
File without changes

1

不具合内容を変更

2021/04/13 14:41

投稿

shishi_maru440
shishi_maru440

スコア38

title CHANGED
@@ -1,1 +1,1 @@
1
- 自前の画像データから機械学習用のデータセットを作りたい
1
+ 画像データから機械学習用のデータセットを作る時にエラーが発生(setting an array element with a sequence.)
body CHANGED
@@ -9,15 +9,12 @@
9
9
  ```
10
10
  ---------------------------------------------------------------------------
11
11
  TypeError Traceback (most recent call last)
12
- <ipython-input-14-70537278e5a3> in <module>
13
- 7
14
- 8 for train_index, test_index in ss.split(photo_array, target):
15
- ----> 9 x_train, x_test = photo_array[train_index], photo_array[test_index]
16
- 10 y_train, y_test = target[train_index], target[test_index]
12
+ TypeError: only size-1 arrays can be converted to Python scalars
17
- 11
18
13
 
19
- TypeError: only integer scalar arrays can be converted to a scalar index
14
+ The above exception was the direct cause of the following exception:
20
15
 
16
+ ValueError Traceback (most recent call last)
17
+ ValueError: setting an array element with a sequence.
21
18
  ```
22
19
 
23
20
  ### 該当のソースコード
@@ -57,46 +54,34 @@
57
54
  df_join.to_csv("df_join.csv")
58
55
 
59
56
  #targetにsmileのカテゴリを代入
60
- target = df_join["smile"]
57
+ smiles =df_join["smile"]
61
- target
58
+ target = []
59
+
60
+ for smile in smiles:
61
+ data = np.asarray(smile)
62
+ target.append(data)
63
+ y = np.array(target)
64
+ y
62
65
  ```
63
66
  ```OUT
64
- 0 1
65
- 1 3
66
- 2 1
67
- 3 3
68
- 4 2
69
- ..
70
- 6436 3
71
- 6437 3
72
- 6438 3
73
- 6439 3
74
- 6440 3
75
- Name: smile, Length: 6441, dtype: int64
67
+ array([1, 3, 1, ..., 3, 3, 3])
76
68
  ```
77
69
  ```
78
-
70
+ #photo_array に画像データの配列を代入
79
71
  titles = df_join["data"]
80
72
 
73
+
74
+ image_size = 50
75
+
81
76
  photo_array = []
82
77
 
83
78
  for title in titles:
84
79
  bgr = cv2.imread(title, cv2.IMREAD_GRAYSCALE)
85
- bgr = np.array(bgr)
80
+ bgr = np.asarray(bgr)
86
81
  photo_array.append(bgr)
87
-
88
- photo_array[0]
82
+ x = np.array(photo_array)
89
83
  ```
90
- ```OUT
91
- array([[217, 217, 217, ..., 188, 196, 203],
92
- [217, 217, 217, ..., 188, 196, 203],
93
- [217, 217, 217, ..., 188, 196, 204],
94
- ...,
95
- [ 4, 4, 4, ..., 44, 43, 42],
96
- [ 4, 4, 4, ..., 44, 43, 42],
97
- [ 4, 4, 4, ..., 44, 43, 41]], dtype=uint8)
98
84
  ```
99
- ```
100
85
  from sklearn import linear_model
101
86
  clf = linear_model.LogisticRegression()
102
87
 
@@ -104,10 +89,76 @@
104
89
  ss = StratifiedKFold(n_splits=10,
105
90
  shuffle=True)
106
91
 
107
- for train_index, test_index in ss.split(photo_array, target):
92
+ for train_index, test_index in ss.split(x, y):
108
- x_train, x_test = photo_array[train_index], photo_array[test_index]
93
+ x_train, x_test = x[train_index], x[test_index]
109
- y_train, y_test = target[train_index], target[test_index]
94
+ y_train, y_test = y[train_index], y[test_index]
110
95
 
111
- clf.fit(x_train, y_train)
112
- print(clf.score(x_test, y_test))
96
+ clf.fit(x_train, y_train)
97
+ print(clf.score(x_test, y_test))
98
+ ```
99
+ ```エラーメッセージ
100
+ ---------------------------------------------------------------------------
101
+ TypeError Traceback (most recent call last)
102
+ TypeError: only size-1 arrays can be converted to Python scalars
103
+
104
+ The above exception was the direct cause of the following exception:
105
+
106
+ ValueError Traceback (most recent call last)
107
+ <ipython-input-15-305a5bf690e8> in <module>
108
+ 10 y_train, y_test = y[train_index], y[test_index]
109
+ 11
110
+ ---> 12 clf.fit(x_train, y_train)
111
+ 13 print(clf.score(x_test, y_test))
112
+ 14
113
+
114
+ /opt/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py in fit(self, X, y, sample_weight)
115
+ 1342 X, y = self._validate_data(X, y, accept_sparse='csr', dtype=_dtype,
116
+ 1343 order="C",
117
+ -> 1344 accept_large_sparse=solver != 'liblinear')
118
+ 1345 check_classification_targets(y)
119
+ 1346 self.classes_ = np.unique(y)
120
+
121
+ /opt/anaconda3/lib/python3.7/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
122
+ 430 y = check_array(y, **check_y_params)
123
+ 431 else:
124
+ --> 432 X, y = check_X_y(X, y, **check_params)
125
+ 433 out = X, y
126
+ 434
127
+
128
+ /opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
129
+ 70 FutureWarning)
130
+ 71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
131
+ ---> 72 return f(**kwargs)
132
+ 73 return inner_f
133
+ 74
134
+
135
+ /opt/anaconda3/lib/python3.7/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, estimator)
136
+ 800 ensure_min_samples=ensure_min_samples,
137
+ 801 ensure_min_features=ensure_min_features,
138
+ --> 802 estimator=estimator)
139
+ 803 if multi_output:
140
+ 804 y = check_array(y, accept_sparse='csr', force_all_finite=True,
141
+
142
+ /opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
143
+ 70 FutureWarning)
144
+ 71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
145
+ ---> 72 return f(**kwargs)
146
+ 73 return inner_f
147
+ 74
148
+
149
+ /opt/anaconda3/lib/python3.7/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, estimator)
150
+ 596 array = array.astype(dtype, casting="unsafe", copy=False)
151
+ 597 else:
152
+ --> 598 array = np.asarray(array, order=order, dtype=dtype)
153
+ 599 except ComplexWarning:
154
+ 600 raise ValueError("Complex data not supported\n"
155
+
156
+ /opt/anaconda3/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
157
+ 81
158
+ 82 """
159
+ ---> 83 return array(a, dtype, copy=False, order=order)
160
+ 84
161
+ 85
162
+
163
+ ValueError: setting an array element with a sequence.
113
164
  ```