第1章 食品プロセスとAIの基礎

Food Processing and AI Fundamentals

1.1 食品プロセスの特性

食品製造プロセスは、化学プロセスとは異なる独自の特性を持ちます。 原材料の品質変動が大きく(農産物の季節変動、産地差)、微生物制御が重要で、 官能特性(風味、食感、色)の定量化が困難です。AI技術はこれらの課題に対処する強力なツールとなります。

食品プロセスの主要な特徴

📊 コード例1: 原材料品質変動のシミュレーション
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 農産物の季節変動シミュレーション
np.random.seed(42)
months = np.arange(1, 13)
seasons = ['Winter', 'Winter', 'Spring', 'Spring', 'Spring', 'Summer',
           'Summer', 'Summer', 'Fall', 'Fall', 'Fall', 'Winter']

# 糖度の季節変動(夏に高く、冬に低い)
sugar_content = 12 + 3*np.sin(2*np.pi*(months-3)/12) + np.random.normal(0, 0.5, 12)

# 水分含量の季節変動
moisture_content = 85 - 5*np.sin(2*np.pi*(months-6)/12) + np.random.normal(0, 1, 12)

# 可視化
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8))

# 糖度プロット
ax1.plot(months, sugar_content, marker='o', linewidth=2, color='#11998e', label='糖度 (Brix)')
ax1.axhline(y=12, color='gray', linestyle='--', alpha=0.5, label='年間平均')
ax1.fill_between(months, sugar_content - 1, sugar_content + 1, alpha=0.2, color='#11998e')
ax1.set_xlabel('月 (Month)', fontsize=12)
ax1.set_ylabel('糖度 (°Brix)', fontsize=12)
ax1.set_title('農産物の糖度季節変動', fontsize=14, fontweight='bold')
ax1.grid(True, alpha=0.3)
ax1.legend()
ax1.set_xticks(months)

# 水分含量プロット
ax2.plot(months, moisture_content, marker='s', linewidth=2, color='#38ef7d', label='水分含量 (%)')
ax2.axhline(y=85, color='gray', linestyle='--', alpha=0.5, label='年間平均')
ax2.fill_between(months, moisture_content - 2, moisture_content + 2, alpha=0.2, color='#38ef7d')
ax2.set_xlabel('月 (Month)', fontsize=12)
ax2.set_ylabel('水分含量 (%)', fontsize=12)
ax2.set_title('農産物の水分含量季節変動', fontsize=14, fontweight='bold')
ax2.grid(True, alpha=0.3)
ax2.legend()
ax2.set_xticks(months)

plt.tight_layout()
plt.savefig('seasonal_variation.png', dpi=300, bbox_inches='tight')
plt.show()

print("=== 季節変動統計 ===")
print(f"糖度: 平均 {sugar_content.mean():.2f}°Brix, 標準偏差 {sugar_content.std():.2f}°Brix")
print(f"水分含量: 平均 {moisture_content.mean():.2f}%, 標準偏差 {moisture_content.std():.2f}%")
print(f"変動係数: 糖度 {(sugar_content.std()/sugar_content.mean()*100):.2f}%")

1.2 食品プロセスにおけるAIの役割

AI技術は、食品プロセスの複雑性に対処するための様々な手法を提供します:

主要なAI応用分野

  1. 品質予測: 原材料特性から最終製品品質を予測
  2. プロセス最適化: 加熱時間・温度の最適化、エネルギー効率向上
  3. 異常検知: 微生物汚染、異物混入の早期発見
  4. 官能評価: 風味・食感の定量化と予測
  5. トレーサビリティ: 原材料追跡、ロット管理
📊 コード例2: 品質予測モデルの構築(原材料→最終製品品質)
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# 食品製造データの生成(原材料特性→最終製品品質)
np.random.seed(42)
n_samples = 200

# 原材料特性
data = pd.DataFrame({
    '糖度_Brix': np.random.uniform(10, 15, n_samples),
    '水分含量_%': np.random.uniform(80, 90, n_samples),
    '酸度_pH': np.random.uniform(3.0, 4.5, n_samples),
    '加熱温度_C': np.random.uniform(85, 95, n_samples),
    '加熱時間_min': np.random.uniform(10, 30, n_samples),
})

# 最終製品品質(風味スコア: 複雑な非線形関係)
data['風味スコア'] = (
    5 * data['糖度_Brix'] +
    0.5 * data['水分含量_%'] -
    10 * (data['酸度_pH'] - 3.5)**2 +
    0.3 * data['加熱温度_C'] -
    0.1 * data['加熱時間_min']**2 +
    np.random.normal(0, 5, n_samples)
)

# データ分割
X = data.drop('風味スコア', axis=1)
y = data['風味スコア']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Random Forestモデル構築
model = RandomForestRegressor(n_estimators=100, max_depth=10, random_state=42)
model.fit(X_train, y_train)

# 予測と評価
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
cv_scores = cross_val_score(model, X_train, y_train, cv=5, scoring='r2')

print("=== 品質予測モデル性能 ===")
print(f"R² スコア: {r2:.4f}")
print(f"RMSE: {np.sqrt(mse):.4f}")
print(f"交差検証 R² (平均±標準偏差): {cv_scores.mean():.4f} ± {cv_scores.std():.4f}")

# 特徴量重要度
feature_importance = pd.DataFrame({
    '特徴量': X.columns,
    '重要度': model.feature_importances_
}).sort_values('重要度', ascending=False)

print("\n=== 特徴量重要度 ===")
print(feature_importance.to_string(index=False))

# 予測vs実測値プロット
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))

# 予測vs実測値
ax1.scatter(y_test, y_pred, alpha=0.6, s=50, color='#11998e')
ax1.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()],
         'r--', lw=2, label='理想ライン')
ax1.set_xlabel('実測値', fontsize=12)
ax1.set_ylabel('予測値', fontsize=12)
ax1.set_title(f'品質予測モデル (R²={r2:.4f})', fontsize=14, fontweight='bold')
ax1.legend()
ax1.grid(True, alpha=0.3)

# 特徴量重要度
ax2.barh(feature_importance['特徴量'], feature_importance['重要度'], color='#38ef7d')
ax2.set_xlabel('重要度', fontsize=12)
ax2.set_title('特徴量重要度ランキング', fontsize=14, fontweight='bold')
ax2.grid(True, alpha=0.3, axis='x')

plt.tight_layout()
plt.savefig('quality_prediction_model.png', dpi=300, bbox_inches='tight')
plt.show()

1.3 食品安全とHACCP

HACCP(Hazard Analysis and Critical Control Points)は食品安全管理の国際標準です。 AIはHACCPの各ステップを強化し、リアルタイム監視と予測的管理を可能にします。

🔍 HACCP 7原則

  1. 危害要因分析(Hazard Analysis)
  2. 重要管理点(CCP)の決定
  3. 管理基準(CL)の設定
  4. モニタリング方法の設定
  5. 改善措置の設定
  6. 検証方法の設定
  7. 記録と文書化
📊 コード例3: HACCP温度監視システムシミュレーション
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta

# 加熱殺菌プロセスの温度監視シミュレーション
np.random.seed(42)
time_points = 100
time = np.arange(time_points)

# 温度プロファイル(目標: 85°C、管理基準: 83-87°C)
target_temp = 85
temp_profile = target_temp + np.random.normal(0, 1.5, time_points)

# 異常イベントの挿入
temp_profile[30:35] = 80  # 温度低下異常
temp_profile[70:75] = 90  # 温度上昇異常

# 管理基準
CL_lower = 83  # 下限管理基準
CL_upper = 87  # 上限管理基準

# 異常検知
violations = (temp_profile < CL_lower) | (temp_profile > CL_upper)
violation_indices = np.where(violations)[0]

# 可視化
fig, ax = plt.subplots(figsize=(14, 6))

# 温度プロット
ax.plot(time, temp_profile, linewidth=2, color='#11998e', label='実測温度')
ax.axhline(y=target_temp, color='green', linestyle='--', label='目標温度 (85°C)', linewidth=2)
ax.axhline(y=CL_upper, color='red', linestyle='--', label='上限管理基準 (87°C)', linewidth=1.5)
ax.axhline(y=CL_lower, color='red', linestyle='--', label='下限管理基準 (83°C)', linewidth=1.5)

# 管理基準範囲を塗りつぶし
ax.fill_between(time, CL_lower, CL_upper, alpha=0.2, color='green', label='管理基準範囲')

# 異常箇所をハイライト
if len(violation_indices) > 0:
    ax.scatter(violation_indices, temp_profile[violation_indices],
               color='red', s=100, marker='x', linewidths=3,
               label=f'異常検知 ({len(violation_indices)}件)', zorder=5)

ax.set_xlabel('時間 (分)', fontsize=12)
ax.set_ylabel('温度 (°C)', fontsize=12)
ax.set_title('HACCP温度監視システム - 加熱殺菌プロセス', fontsize=14, fontweight='bold')
ax.legend(loc='upper right')
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig('haccp_monitoring.png', dpi=300, bbox_inches='tight')
plt.show()

# 異常レポート
print("=== HACCP 温度監視レポート ===")
print(f"監視時間: {time_points}分")
print(f"目標温度: {target_temp}°C")
print(f"管理基準範囲: {CL_lower}-{CL_upper}°C")
print(f"異常検知件数: {len(violation_indices)}件 ({len(violation_indices)/time_points*100:.1f}%)")
print(f"平均温度: {temp_profile.mean():.2f}°C")
print(f"温度変動 (SD): {temp_profile.std():.2f}°C")

if len(violation_indices) > 0:
    print("\n=== 異常発生時刻と温度 ===")
    for idx in violation_indices[:10]:  # 最初の10件表示
        status = "低温" if temp_profile[idx] < CL_lower else "高温"
        print(f"  時刻 {idx}分: {temp_profile[idx]:.2f}°C ({status})")

1.4 食品プロセスのデータ取得

食品プロセスにおけるデータ取得は、センサー技術とIoTの進歩により飛躍的に向上しています。 温度、圧力、流量などの物理量だけでなく、近赤外分光(NIR)や画像解析による成分・品質データの リアルタイム取得が可能になっています。

📊 コード例4: 多変量プロセスデータの可視化
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# 食品製造プロセスの多変量データ生成
np.random.seed(42)
n_samples = 200

process_data = pd.DataFrame({
    '温度_C': np.random.normal(85, 3, n_samples),
    '圧力_kPa': np.random.normal(150, 10, n_samples),
    '流量_L/min': np.random.normal(50, 5, n_samples),
    'pH': np.random.normal(4.0, 0.3, n_samples),
    '糖度_Brix': np.random.normal(12, 1.5, n_samples),
    '品質スコア': np.random.normal(80, 10, n_samples)
})

# 相関行列
correlation_matrix = process_data.corr()

# ヒートマップ
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))

# 相関行列ヒートマップ
sns.heatmap(correlation_matrix, annot=True, fmt='.2f', cmap='RdYlGn',
            center=0, ax=ax1, square=True, linewidths=1,
            cbar_kws={'label': '相関係数'})
ax1.set_title('プロセス変数間の相関', fontsize=14, fontweight='bold')

# ペアプロット(主要3変数)
selected_cols = ['温度_C', '糖度_Brix', '品質スコア']
for i, col1 in enumerate(selected_cols):
    for j, col2 in enumerate(selected_cols):
        if i < j:
            ax2.scatter(process_data[col1], process_data[col2],
                       alpha=0.5, s=30, color='#11998e')
            ax2.set_xlabel(col1, fontsize=10)
            ax2.set_ylabel(col2, fontsize=10)
            
ax2.set_title('主要プロセス変数の関係', fontsize=14, fontweight='bold')
ax2.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig('process_data_visualization.png', dpi=300, bbox_inches='tight')
plt.show()

print("=== プロセスデータ統計 ===")
print(process_data.describe())

⚠️ 実装時の注意点

まとめ

本章では、食品プロセスの特性とAI応用の基礎を学びました:

次章では、プロセス監視と品質管理の実践的手法を学びます。