前提・実現したいこと
画像を分類するモデルを作りたい。
発生している問題・エラーメッセージ
モデルをfitさせる時にエラーで実行できない・
ValueError: Found array with dim 3. Estimator expected <= 2.
該当のソースコード
python
1import pandas as pd 2import numpy as np 3import glob 4import cv2 5from PIL import Image 6 7df2 = pd.read_csv("data2.csv") 8files = glob.glob("/Users/a440/Desktop/happy_images/*") 9 10df_files = pd.DataFrame({"data":files}) 11df2["data"] = "/Users/a440/Desktop/happy_images/" + df2["data"] + ".jpg" 12df2 = df2.drop("Unnamed: 0", axis=1) 13df2.to_csv("data2-2.csv") 14 15df_join = pd.merge(df2, df_files, how="inner",on="data",indicator=True) 16 17#yの設定 18smiles =df_join["smile"] 19target = [] 20 21for smile in smiles: 22 data = np.asarray(smile) 23 target.append(data) 24y = np.array(target) 25 26 27#xの設定 28titles = df_join["data"] 29 30photo_array = [] 31 32for title in titles: 33 bgr = Image.open(title) 34 35 bgr = bgr.convert("RGB") 36 bgr = bgr.resize((50, 50)) 37 bgr = np.asarray(bgr) 38 photo_array.append(bgr) 39 40x = np.array(photo_array) 41 42from sklearn.model_selection import train_test_split 43 44(train_x,test_x, train_y, test_y) = train_test_split(x, y, test_size=0.3, random_state=0) 45 46from sklearn.ensemble import RandomForestClassifier 47 48model = RandomForestClassifier(n_estimators = 100,random_state=0) 49 50model.fit(train_x, train_y)
OUT
1--------------------------------------------------------------------------- 2ValueError Traceback (most recent call last) 3<ipython-input-27-2140cc40a840> in <module> 4 3 model = RandomForestClassifier(n_estimators = 100,random_state=0) 5 4 6----> 5 model.fit(train_x, train_y) 7 8/opt/anaconda3/lib/python3.7/site-packages/sklearn/ensemble/_forest.py in fit(self, X, y, sample_weight) 9 302 ) 10 303 X, y = self._validate_data(X, y, multi_output=True, 11--> 304 accept_sparse="csc", dtype=DTYPE) 12 305 if sample_weight is not None: 13 306 sample_weight = _check_sample_weight(sample_weight, X) 14 15/opt/anaconda3/lib/python3.7/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params) 16 430 y = check_array(y, **check_y_params) 17 431 else: 18--> 432 X, y = check_X_y(X, y, **check_params) 19 433 out = X, y 20 434 21 22/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs) 23 70 FutureWarning) 24 71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)}) 25---> 72 return f(**kwargs) 26 73 return inner_f 27 74 28 29/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) 30 800 ensure_min_samples=ensure_min_samples, 31 801 ensure_min_features=ensure_min_features, 32--> 802 estimator=estimator) 33 803 if multi_output: 34 804 y = check_array(y, accept_sparse='csr', force_all_finite=True, 35 36/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs) 37 70 FutureWarning) 38 71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)}) 39---> 72 return f(**kwargs) 40 73 return inner_f 41 74 42 43/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) 44 639 if not allow_nd and array.ndim >= 3: 45 640 raise ValueError("Found array with dim %d. %s expected <= 2." 46--> 641 % (array.ndim, estimator_name)) 47 642 48 643 if force_all_finite: 49 50ValueError: Found array with dim 3. Estimator expected <= 2.
回答1件
あなたの回答
tips
プレビュー