Will every generated project train a model?
No. Some missions use pretrained models for inference without a training step. Trainable workflows still depend on valid files, labels, splits, package versions, available memory, and task assumptions.
Answer first
Choose the task, data source, model direction, execution settings, and deployment target to generate a structured Python project instead of an isolated snippet. Trainable tasks include the relevant data and training workflow; inference-only missions generate prediction code without pretending retraining is required. Every path still needs validation, hardware checks, package review, and measured deployment performance.
Start with the prediction you need. The technical name stays visible so you learn the vocabulary while building.
pip install scikit-learn>=1.5,<2 pandas>=2.2,<3 numpy>=1.26,<3 joblib>=1.4,<2Logistic Regression for Breast Cancer Wisconsin, using train validation test and standard scaling.
"""Configurable classification workflow generated by the AI Script Generator."""
import joblib
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer, make_column_selector
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline as SklearnPipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score, balanced_accuracy_score, classification_report,
confusion_matrix, f1_score,
)
from sklearn.datasets import load_breast_cancer
dataset = load_breast_cancer(as_frame=True)
X = dataset.data.copy()
y = dataset.target.copy()
y.name = getattr(y, "name", None) or "target"
# Inspect the data before transforming it
print("\nFeature preview:")
print(X.head())
print("\nShapes:", X.shape, y.shape)
print("\nColumn types:")
print(X.dtypes)
print("\nFeature statistics:")
print(X.describe(include="all").T)
print("\nMissing values:")
print(pd.concat([X, y.rename("__target__")], axis=1).isna().sum())
print("\nTarget distribution:")
print(y.value_counts(dropna=False, normalize=True).sort_index())
# Split before fitting imputers, encoders, scalers, or samplers
RANDOM_SEED = 42
TEST_RATIO = 0.15
STRATIFY = True
VALIDATION_RATIO = 0.15
X_development, X_test, y_development, y_test = train_test_split(
X, y, test_size=TEST_RATIO, random_state=RANDOM_SEED, stratify=y if STRATIFY else None,
)
validation_share = VALIDATION_RATIO / (1.0 - TEST_RATIO)
X_train, X_validation, y_train, y_validation = train_test_split(
X_development, y_development, test_size=validation_share, random_state=RANDOM_SEED, stratify=y_development if STRATIFY else None,
)
# Learn preprocessing from training data only
numeric_pipeline = SklearnPipeline(steps=[
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler()),
])
categorical_pipeline = SklearnPipeline(steps=[
("imputer", SimpleImputer(strategy="most_frequent")),
("encoder", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),
])
preprocessor = ColumnTransformer(
transformers=[
("numeric", numeric_pipeline, make_column_selector(dtype_include=np.number)),
("categorical", categorical_pipeline, make_column_selector(dtype_exclude=np.number)),
],
remainder="drop",
)
DECISION_THRESHOLD = None
def predict_labels(model, features):
if DECISION_THRESHOLD is None:
return model.predict(features)
if len(model.classes_) != 2:
raise ValueError("Decision thresholds require a binary classifier.")
probabilities = model.predict_proba(features)[:, 1]
return np.where(
probabilities >= DECISION_THRESHOLD,
model.classes_[1],
model.classes_[0],
)
def evaluate(model, features, target, split_name):
predictions = predict_labels(model, features)
print(f"\n{split_name} accuracy: {accuracy_score(target, predictions):.4f}")
print(f"{split_name} balanced accuracy: {balanced_accuracy_score(target, predictions):.4f}")
print(f"{split_name} weighted F1: {f1_score(target, predictions, average='weighted'):.4f}")
print("\nClassification report:")
print(classification_report(target, predictions, zero_division=0))
print("Confusion matrix:")
print(confusion_matrix(target, predictions))
return predictions
# Keep preprocessing, optional balancing, and the model in one pipeline
pipeline_steps = [
("preprocess", preprocessor),
("model", LogisticRegression(C=1, max_iter=1000, class_weight=None)),
]
pipeline = SklearnPipeline(steps=pipeline_steps)
pipeline.fit(X_train, y_train)
evaluate(pipeline, X_validation, y_validation, "Validation")
# After configuration decisions, refit on train + validation
X_development = pd.concat([X_train, X_validation], axis=0)
y_development = pd.concat([y_train, y_validation], axis=0)
pipeline.fit(X_development, y_development)
test_predictions = evaluate(pipeline, X_test, y_test, "Final test")
MODEL_PATH = "classification_pipeline.joblib"
joblib.dump(pipeline, MODEL_PATH)
print(f"\nSaved fitted pipeline to {MODEL_PATH}")
# Run inference with the same fitted preprocessing
sample_prediction = predict_labels(pipeline, X_test.iloc[[0]])
print("Sample prediction:", sample_prediction[0])
print("Sample target:", y_test.iloc[0])
Design guide
Technical content reviewed
Worked approach
Select the task and create the project. For a trainable mission, inspect labels and dataset splits before tuning. For an inference-only mission, verify inputs and outputs on representative samples. In both cases, establish a simple baseline and measure target-device constraints before adding complexity.
Common decisions
No. Some missions use pretrained models for inference without a training step. Trainable workflows still depend on valid files, labels, splits, package versions, available memory, and task assumptions.
Begin with a small baseline that trains and runs on the target. Increase capacity only after error analysis shows that model size, rather than data or labeling, limits the result.
Measure accuracy on unseen data, latency, memory, power, startup time, thermal behavior, and failures on the actual device. Desktop inference speed is not enough.
Related build evidence
Plant Care AI combines sensor inputs, leaf-image analysis, and a Flutter application. It demonstrates the system work required around an AI model.
See the projectApply it to real hardware
Share the task, available data, target hardware, latency limit, expected outputs, and how users will act on the result.
Discuss the system