File size: 24,076 Bytes
e4321e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524ea6a
 
 
 
e4321e1
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
 
 
 
524ea6a
e4321e1
 
 
 
 
 
 
 
 
 
 
524ea6a
 
 
e4321e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524ea6a
 
 
 
 
 
 
 
e4321e1
 
 
 
 
 
 
524ea6a
e4321e1
 
 
 
 
 
 
 
 
 
 
 
 
 
524ea6a
 
e4321e1
 
 
 
 
 
 
524ea6a
 
 
e4321e1
 
 
524ea6a
e4321e1
 
 
524ea6a
 
 
 
 
 
 
e4321e1
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
524ea6a
e4321e1
 
 
 
524ea6a
e4321e1
524ea6a
e4321e1
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
524ea6a
e4321e1
 
 
524ea6a
 
 
 
 
e4321e1
 
 
 
 
 
 
524ea6a
e4321e1
 
 
 
 
 
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
524ea6a
e4321e1
 
524ea6a
e4321e1
 
524ea6a
e4321e1
 
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
524ea6a
e4321e1
 
 
 
 
 
524ea6a
 
 
e4321e1
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
e4321e1
524ea6a
 
 
 
 
 
 
e4321e1
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524ea6a
e4321e1
 
 
 
 
 
 
 
524ea6a
e4321e1
 
 
 
 
 
 
524ea6a
e4321e1
 
 
524ea6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4321e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524ea6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
---
language:
  - en
  - ru
license: mit
tags:
  - pectin
  - chemical-engineering
  - machine-learning
  - regression
  - biotechnology
  - food-technology
  - production-optimization
  - ml-in-chemistry
---

# Pectin Production Models

**Machine Learning Models for Predicting Pectin Production Parameters from Process Conditions**

This repository contains trained machine learning models for predicting pectin quality parameters based on production process conditions. The models were trained on experimental data from various raw materials and extraction methods.

## 🎯 Model Overview

### Performance Summary

| Model | Type | R² Score | MAE | Description |
|-------|------|----------|-----|-------------|
| **Best Model** | Gradient Boosting | 0.9427 | 868.44 | **Best overall model for pectin production** |
| Extra Trees | extra_trees | 0.9135 | 1060.1741 | Extra Trees model for pectin parameter prediction |
| Gradient Boosting | gradient_boosting | 0.9427 | 868.4403 | Gradient Boosting model - best performance for multi-target regression |
| K-Neighbors | k-neighbors | 0.8684 | 1287.5126 | Machine learning model for pectin production |
| Lasso Regression | lasso_regression | 0.3846 | 3702.0325 | Lasso Regression model with L1 regularization |
| Linear Regression | linear_regression | 0.6965 | 3730.7550 | Linear Regression baseline model |
| MultiLayer Perceptron | multilayer_perceptron | 0.8046 | 4253.8431 | Machine learning model for pectin production |
| Random Forest | random_forest | 0.9259 | 978.0065 | Random Forest model for robust pectin quality prediction |
| Ridge Regression | ridge_regression | 0.5553 | 3665.3101 | Ridge Regression model with L2 regularization |
| Support Vector Regression | support_vector_regression | 0.4832 | 6612.2360 | Machine learning model for pectin production |
| XGBoost | xgboost | 0.9203 | 1074.2310 | XGBoost model with excellent performance on tabular data |

### Best Model Performance
- **Average R²**: 0.9427
- **Average MAE**: 868.44
- **Targets Predicted**: 4 parameters simultaneously

## 📊 Model Details

### Target Variables
- `pectin_yield`: Пектиновые вещества, ПВ, % - Pectin yield (%)
- `galacturonic_acid`: Галактуроновая кислота, ГК, % - Galacturonic acid content (%)
- `molecular_weight`: Молекулярная масса, Mw, Д - Molecular weight (Da)
- `esterification_degree`: Степень этерификации, СЭ, % - Esterification degree (%)

### Feature Variables
- `time_min`: Время процесса, t, мин - Extraction time (minutes)
- `temperature_c`: Температура, T, °C - Temperature (°C)
- `pressure_atm`: Давление, P, атм - Pressure (atm)
- `ph`: Кислотность, pH - pH level
- `sample_encoded`: Тип сырья - Raw material type (encoded)
- `method_encoded`: Метод экстракции - Extraction method (encoded: 1 for fast ≤15 min, 0 for slow >15 min)

**Note**: Parameter Т:Ж (соотношение твердое:жидкое) was excluded from model training because it had a constant value of 1:20 across all experiments and therefore carried no predictive information.

## 📋 Experimental Data Examples

### Sample Experimental Data

| Exp | Sample | t, мин | T, °C | P, атм | pH | ПВ, % | ГК, % | Mw, Д | СЭ, % |
|-----|--------|--------|-------|--------|-----|-------|-------|-------|-------|
| 1 | ЯП(М) | 7 | 120 | 2.08 | 2.0 | 25.864 | 52.706 | 103773.64 | 71.17 |
| 2 | ЯП(М) | 7 | 120 | 1.74 | 2.08 | 24.83 | 51.645 | 103098.49 | 70.015 |
| 3 | Абр. | 5 | 130 | 2.09 | 1.74 | 14.755 | 67.55 | 127235.35 | 82.813 |
| 4 | ЯП(М) | 7 | 120 | 2.05 | 2.0 | 26.353 | 53.804 | 105994.85 | 65.415 |

### Raw Material Types

| Code | Full Name | Type |
|------|-----------|------|
| Абр. | Абрикосовый (Apricot) | Fruit |
| Рв. | Ревень (Rhubarb) | Vegetable |
| Айв. | Айвы (Quince) | Fruit |
| Ткв. | Тыквенный (Pumpkin) | Vegetable |
| КрП | Корзинка подсолнечника (Sunflower head) | Plant |
| ЯП(Ф) | Яблочный пектин Файзобод (Apple Faizobod) | Fruit |
| ЯП(М) | Яблочный пектин Муминобод (Apple Muminobod) | Fruit |

## 🚀 Quick Start

### Installation
```bash
pip install transformers huggingface-hub scikit-learn xgboost pandas numpy joblib tabulate
```

### Basic Usage

```python
from huggingface_hub import hf_hub_download
import joblib
import pandas as pd
import numpy as np
import pickle

import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="sklearn")

# Download model and supporting files
model_path = hf_hub_download(
    repo_id="arabovs-ai-lab/PectinProductionModels",
    filename="best_model/model.pkl",
    repo_type="model"
)

scaler_path = hf_hub_download(
    repo_id="arabovs-ai-lab/PectinProductionModels", 
    filename="scaler.pkl",
    repo_type="model"
)

encoder_path = hf_hub_download(
    repo_id="arabovs-ai-lab/PectinProductionModels",
    filename="label_encoder.pkl", 
    repo_type="model"
)

# Load artifacts
model = joblib.load(model_path)
scaler = joblib.load(scaler_path)
with open(encoder_path, 'rb') as f:
    label_encoder = pickle.load(f)

# Prepare input data (Т:Ж parameter is not required as it was constant)
input_data = {
    'sample': 'Айв.', 
    'time_min': 5, 
    'temperature_c': 120, 
    'pressure_atm': 1.0, 
    'ph': 2.5
}

# Create DataFrame
df = pd.DataFrame([input_data])

# Preprocess: encode sample type
df['sample_encoded'] = label_encoder.transform([input_data['sample']])[0]

# Create method_encoded feature based on extraction time
df['method_encoded'] = 1 if input_data['time_min'] <= 15 else 0

# Select features in correct order
features = ['time_min', 'temperature_c', 'pressure_atm', 'ph', 'sample_encoded', 'method_encoded']
X = df[features]

# Scale features
X_scaled = scaler.transform(X)

# Make prediction
predictions = model.predict(X_scaled)

# Create results dictionary
results = {}
target_names = ['pectin_yield', 'galacturonic_acid', 'molecular_weight', 'esterification_degree']
for i, target in enumerate(target_names):
    results[target] = predictions[0, i]

print("Prediction results:")
for target, value in results.items():
    print(f"  {target}: {value:.4f}")
```

## 🔬 Advanced Model Comparison System

For comprehensive comparison of all available models, use the `PectinPredictor` class:

```python
import pandas as pd
import numpy as np
from huggingface_hub import hf_hub_download
import joblib
import pickle
import warnings
from sklearn.exceptions import InconsistentVersionWarning
from tabulate import tabulate

# Suppress sklearn version compatibility warnings
warnings.filterwarnings("ignore", category=UserWarning, module="sklearn")
warnings.filterwarnings("ignore", category=UserWarning, module="xgboost")

class PectinPredictor:
    """
    A machine learning model for predicting pectin production parameters
    from experimental conditions using pre-trained models from Hugging Face Hub.
    """
    
    # Available models with descriptions and metadata
    AVAILABLE_MODELS = {
        "best_model": {
            "subfolder": "best_model",
            "description": "🎯 Best overall model (Gradient Boosting) - optimal performance",
            "color": "#FF6B6B"
        },
        "gradient_boosting": {
            "subfolder": "gradient_boosting", 
            "description": "📈 Gradient Boosting - best for multi-task regression",
            "color": "#4ECDC4"
        },
        "random_forest": {
            "subfolder": "random_forest",
            "description": "🌲 Random Forest - reliable and stable",
            "color": "#45B7D1"
        },
        "xgboost": {
            "subfolder": "xgboost", 
            "description": "⚡ XGBoost - high performance on tabular data",
            "color": "#96CEB4"
        },
        "linear_regression": {
            "subfolder": "linear_regression",
            "description": "📊 Linear Regression - basic linear model",
            "color": "#FECA57"
        },
        "extra_trees": {
            "subfolder": "extra_trees",
            "description": "🌳 Extra Trees - extreme random forests",
            "color": "#FF9FF3"
        },
        "k_neighbors": {
            "subfolder": "k-neighbors",
            "description": "📏 K-Neighbors - nearest neighbors method",
            "color": "#54A0FF"
        },
        "lasso_regression": {
            "subfolder": "lasso_regression",
            "description": "🎯 Lasso Regression - L1 regularization",
            "color": "#5F27CD"
        },
        "multilayer_perceptron": {
            "subfolder": "multilayer_perceptron", 
            "description": "🧠 Neural Network MLP - multilayer perceptron",
            "color": "#00D2D3"
        },
        "ridge_regression": {
            "subfolder": "ridge_regression",
            "description": "🏔️ Ridge Regression - L2 regularization", 
            "color": "#FF9F43"
        },
        "support_vector_regression": {
            "subfolder": "support_vector_regression",
            "description": "🔗 Support Vector Regression - support vector method",
            "color": "#A3CB38"
        }
    }
    
    def __init__(self, repo_id="arabovs-ai-lab/PectinProductionModels"):
        """Initialize the predictor with model repository ID."""
        self.repo_id = repo_id
        self.model = None
        self.scaler = None
        self.label_encoder = None
        # Model input features (after preprocessing)
        self.feature_columns = ['time_min', 'temperature_c', 'pressure_atm', 'ph', 'sample_encoded', 'method_encoded']
        # Model output targets (pectin characteristics)
        self.target_columns = ['pectin_yield', 'galacturonic_acid', 'molecular_weight', 'esterification_degree']
        
    def load_from_hub(self, model_type="best_model"):
        """
        Load model, scaler, and label encoder from Hugging Face Hub repository.
        
        Args:
            model_type: Key from AVAILABLE_MODELS to load specific model
        """
        if model_type not in self.AVAILABLE_MODELS:
            raise ValueError(f"Model type '{model_type}' not found. Available: {list(self.AVAILABLE_MODELS.keys())}")
            
        model_info = self.AVAILABLE_MODELS[model_type]
        
        # Download and load the specified model
        model_path = hf_hub_download(
            repo_id=self.repo_id,
            filename=f"{model_info['subfolder']}/model.pkl",
            repo_type="model"
        )
        
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=UserWarning)
            self.model = joblib.load(model_path)
        
        # Download and load the feature scaler for data normalization
        scaler_path = hf_hub_download(
            repo_id=self.repo_id,
            filename="scaler.pkl", 
            repo_type="model"
        )
        self.scaler = joblib.load(scaler_path)
        
        # Download and load the label encoder for sample type conversion
        encoder_path = hf_hub_download(
            repo_id=self.repo_id,
            filename="label_encoder.pkl",
            repo_type="model"
        )
        with open(encoder_path, 'rb') as f:
            self.label_encoder = pickle.load(f)

    def prepare_dataframe(self, df):
        """
        Rename DataFrame columns from Russian to English to match model expectations.
        """
        column_mapping = {
            'Образец \nпектина': 'sample',
            't, мин': 'time_min', 
            'T, °C': 'temperature_c',
            'P, атм': 'pressure_atm',
            'pH': 'ph'
        }
        return df.rename(columns=column_mapping)

    def preprocess_input(self, input_df):
        """
        Preprocess input data for model prediction.
        Applies feature engineering, encoding, and scaling.
        """
        processed_df = input_df.copy()
        
        # Convert sample names to numeric codes using trained label encoder
        processed_df['sample_encoded'] = self.label_encoder.transform(processed_df['sample'])
        
        # Create binary feature indicating extraction method based on time
        processed_df['method_encoded'] = np.where(processed_df['time_min'] <= 15, 1, 0)
        
        # Select features in correct order and apply scaling
        X = processed_df[self.feature_columns]
        X_scaled = self.scaler.transform(X)
        
        return X_scaled
            
    def predict_batch(self, input_df, model_type="best_model"):
        """
        Generate predictions for multiple experimental conditions.
        
        Args:
            input_df: DataFrame containing experimental parameters
            model_type: Which model to use for prediction
            
        Returns:
            Original DataFrame augmented with prediction columns
        """
        # Load specified model if not already loaded or different from current
        if self.model is None or model_type != getattr(self, '_current_model', None):
            self.load_from_hub(model_type)
            self._current_model = model_type
            
        # Preprocess input data
        X_scaled = self.preprocess_input(input_df)
        
        # Generate predictions using the trained model
        predictions = self.model.predict(X_scaled)
        
        # Combine original data with predictions
        result_df = input_df.copy()
        for i, target in enumerate(self.target_columns):
            result_df[f'predicted_{target}'] = predictions[:, i]
            
        return result_df

    def compare_all_models(self, input_data):
        """
        Compare predictions from ALL available machine learning models.
        
        Args:
            input_data: DataFrame or dictionary with input features
            
        Returns:
            DataFrame with predictions from each model for easy comparison
        """
        # Convert single input to DataFrame if needed
        if isinstance(input_data, dict):
            input_df = pd.DataFrame([input_data])
        else:
            input_df = input_data.copy()
            
        # Preprocess input data once for all models
        X_scaled = self.preprocess_input(input_df)
        
        comparison_results = []
        
        for model_name, model_info in self.AVAILABLE_MODELS.items():
            try:
                # Download and load model
                model_path = hf_hub_download(
                    repo_id=self.repo_id,
                    filename=f"{model_info['subfolder']}/model.pkl",
                    repo_type="model"
                )
                
                # Load model with suppressed warnings
                with warnings.catch_warnings():
                    warnings.filterwarnings("ignore", category=UserWarning)
                    model = joblib.load(model_path)
                
                # Generate predictions
                predictions = model.predict(X_scaled)
                
                # Extract predictions for this sample
                result = {
                    'model': model_name,
                    'description': model_info['description']
                }
                
                for i, target in enumerate(self.target_columns):
                    if len(predictions.shape) > 1:
                        result[target] = predictions[0, i]
                    else:
                        result[target] = predictions[i]
                        
                comparison_results.append(result)
                
            except Exception as e:
                print(f"⚠️  Could not load model {model_name}: {e}")
                continue
                
        return pd.DataFrame(comparison_results)

    def create_comparison_tables(self, comparison_df):
        """
        Create formatted comparison tables for easy analysis.
        
        Args:
            comparison_df: DataFrame from compare_all_models()
            
        Returns:
            Dictionary with different formatted tables
        """
        tables = {}
        
        # Table 1: Detailed comparison with all metrics
        detailed_table = comparison_df.copy()
        detailed_table = detailed_table.round(4)
        tables['detailed'] = tabulate(
            detailed_table, 
            headers='keys', 
            tablefmt='grid',
            showindex=False
        )
        
        # Table 2: Summary statistics
        summary_data = []
        for target in self.target_columns:
            values = comparison_df[target]
            summary_data.append({
                'Target': target,
                'Mean': values.mean(),
                'Std': values.std(),
                'Min': values.min(),
                'Max': values.max(),
                'Range': values.max() - values.min()
            })
        
        summary_df = pd.DataFrame(summary_data).round(4)
        tables['summary'] = tabulate(
            summary_df, 
            headers='keys', 
            tablefmt='grid',
            showindex=False
        )
        
        # Table 3: Ranked by pectin yield (most important metric)
        ranked_df = comparison_df.sort_values('pectin_yield', ascending=False).round(4)
        tables['ranked'] = tabulate(
            ranked_df, 
            headers='keys', 
            tablefmt='grid',
            showindex=False
        )
        
        return tables

    def calculate_prediction_metrics(self, df_with_predictions):
        """
        Calculate basic metrics to evaluate prediction quality against actual values.
        """
        metrics = {}
        
        for target in self.target_columns:
            actual_col = None
            # Find the actual value column
            if target == 'pectin_yield':
                actual_col = 'ПВ, %'
            elif target == 'galacturonic_acid':
                actual_col = 'ГК, %'
            elif target == 'molecular_weight':
                actual_col = 'Mw, Д'
            elif target == 'esterification_degree':
                actual_col = 'СЭ, %'
            
            if actual_col and actual_col in df_with_predictions.columns:
                actual = df_with_predictions[actual_col]
                predicted = df_with_predictions[f'predicted_{target}']
                
                # Calculate metrics
                rmse = np.sqrt(np.mean((actual - predicted) ** 2))
                mae = np.mean(np.abs(actual - predicted))
                
                metrics[target] = {
                    'RMSE': rmse,
                    'MAE': mae,
                    'correlation': np.corrcoef(actual, predicted)[0, 1]
                }
        
        return metrics

# Example usage
if __name__ == "__main__":
    # Initialize predictor
    predictor = PectinPredictor()
    
    # Load experimental data
    df = pd.read_excel("/content/ShortExperiments_DataSet.xlsx")
    df_renamed = predictor.prepare_dataframe(df)
    
    print("🔬 PECTIN PRODUCTION MODEL COMPARISON SYSTEM")
    print("=" * 60)
    
    # 1. Batch prediction with best model
    print("\n1. BATCH PREDICTIONS WITH BEST MODEL:")
    print("-" * 40)
    
    results = predictor.predict_batch(df_renamed, model_type="best_model")
    print(f"✅ Processed {len(results)} experiments")
    
    # Calculate prediction quality metrics
    metrics = predictor.calculate_prediction_metrics(results)
    print("\n📊 PREDICTION QUALITY METRICS:")
    for target, metric in metrics.items():
        print(f"   {target}:")
        print(f"     RMSE: {metric['RMSE']:.4f}")
        print(f"     MAE: {metric['MAE']:.4f}")
        print(f"     Correlation: {metric['correlation']:.4f}")
    
    # 2. Compare all models for a single experiment
    print("\n2. COMPARING ALL MODELS FOR SINGLE EXPERIMENT:")
    print("-" * 50)
    
    single_experiment = {
        'sample': 'ЯП(М)', 
        'time_min': 7, 
        'temperature_c': 120, 
        'pressure_atm': 2.08, 
        'ph': 2.0
    }
    
    print(f"🔍 Input parameters: {single_experiment}")
    
    # Compare all models
    comparison_df = predictor.compare_all_models(single_experiment)
    
    # Create and display comparison tables
    tables = predictor.create_comparison_tables(comparison_df)
    
    print("\n📋 DETAILED MODEL COMPARISON:")
    print(tables['detailed'])
    
    print("\n📈 PREDICTION SUMMARY STATISTICS:")
    print(tables['summary'])
    
    print("\n🏆 MODELS RANKED BY PECTIN YIELD:")
    print(tables['ranked'])
    
    # 3. Show available models
    print("\n3. AVAILABLE MODELS:")
    print("-" * 20)
    for model_name, info in predictor.AVAILABLE_MODELS.items():
        print(f"   • {model_name}: {info['description']}")
    
    print(f"\n🎯 Total models available: {len(predictor.AVAILABLE_MODELS)}")
    print(f"✅ Successfully loaded: {len(comparison_df)}")
```

## 📁 Repository Structure

```
arabovs-ai-lab/PectinProductionModels/
├── best_model/                    # Best overall model (Gradient Boosting)
│   ├── model.pkl                 # Serialized model file
│   └── metadata.json            # Model metadata
├── random_forest/                # Random Forest model
├── gradient_boosting/           # Gradient Boosting model
├── xgboost/                     # XGBoost model
├── extra_trees/                 # Extra Trees model
├── linear_regression/           # Linear Regression model
├── ridge_regression/            # Ridge Regression model
├── lasso_regression/            # Lasso Regression model
├── support_vector_regression/   # SVR model
├── k_neighbors/                 # K-Neighbors model
├── multilayer_perceptron/       # MLP model
├── scaler.pkl                   # Feature scaler
├── label_encoder.pkl            # Label encoder for sample types
├── model_metadata.json          # Training metadata
├── models_metadata.json         # All models metadata
└── README.md                    # This file
```

## 🧪 Training Information

- **Dataset**: 1000 experimental records
- **Features**: 6 process parameters (excluding constant Т:Ж parameter)
- **Targets**: 4 quality parameters
- **Validation**: 80/20 train-test split
- **Cross-validation**: 5-fold
- **Best Algorithm**: Gradient Boosting

## 💡 Key Features

- **Multi-target regression**: Predicts 4 pectin quality parameters simultaneously
- **Process optimization**: Helps optimize pectin production conditions
- **Quality prediction**: Estimates pectin quality from process variables
- **Multiple algorithms**: 10 different ML algorithms for comparison
- **Industrial focus**: Specifically designed for pectin production technology

## ⚠️ Important Notes

### Data Requirements:
- **Supported samples**: 7 types as listed above
- **Parameter ranges**:
  - Time: 5-180 minutes
  - Temperature: 60-160°C
  - Pressure: 1.0-5.0 atm
  - pH: 1.5-4.0

### Limitations:
- Models trained on specific raw materials listed above
- Accuracy may decrease outside trained parameter ranges
- Retraining required for new types of raw materials

## 📜 Citation

If you use this model in your research, please cite it as:

```bibtex
@misc{PectinProductionModels2025,
  title = {Pectin Production Models: Machine Learning for Predicting Pectin Quality Parameters},
  author = {Arabovs AI Lab},
  year = {2025},
  publisher = {Hugging Face},
  url = {https://huggingface.co/arabovs-ai-lab/PectinProductionModels}
}
```

## 📄 License

MIT License

---

*Last updated: 2025-11-21*  
*Repository: https://huggingface.co/arabovs-ai-lab/PectinProductionModels*

## 🔗 References

- [Pectin Production Technology](https://en.wikipedia.org/wiki/Pectin)
- [Scikit-learn](https://scikit-learn.org/)
- [Hugging Face Hub](https://huggingface.co/docs/hub/)
```