画像データがある場合、画像を適切に前処理して特徴量として使用します。例えば、画像をリサイズし、正規化します。 商品名やその他のテキストデータがある場合、テキストをトークン化して数値特徴量に変換します。 カテゴリカルなデータがある場合、エンコーディングして数値データに変換します。

以下は、画像データ、テキストデータ、およびカテゴリカルデータを前処理して特徴量として使用する方法の例です。

python
import cv2 import numpy as np from sklearn.feature_extraction.text import CountVectorizer from sklearn.preprocessing import LabelEncoder # 画像データの前処理 def preprocess_image(image_path, target_size=(128, 128)): # 画像を読み込み、リサイズして正規化 image = cv2.imread(image_path) image = cv2.resize(image, target_size) # 画像サイズを指定のサイズにリサイズ image = image.astype("float") / 255.0 # 正規化(0から1の範囲に) return image # テキストデータの前処理 def preprocess_text(text_data): # テキストデータをトークン化して数値特徴量に変換 vectorizer = CountVectorizer() text_features = vectorizer.fit_transform(text_data).toarray() return text_features # カテゴリカルデータの前処理 def preprocess_categorical(categorical_data): # カテゴリカルデータをエンコーディングして数値データに変換 label_encoder = LabelEncoder() encoded_categorical_data = label_encoder.fit_transform(categorical_data) return encoded_categorical_data # 画像データのパス image_paths = ["image1.jpg", "image2.jpg", "image3.jpg"] # 画像データの前処理 preprocessed_images = [preprocess_image(image_path) for image_path in image_paths] # テキストデータ text_data = ["product name 1", "product name 2", "product name 3"] # テキストデータの前処理 preprocessed_text = preprocess_text(text_data) # カテゴリカルデータ categorical_data = ["category1", "category2", "category3"] # カテゴリカルデータの前処理 preprocessed_categorical = preprocess_categorical(categorical_data) # それぞれの特徴量を結合 all_features = np.concatenate([preprocessed_images, preprocessed_text, preprocessed_categorical], axis=1) print("All features shape:", all_features.shape)

この例では、画像データはOpenCVを使用してリサイズされ、テキストデータはCountVectorizerを使用してトークン化され、カテゴリカルデータはLabelEncoderを使用してエンコーディングされます。最後に、それぞれの特徴量が結合され、モデルの入力として使用されます。

未分類

Posted by ぼっち