23/08/2025, 17:53 Untitled2.
ipynb - Colab
Start coding or generate with AI.
Start coding or generate with AI.
# Import TPOTClassifier and roc_auc_score
from tpot import TPOTClassifier
from [Link] import roc_auc_score
# Instantiate TPOTClassifier
tpot = TPOTClassifier(
generations=5,
population_size=20,
verbosity=2,
scoring='roc_auc',
random_state=42,
disable_update_check=True,
config_dict='TPOT light'
)
[Link](X_train, y_train)
# AUC score for tpot model
tpot_auc_score = roc_auc_score(y_test, tpot.predict_proba(X_test)[:, 1])
print(f'\nAUC score: {tpot_auc_score:.4f}')
# Print best pipeline steps
print('\nBest pipeline steps:', end='\n')
for idx, (name, transform) in enumerate(tpot.fitted_pipeline_.steps, start=1):
# Print idx and transform
print(f'{idx}. {transform}')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/tmp/[Link] in <cell line: 0>()
13 config_dict='TPOT light'
14 )
---> 15 [Link](X_train, y_train)
16
17 # AUC score for tpot model
NameError: name 'X_train' is not defined
Next steps: Explain error
!pip install tpot
[Link] 1/7
23/08/2025, 17:53 [Link] - Colab
Collecting tpot
Downloading [Link] (1.9 kB)
Requirement already satisfied: numpy>=1.26.4 in /usr/local/lib/python3.12/dist-packages (from tpot) (
Requirement already satisfied: scipy>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from tpot) (1
Requirement already satisfied: scikit-learn>=1.6 in /usr/local/lib/python3.12/dist-packages (from tpo
Collecting update-checker>=0.16 (from tpot)
Downloading update_checker-[Link] (2.3 kB)
Requirement already satisfied: tqdm>=4.36.1 in /usr/local/lib/python3.12/dist-packages (from tpot) (4
Collecting stopit>=1.1.1 (from tpot)
Downloading [Link] (18 kB)
Preparing metadata ([Link]) ... done
Requirement already satisfied: pandas>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from tpot) (
Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.12/dist-packages (from tpot) (
Requirement already satisfied: xgboost>=3.0.0 in /usr/local/lib/python3.12/dist-packages (from tpot)
Requirement already satisfied: matplotlib>=3.6.2 in /usr/local/lib/python3.12/dist-packages (from tpo
Collecting traitlets>=5.8.0 (from tpot)
Downloading [Link] (10 kB)
Requirement already satisfied: lightgbm>=3.3.3 in /usr/local/lib/python3.12/dist-packages (from tpot)
Collecting optuna>=3.0.5 (from tpot)
Downloading [Link] (17 kB)
Requirement already satisfied: networkx>=3.0 in /usr/local/lib/python3.12/dist-packages (from tpot) (
Requirement already satisfied: dask>=2024.4.2 in /usr/local/lib/python3.12/dist-packages (from tpot)
Requirement already satisfied: distributed>=2024.4.2 in /usr/local/lib/python3.12/dist-packages (from
Collecting dask-expr>=1.0.12 (from tpot)
Downloading dask_expr-[Link] (2.3 kB)
Collecting dask-jobqueue>=0.8.5 (from tpot)
Downloading dask_jobqueue-[Link] (1.3 kB)
Collecting func-timeout>=4.3.5 (from tpot)
Downloading func_timeout-[Link] (44 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.3/44.3 kB 1.8 MB/s eta 0:00:00
Preparing metadata ([Link]) ... done
Collecting configspace>=1.1.1 (from tpot)
Downloading [Link] (130 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 131.0/131.0 kB 5.3 MB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata ([Link]) ... done
Collecting dill>=0.3.9 (from tpot)
Downloading [Link] (10 kB)
Requirement already satisfied: seaborn>=0.13.2 in /usr/local/lib/python3.12/dist-packages (from tpot)
Requirement already satisfied: pyparsing in /usr/local/lib/python3.12/dist-packages (from configspace
Requirement already satisfied: typing_extensions in /usr/local/lib/python3.12/dist-packages (from con
Requirement already satisfied: more_itertools in /usr/local/lib/python3.12/dist-packages (from config
Requirement already satisfied: click>=8.1 in /usr/local/lib/python3.12/dist-packages (from dask>=2024
Requirement already satisfied: cloudpickle>=3.0.0 in /usr/local/lib/python3.12/dist-packages (from da
Requirement already satisfied: fsspec>=2021.09.0 in /usr/local/lib/python3.12/dist-packages (from das
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from dask>
Requirement already satisfied: partd>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from dask>=20
Requirement already satisfied: pyyaml>=5.3.1 in /usr/local/lib/python3.12/dist-packages (from dask>=2
Requirement already satisfied: toolz>=0.10.0 in /usr/local/lib/python3.12/dist-packages (from dask>=2
Requirement already satisfied: jinja2>=2.10.3 in /usr/local/lib/python3.12/dist-packages (from distri
Requirement already satisfied: locket>=1.0.0 in /usr/local/lib/python3.12/dist-packages (from distrib
Requirement already satisfied: msgpack>=1.0.2 in /usr/local/lib/python3.12/dist-packages (from distri
Requirement already satisfied: psutil>=5.8.0 in /usr/local/lib/python3.12/dist-packages (from distrib
Requirement already satisfied: sortedcontainers>=2.0.5 in /usr/local/lib/python3.12/dist-packages (fr
Requirement already satisfied: tblib>=1.6.0 in /usr/local/lib/python3.12/dist-packages (from distribu
Requirement already satisfied: tornado>=6.2.0 in /usr/local/lib/python3.12/dist-packages (from distri
Requirement already satisfied: urllib3>=1.26.5 in /usr/local/lib/python3.12/dist-packages (from distr
Requirement already satisfied: zict>=3.0.0 in /usr/local/lib/python3.12/dist-packages (from distribut
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matp
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-packages (from matplotl
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from mat
Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from mat
Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-packages (from matplotlib>
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.12/dist-packages (from
Collecting alembic>=1.5.0 (from optuna>=3.0.5->tpot)
Downloading [Link] (7.3 kB)
Collecting colorlog (from optuna>=3.0.5->tpot)
Downloading [Link] (10 kB)
Requirement already satisfied: sqlalchemy>=1.4.2 in /usr/local/lib/python3.12/dist-packages (from opt
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas>=
Requirement already satisfied: tzdata> 2022 7 in /usr/local/lib/python3 12/dist packages (from pandas
[Link] 2/7
23/08/2025, 17:53 [Link] - Colab
Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas
Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from
Requirement already satisfied: requests>=2.3.0 in /usr/local/lib/python3.12/dist-packages (from updat
Requirement already satisfied: nvidia-nccl-cu12 in /usr/local/lib/python3.12/dist-packages (from xgbo
Requirement already satisfied: Mako in /usr/lib/python3/dist-packages (from alembic>=1.5.0->optuna>=3
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateu
Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (f
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from re
Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.12/dist-packages (from sqlalchem
Downloading [Link] (215 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 215.1/215.1 kB 16.2 MB/s eta 0:00:00
Downloading dask_expr-[Link] (3.2 kB)
Downloading dask_jobqueue-[Link] (52 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 52.0/52.0 kB 4.4 MB/s eta 0:00:00
Downloading [Link] (119 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 119.7/119.7 kB 9.4 MB/s eta 0:00:00
Downloading [Link] (400 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 400.9/400.9 kB 21.8 MB/s eta 0:00:00
Downloading [Link] (85 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 85.4/85.4 kB 6.2 MB/s eta 0:00:00
Downloading update_checker-[Link] (7.0 kB)
Downloading [Link] (247 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.0/247.0 kB 16.0 MB/s eta 0:00:00
Downloading [Link] (11 kB)
Building wheels for collected packages: configspace, func-timeout, stopit
Building wheel for configspace ([Link]) ... done
Created wheel for configspace: filename=[Link] size=115990 sha256=70bcf
Stored in directory: /root/.cache/pip/wheels/bb/a5/10/517a06ebdda14bf732250e82f8ba212a73bf88a8fc675
Building wheel for func-timeout ([Link]) ... done
Created wheel for func-timeout: filename=func_timeout-[Link] size=15077 sha256=e0cf
Stored in directory: /root/.cache/pip/wheels/ed/94/4a/9ccd71f7284a92e4137b7dd0ce8540c40ea7c26892515
Building wheel for stopit ([Link]) ... done
Created wheel for stopit: filename=[Link] size=11938 sha256=d4381fbb381f8277
Stored in directory: /root/.cache/pip/wheels/10/10/63/c3c98c9859d2aa59553536cc2ea005d3c9c39e214ab4f
Successfully built configspace func-timeout stopit
Installing collected packages: stopit, func-timeout, traitlets, dill, colorlog, update-checker, confi
Attempting uninstall: traitlets
Found existing installation: traitlets 5.7.1
Uninstalling traitlets-5.7.1:
Successfully uninstalled traitlets-5.7.1
Attempting uninstall: dill
Found existing installation: dill 0.3.8
Uninstalling dill-0.3.8:
Successfully uninstalled dill-0.3.8
ERROR: pip's dependency resolver does not currently take into account all the packages that are insta
ipython 7.34.0 requires jedi>=0.16, which is not installed.
datasets 4.0.0 requires dill<0.3.9,>=0.3.0, but you have dill 0.4.0 which is incompatible.
Successfully installed alembic-1.16.4 colorlog-6.9.0 configspace-1.2.1 dask-expr-2.0.0 dask-jobqueue-
WARNING: The following packages were previously imported in this runtime:
[traitlets]
You must restart the runtime in order to use newly installed versions.
RESTART SESSION
[Link] 3/7
23/08/2025, 17:53 [Link] - Colab
import pandas as pd
from sklearn.model_selection import train_test_split
# Load the dataset (replace with your data loading code)
# Assuming your data is in a pandas DataFrame named 'df' and the target column is 'target'
# For demonstration, I'll create a dummy dataset
from [Link] import make_classification
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
df = [Link](X, columns=[f'feature_{i}' for i in range([Link][1])])
df['target'] = y
# Define features (X) and target (y)
X = [Link]('target', axis=1)
y = df['target']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print("Data loaded and split successfully!")
print("X_train shape:", X_train.shape)
print("X_test shape:", X_test.shape)
print("y_train shape:", y_train.shape)
print("y_test shape:", y_test.shape)
Data loaded and split successfully!
X_train shape: (800, 20)
X_test shape: (200, 20)
y_train shape: (800,)
y_test shape: (200,)
!head -n 5 /content/[Link]
Recency (months),Frequency (times),Monetary (c.c. blood),Time (months),"whether he/she donated blood in
2 ,50,12500,98 ,1
0 ,13,3250,28 ,1
1 ,16,4000,35 ,1
2 ,20,5000,45 ,1
import pandas as pd
# Load the dataset
transfusion = pd.read_csv('/content/[Link]')
# Display the first rows
display([Link]())
Recency Frequency Monetary (c.c. Time whether he/she donated blood
(months) (times) blood) (months) in March 2007
0 2 50 12500 98 1
1 0 13 3250 28 1
2 1 16 4000 35 1
3 2 20 5000 45 1
4 1 24 6000 77 0
[Link]()
<class '[Link]'>
RangeIndex: 748 entries, 0 to 747
[Link] 4/7
23/08/2025, 17:53 [Link] - Colab
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Recency (months) 748 non-null int64
1 Frequency (times) 748 non-null int64
2 Monetary (c.c. blood) 748 non-null int64
3 Time (months) 748 non-null int64
4 whether he/she donated blood in March 2007 748 non-null int64
dtypes: int64(5)
memory usage: 29.3 KB
# Rename the target column
[Link](
columns={'whether he/she donated blood in March 2007': 'target'},
inplace=True
)
# Print the first 2 rows to verify the change
display([Link](2))
Recency (months) Frequency (times) Monetary (c.c. blood) Time (months) target
0 2 50 12500 98 1
1 0 13 3250 28 1
# Print target incidence proportions
target_incidence = transfusion['target'].value_counts(normalize=True).round(3)
print(target_incidence)
target
0 0.762
1 0.238
Name: proportion, dtype: float64
from sklearn.model_selection import train_test_split
# Split the data into training and testing sets, stratifying on the target column
X_train, X_test, y_train, y_test = train_test_split(
[Link]('target', axis=1),
transfusion['target'],
test_size=0.25, # A common split ratio
random_state=42,
stratify=transfusion['target']
)
# Print the first 2 rows of X_train
display(X_train.head(2))
Recency (months) Frequency (times) Monetary (c.c. blood) Time (months)
334 16 2 500 16
99 5 7 1750 26
# Import TPOTClassifier and roc_auc_score
from tpot import TPOTClassifier
from [Link] import roc_auc_score
# Instantiate TPOTClassifier
tpot = TPOTClassifier(
generations=5,
population_size=20,
random_state=42
)
[Link] 5/7
23/08/2025, 17:53 [Link] - Colab
)
# Fit TPOTClassifier
[Link](X_train, y_train)
# AUC score for tpot model
tpot_auc_score = roc_auc_score(y_test, tpot.predict_proba(X_test)[:, 1])
print(f'\nAUC score: {tpot_auc_score:.4f}')
# Print best pipeline steps
print('\nBest pipeline steps:', end='\n')
for idx, (name, transform) in enumerate(tpot.fitted_pipeline_.steps, start=1):
# Print idx and transform
print(f'{idx}. {transform}')
/usr/local/lib/python3.12/dist-packages/tpot/tpot_estimator/[Link]: UserWarning: Both generat
[Link]("Both generations and max_time_mins are set. TPOT will terminate when the first conditi
INFO:[Link]:To route to workers diagnostics web server please install jupyter-server-pr
INFO:[Link]:State start
INFO:[Link]: Scheduler at: tcp://[Link]:45923
INFO:[Link]: dashboard at: [Link]
INFO:[Link]:Registering Worker plugin shuffle
INFO:[Link]: Start Nanny at: 'tcp://[Link]:35419'
INFO:[Link]:Register worker addr: tcp://[Link]:36107 name: 0
INFO:[Link]:Starting worker compute stream, tcp://[Link]:36107
INFO:[Link]:Starting established connection to tcp://[Link]:41648
INFO:[Link]:Receive client connection: Client-e2946548-8019-11f0-8386-0242ac1c000c
INFO:[Link]:Starting established connection to tcp://[Link]:41650
Generation: 100%|██████████| 5/5 [07:01<00:00, 84.25s/it]
INFO:[Link]:Retire worker addresses (stimulus_id='retire-workers-1755951335.2196038') (0
INFO:[Link]:Closing Nanny at 'tcp://[Link]:35419'. Reason: nanny-close
INFO:[Link]:Nanny asking worker to close. Reason: nanny-close
INFO:[Link]:Received 'close-stream' from tcp://[Link]:41648; closing.
INFO:[Link]:Remove worker addr: tcp://[Link]:36107 name: 0 (stimulus_id='handle-worke
ERROR:[Link]:Removing worker 'tcp://[Link]:36107' caused the cluster to lose scattere
INFO:[Link]:Lost all workers
WARNING:[Link]:Worker process still alive after 4.0 seconds, killing
INFO:[Link]:Worker process 3551 was killed by signal 9
INFO:[Link]:Nanny at 'tcp://[Link]:35419' closed.
INFO:[Link]:Closing scheduler. Reason: unknown
INFO:[Link]:Scheduler closing all comms
AUC score: 0.7669
Best pipeline steps:
1. Normalizer(norm=np.str_('max'))
2. Passthrough()
3. FeatureUnion(transformer_list=[('featureunion',
FeatureUnion(transformer_list=[('columnonehotencoder',
ColumnOneHotEncoder())])),
('passthrough', Passthrough())])
4. FeatureUnion(transformer_list=[('skiptransformer', SkipTransformer()),
('passthrough', Passthrough())])
5. AdaBoostClassifier(learning_rate=0.27687721667, n_estimators=314,
random_state=42)
Double-click (or enter) to edit
Start coding or generate with AI.
# Print X_train's variance
print(X_train.var().round(3))
Recency (months) 66.929
Frequency (times) 33.830
Monetary (c.c. blood) 2114363.700
Time (months) 611.147
[Link] 6/7
23/08/2025, 17:53 [Link] - Colab
dtype: float64
# Identify the column with the highest variance
col_to_normalize = X_train.var().idxmax()
print(f"Column with highest variance: {col_to_normalize}")
Column with highest variance: Monetary (c.c. blood)
import numpy as np
# Copy X_train and X_test
X_train_normed, X_test_normed = X_train.copy(), X_test.copy()
# Log normalize the column with highest variance and drop the original
for df in [X_train_normed, X_test_normed]:
df['monetary_log'] = [Link](df[col_to_normalize])
[Link](col_to_normalize, axis=1, inplace=True)
# Print the first 2 rows of X_train_normed to verify the change
display(X_train_normed.head(2))
Recency (months) Frequency (times) Time (months) monetary_log
334 16 2 16 6.214608
99 5 7 26 7.467371
# Print X_train_normed variance
print(X_train_normed.var().round(3))
Recency (months) 66.929
Frequency (times) 33.830
Time (months) 611.147
monetary_log 0.837
dtype: float64
from sklearn import linear_model
from [Link] import roc_auc_score
# Create an instance of LogisticRegression
logreg = linear_model.LogisticRegression(random_state=42)
# Train the logistic regression model
[Link](X_train_normed, y_train)
# Predict probabilities on the test set
y_pred_prob = logreg.predict_proba(X_test_normed)[:, 1]
# Calculate and print the AUC score
logreg_auc_score = roc_auc_score(y_test, y_pred_prob)
print(f'\nAUC score for Logistic Regression: {logreg_auc_score:.4f}')
AUC score for Logistic Regression: 0.7814
spark Gemini
from operator import itemgetter
# Create a list of (model_name, model_score) pairs
model_scores = [
('TPOT', tpot_auc_score),
('Logistic Regression', logreg_auc_score)
]
[Link] 7/7