The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
iris data# ============================================================================
# WORKING EXAMPLES: predict_proba with unifiedml using IRIS dataset
# ============================================================================
# Load required packages
library(unifiedml)
library(randomForest)
library(nnet)
library(e1071)
# Load iris dataset
data(iris)
# Setup reproducible data
set.seed(42)
# Create feature matrix (all 4 numeric features)
X <- as.matrix(iris[, 1:4])
colnames(X) <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
# Target: Species (multi-class with 3 levels)
y_multiclass <- iris$Species
# Create binary classification target (Versicolor vs others)
y_binary <- factor(
ifelse(iris$Species == "versicolor", "versicolor", "other"),
levels = c("other", "versicolor")
)
# Split into train/test (75% train, 25% test)
set.seed(42)
train_idx <- sample(1:nrow(X), size = floor(0.75 * nrow(X)), replace = FALSE)
test_idx <- setdiff(1:nrow(X), train_idx)
X_train <- X[train_idx, ]
X_test <- X[test_idx, ]
y_train_multiclass <- y_multiclass[train_idx]
y_test_multiclass <- y_multiclass[test_idx]
y_train_binary <- y_binary[train_idx]
y_test_binary <- y_binary[test_idx]
cat("\n")## ============================================================================
## IRIS DATASET - Summary
## ============================================================================
## Training samples: 112
## Test samples: 38
## Features: 4
## Classes: setosa, versicolor, virginica
# ============================================================================
# EXAMPLE 1: randomForest - Multi-class Classification on IRIS
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 1: randomForest - Multi-class Classification
## ============================================================================
mod_rf <- Model$new(randomForest::randomForest)
mod_rf$fit(X_train, y_train_multiclass, ntree = 100)
cat("\nPredicting probabilities for first 5 test samples:\n")##
## Predicting probabilities for first 5 test samples:
##
## Probability matrix:
## setosa versicolor virginica
## 1 1 0 0
## 2 1 0 0
## 3 1 0 0
## 4 1 0 0
## 5 1 0 0
## attr(,"assign")
## [1] 1 1 1
## attr(,"contrasts")
## attr(,"contrasts")$pred
## [1] "contr.treatment"
##
## attr(,"extraction_method")
## [1] "fallback::1"
## attr(,"model_class")
## [1] "randomForest.formula"
##
## Interpretation:
for(i in 1:5) {
cat(sprintf("\nSample %d (Actual: %s):\n", i, as.character(y_test_multiclass[i])))
cat(sprintf(" setosa: %.1f%%\n", probs_rf[i, "setosa"] * 100))
cat(sprintf(" versicolor: %.1f%%\n", probs_rf[i, "versicolor"] * 100))
cat(sprintf(" virginica: %.1f%%\n", probs_rf[i, "virginica"] * 100))
cat(sprintf(" Predicted: %s\n", colnames(probs_rf)[which.max(probs_rf[i, ])]))
}##
## Sample 1 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 2 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 3 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 4 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 5 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
# Get class predictions
pred_classes_rf <- mod_rf$predict(X_test[1:5, ], type = "class")
cat("\nPredicted classes (first 5):", as.character(pred_classes_rf), "\n")##
## Predicted classes (first 5): setosa setosa setosa setosa setosa
## Actual classes (first 5): setosa setosa setosa setosa setosa
# Calculate accuracy on full test set
probs_all_rf <- mod_rf$predict_proba(X_test)
pred_all_rf <- colnames(probs_all_rf)[apply(probs_all_rf, 1, which.max)]
accuracy_rf <- mean(pred_all_rf == as.character(y_test_multiclass))
cat(sprintf("\nTest set accuracy: %.1f%%\n", accuracy_rf * 100))##
## Test set accuracy: 94.7%
# ============================================================================
# EXAMPLE 2: nnet - Multi-class Classification on IRIS
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 2: nnet - Multi-class Classification
## ============================================================================
mod_nnet <- Model$new(nnet::nnet)
mod_nnet$fit(X_train, y_train_multiclass, size = 10, maxit = 200, trace = FALSE)
cat("\nPredicting probabilities for first 5 test samples:\n")##
## Predicting probabilities for first 5 test samples:
##
## Probability matrix (all 3 classes):
## setosa versicolor virginica
## 1 1 0 0
## 2 1 0 0
## 3 1 0 0
## 4 1 0 0
## 5 1 0 0
## attr(,"extraction_method")
## [1] "fallback::5"
## attr(,"model_class")
## [1] "nnet.formula"
##
## Detailed predictions:
for(i in 1:5) {
cat(sprintf("\nSample %d (Actual: %s):\n", i, as.character(y_test_multiclass[i])))
cat(sprintf(" setosa: %.1f%%\n", probs_nnet[i, "setosa"] * 100))
cat(sprintf(" versicolor: %.1f%%\n", probs_nnet[i, "versicolor"] * 100))
cat(sprintf(" virginica: %.1f%%\n", probs_nnet[i, "virginica"] * 100))
cat(sprintf(" Predicted: %s\n", colnames(probs_nnet)[which.max(probs_nnet[i, ])]))
}##
## Sample 1 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 2 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 3 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 4 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 5 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
# Get class predictions
pred_classes_nnet <- mod_nnet$predict(X_test[1:5, ], type = "class")
cat("\nPredicted classes (first 5):", as.character(pred_classes_nnet), "\n")##
## Predicted classes (first 5): setosa setosa setosa setosa setosa
## Actual classes (first 5): setosa setosa setosa setosa setosa
# Calculate accuracy
probs_all_nnet <- mod_nnet$predict_proba(X_test)
pred_all_nnet <- colnames(probs_all_nnet)[apply(probs_all_nnet, 1, which.max)]
accuracy_nnet <- mean(pred_all_nnet == as.character(y_test_multiclass))
cat(sprintf("\nTest set accuracy: %.1f%%\n", accuracy_nnet * 100))##
## Test set accuracy: 97.4%
# ============================================================================
# EXAMPLE 3: SVM - Multi-class Classification on IRIS
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 3: SVM - Multi-class Classification
## ============================================================================
mod_svm <- Model$new(e1071::svm)
mod_svm$fit(X_train, y_train_multiclass, probability = TRUE, kernel = "radial")
cat("\nPredicting probabilities for first 5 test samples:\n")##
## Predicting probabilities for first 5 test samples:
##
## Probability matrix:
## setosa versicolor virginica
## 1 1 0 0
## 2 1 0 0
## 3 1 0 0
## 4 1 0 0
## 5 1 0 0
## attr(,"assign")
## [1] 1 1 1
## attr(,"contrasts")
## attr(,"contrasts")$pred
## [1] "contr.treatment"
##
## attr(,"extraction_method")
## [1] "fallback::1"
## attr(,"model_class")
## [1] "svm.formula"
##
## Detailed predictions:
for(i in 1:5) {
cat(sprintf("\nSample %d (Actual: %s):\n", i, as.character(y_test_multiclass[i])))
cat(sprintf(" setosa: %.1f%%\n", probs_svm[i, "setosa"] * 100))
cat(sprintf(" versicolor: %.1f%%\n", probs_svm[i, "versicolor"] * 100))
cat(sprintf(" virginica: %.1f%%\n", probs_svm[i, "virginica"] * 100))
cat(sprintf(" Predicted: %s\n", colnames(probs_svm)[which.max(probs_svm[i, ])]))
}##
## Sample 1 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 2 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 3 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 4 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
##
## Sample 5 (Actual: setosa):
## setosa: 100.0%
## versicolor: 0.0%
## virginica: 0.0%
## Predicted: setosa
# Calculate accuracy
probs_all_svm <- mod_svm$predict_proba(X_test)
pred_all_svm <- colnames(probs_all_svm)[apply(probs_all_svm, 1, which.max)]
accuracy_svm <- mean(pred_all_svm == as.character(y_test_multiclass))
cat(sprintf("\nTest set accuracy: %.1f%%\n", accuracy_svm * 100))##
## Test set accuracy: 94.7%
# ============================================================================
# EXAMPLE 4: Binary Classification on IRIS (Versicolor vs others)
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 4: Binary Classification - Versicolor vs Others
## ============================================================================
# randomForest binary
mod_rf_binary <- Model$new(randomForest::randomForest)
mod_rf_binary$fit(X_train, y_train_binary, ntree = 100)
cat("\nrandomForest - Binary probabilities (first 5 test samples):\n")##
## randomForest - Binary probabilities (first 5 test samples):
## other versicolor
## 1 1 0
## 2 1 0
## 3 1 0
## 4 1 0
## 5 1 0
## attr(,"assign")
## [1] 1 1
## attr(,"contrasts")
## attr(,"contrasts")$pred
## [1] "contr.treatment"
##
## attr(,"extraction_method")
## [1] "fallback::1"
## attr(,"model_class")
## [1] "randomForest.formula"
# SVM binary
mod_svm_binary <- Model$new(e1071::svm)
mod_svm_binary$fit(X_train, y_train_binary, probability = TRUE, kernel = "radial")
cat("\nSVM - Binary probabilities (first 5 test samples):\n")##
## SVM - Binary probabilities (first 5 test samples):
## other versicolor
## 1 1 0
## 2 1 0
## 3 1 0
## 4 1 0
## 5 1 0
## attr(,"assign")
## [1] 1 1
## attr(,"contrasts")
## attr(,"contrasts")$pred
## [1] "contr.treatment"
##
## attr(,"extraction_method")
## [1] "fallback::1"
## attr(,"model_class")
## [1] "svm.formula"
##
## Comparison of Versicolor probabilities:
comparison_binary <- data.frame(
Sample = 1:5,
Actual = as.character(y_test_binary[1:5]),
RandomForest = round(probs_rf_binary[, "versicolor"], 3),
SVM = round(probs_svm_binary[, "versicolor"], 4)
)
print(comparison_binary)## Sample Actual RandomForest SVM
## 1 1 other 0 0
## 2 2 other 0 0
## 3 3 other 0 0
## 4 4 other 0 0
## 5 5 other 0 0
# ============================================================================
# EXAMPLE 5: Using unified predict() method on IRIS
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 5: Using unified predict() method
## ============================================================================
##
## randomForest - predict(type='prob') on first 3 samples:
## setosa versicolor virginica
## 1 1 0 0
## 2 1 0 0
## 3 1 0 0
## attr(,"class")
## [1] "matrix" "array" "votes"
##
## randomForest - predict(type='class') on first 3 samples:
## 1 2 3
## setosa setosa setosa
## Levels: setosa
##
## nnet - predict(type='class') on first 3 samples:
## [1] setosa setosa setosa
## Levels: setosa
##
## SVM - predict(type='class') on first 3 samples:
## 1 2 3
## setosa setosa setosa
## Levels: setosa
# ============================================================================
# EXAMPLE 6: Model Comparison on IRIS
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 6: Model Performance Comparison
## ============================================================================
##
## Model Accuracies on IRIS test set:
## randomForest: 94.7%
## nnet: 97.4%
## SVM: 94.7%
##
## Detailed comparison for first 5 test samples:
comparison_multi <- data.frame(
Sample = 1:5,
Actual = as.character(y_test_multiclass[1:5]),
RF_Pred = as.character(mod_rf$predict(X_test[1:5, ], type = "class")),
nnet_Pred = as.character(mod_nnet$predict(X_test[1:5, ], type = "class")),
SVM_Pred = as.character(mod_svm$predict(X_test[1:5, ], type = "class"))
)
print(comparison_multi)## Sample Actual RF_Pred nnet_Pred SVM_Pred
## 1 1 setosa setosa setosa setosa
## 2 2 setosa setosa setosa setosa
## 3 3 setosa setosa setosa setosa
## 4 4 setosa setosa setosa setosa
## 5 5 setosa setosa setosa setosa
# ============================================================================
# EXAMPLE 7: Confidence Analysis on IRIS
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 7: Prediction Confidence Analysis
## ============================================================================
# randomForest confidence
rf_confidences <- apply(probs_all_rf, 1, max)
cat("\nrandomForest - Prediction confidence:\n")##
## randomForest - Prediction confidence:
## Mean confidence: 100.0%
## Median confidence: 100.0%
cat(sprintf(" Low confidence (<70%%): %d samples (%.1f%%)\n",
sum(rf_confidences < 0.7), mean(rf_confidences < 0.7) * 100))## Low confidence (<70%): 0 samples (0.0%)
cat(sprintf(" High confidence (>90%%): %d samples (%.1f%%)\n",
sum(rf_confidences > 0.9), mean(rf_confidences > 0.9) * 100))## High confidence (>90%): 38 samples (100.0%)
# nnet confidence
nnet_confidences <- apply(probs_all_nnet, 1, max)
cat("\nnnet - Prediction confidence:\n")##
## nnet - Prediction confidence:
## Mean confidence: 100.0%
## Median confidence: 100.0%
cat(sprintf(" Low confidence (<70%%): %d samples (%.1f%%)\n",
sum(nnet_confidences < 0.7), mean(nnet_confidences < 0.7) * 100))## Low confidence (<70%): 0 samples (0.0%)
cat(sprintf(" High confidence (>90%%): %d samples (%.1f%%)\n",
sum(nnet_confidences > 0.9), mean(nnet_confidences > 0.9) * 100))## High confidence (>90%): 38 samples (100.0%)
# SVM confidence
svm_confidences <- apply(probs_all_svm, 1, max)
cat("\nSVM - Prediction confidence:\n")##
## SVM - Prediction confidence:
## Mean confidence: 100.0%
## Median confidence: 100.0%
cat(sprintf(" Low confidence (<70%%): %d samples (%.1f%%)\n",
sum(svm_confidences < 0.7), mean(svm_confidences < 0.7) * 100))## Low confidence (<70%): 0 samples (0.0%)
cat(sprintf(" High confidence (>90%%): %d samples (%.1f%%)\n",
sum(svm_confidences > 0.9), mean(svm_confidences > 0.9) * 100))## High confidence (>90%): 38 samples (100.0%)
# ============================================================================
# EXAMPLE 8: Misclassification Analysis
# ============================================================================
cat("\n")## ============================================================================
## EXAMPLE 8: Misclassification Analysis (randomForest)
## ============================================================================
# Find misclassified samples
rf_misclassified <- which(pred_all_rf != as.character(y_test_multiclass))
if(length(rf_misclassified) > 0) {
cat(sprintf("\nFound %d misclassified samples:\n", length(rf_misclassified)))
for(idx in rf_misclassified[1:min(3, length(rf_misclassified))]) {
cat(sprintf("\nSample %d:\n", idx))
cat(sprintf(" True class: %s\n", as.character(y_test_multiclass[idx])))
cat(sprintf(" Predicted: %s\n", pred_all_rf[idx]))
cat(" Probabilities:\n")
cat(sprintf(" setosa: %.1f%%\n", probs_all_rf[idx, "setosa"] * 100))
cat(sprintf(" versicolor: %.1f%%\n", probs_all_rf[idx, "versicolor"] * 100))
cat(sprintf(" virginica: %.1f%%\n", probs_all_rf[idx, "virginica"] * 100))
}
} else {
cat("\nPerfect classification! No misclassified samples.\n")
}##
## Found 2 misclassified samples:
##
## Sample 19:
## True class: versicolor
## Predicted: virginica
## Probabilities:
## setosa: 0.0%
## versicolor: 0.0%
## virginica: 100.0%
##
## Sample 34:
## True class: virginica
## Predicted: versicolor
## Probabilities:
## setosa: 0.0%
## versicolor: 100.0%
## virginica: 0.0%
# ============================================================================
# SUMMARY
# ============================================================================
cat("\n")## ============================================================================
## SUMMARY - IRIS Dataset
## ============================================================================
cat("
✓ SUCCESSFUL EXAMPLES WITH IRIS DATASET:
1. randomForest - Multi-class classification (3 species)
2. nnet - Multi-class classification
3. SVM - Multi-class classification with probabilities
4. Binary classification (Versicolor vs others)
5. Unified predict() interface
6. Model comparison and accuracy analysis
7. Confidence analysis
8. Misclassification analysis
✓ KEY FINDINGS ON IRIS:
• All models achieve high accuracy (>90%) on iris dataset
• SVM tends to produce extreme probabilities (near 0 or 1)
• randomForest and nnet show more calibrated probabilities
• Setosa is perfectly separable from other species
• Confusion typically occurs between versicolor and virginica
✓ predict_proba() FEATURES DEMONSTRATED:
• Returns matrix [n_samples × 3] for multi-class
• Column names: setosa, versicolor, virginica
• All rows sum to 1
• Works seamlessly across all model types
All working examples on IRIS dataset completed successfully!\n")##
## ✓ SUCCESSFUL EXAMPLES WITH IRIS DATASET:
## 1. randomForest - Multi-class classification (3 species)
## 2. nnet - Multi-class classification
## 3. SVM - Multi-class classification with probabilities
## 4. Binary classification (Versicolor vs others)
## 5. Unified predict() interface
## 6. Model comparison and accuracy analysis
## 7. Confidence analysis
## 8. Misclassification analysis
##
## ✓ KEY FINDINGS ON IRIS:
## • All models achieve high accuracy (>90%) on iris dataset
## • SVM tends to produce extreme probabilities (near 0 or 1)
## • randomForest and nnet show more calibrated probabilities
## • Setosa is perfectly separable from other species
## • Confusion typically occurs between versicolor and virginica
##
## ✓ predict_proba() FEATURES DEMONSTRATED:
## • Returns matrix [n_samples × 3] for multi-class
## • Column names: setosa, versicolor, virginica
## • All rows sum to 1
## • Works seamlessly across all model types
##
## All working examples on IRIS dataset completed successfully!
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.