Skip to main content

Machine learning tools for environmental analysis

Project description

Decision Tree Visualization and Classification Analysis Toolkit

Python License

A comprehensive Python package for creating interactive decision tree visualizations and performing advanced classification model analysis.

Features

  • Interactive HTML Decision Trees: Visualize decision paths with D3.js
  • Automated Model Comparison: Evaluate multiple classifiers with one function
  • Feature Importance Analysis: Multiple methods including SHAP, permutation, and native importance
  • Probability Analysis: Visualize prediction distributions
  • Data Preprocessing: Automatic handling of categorical and numerical variables

Milieudefensie Decision Tree Tools

Python package for creating interactive decision tree visualizations for environmental analysis.

Quick Start Example

Beslisboom

from milieudefensie.beslisboom import CreateHTML
from milieudefensie.model_non_linear_y import generate_example_dataset
import time
import numpy as np

# Generate example data
df = generate_example_dataset(n_samples=10000)
df['Interesse'] = np.where(df['y_binary'] == "yes", 1, 0)

# Configure tree parameters
variables_ = ['major_donor_2024', 'age', 'member']  # Variables to include
y = 'Interesse'  # Target variable for splits
split_method = 'gini'  # 'gini' or 'mean' for splitting criteria
min_records = 500  # Minimum records required for a split
max_integer = 5  # Max splits for integer variables
max_nr_splits = 2  # Max splits except for categoricals
min_split_values = 1000  # Minimum records per split
nr_splits = {'age': 4}  # Custom number of splits per variable
splits = {'age': [20,25,40,60]}  # Custom split points
color_reverse = True  # Color scheme (True: red=low, blue=high)
name_all = 'Selectie interesse nalaten'  # Root node name

# Create and visualize the tree
create_html = CreateHTML(
    df=df,
    vars=variables_,
    y=y,
    split_method=split_method,
    min_records=min_records,
    max_integer=max_integer,
    max_nr_splits=max_nr_splits,
    min_split_values=min_split_values,
    nr_splits=nr_splits,
    splits=splits,
    color_reverse=color_reverse,
    name_all=name_all,
    reorder=False  # Keep original variable order
)

# Generate HTML output
create_html.build_HTML(
    output_file='beslisboom_voorbeeld.html',
    title='Interesse Analysis',
    explanation='This tree shows how different donor characteristics affect legacy interest',
    made_by='Milieudefensie Data Team'
)


# Milieudefensie Machine Learning Toolkit

## Comprehensive Usage Example

```python
from milieudefensie import generate_example_dataset, ClassificationAnalyzer

### 1. Data Preparation
# Generate example dataset (1000 samples)
df = generate_example_dataset(n_samples=1000)

# Select target variable (binary or multiclass)
y = df['y_binary']  # Binary target
# y = df['y_multiclass']  # Multiclass target

# Prepare features
X = df.drop(columns=['y_binary', 'y_multiclass'])

# Generate larger dataset for predictions (10000 samples)
df_all = generate_example_dataset(n_samples=10000)
df_all['relation_id'] = range(1, len(df_all) + 1)
X_all = df_all.drop(columns=['y_binary', 'y_multiclass', 'relation_id'])

### 2. Model Analysis
# Initialize analyzer
analyzer = ClassificationAnalyzer(y=y, X=X)

# Compare multiple models
results = analyzer.compare_models(test_size=0.2)
print("Model Comparison Results:")
print(results['results'])

# Analyze feature importance
feature_importance = analyzer.analyze_feature_importance(method='auto')
print("\nTop Features:")
print(feature_importance.head(10))

### 3. Prediction Generation
# Generate predictions on training data
train_predictions = analyzer.generate_predictions(
    relation_ids=df['age'],  # Using age as ID
    save_as_csv=False
)

# Generate predictions on new data
new_predictions = analyzer.generate_predictions(
    relation_ids=df_all['age'],
    X_new=X_all,
    save_as_csv=True,
    csv_name='new_predictions.csv'
)

### 4. Probability Analysis
# Basic probability analysis
prob_analysis = analyzer.analyze_probabilities()

# Advanced analysis with new data
prob_analysis_new = analyzer.analyze_probabilities(
    X_data=X_all,
    n_cases=1000,
    analysis_type='highest',  # 'highest', 'lowest', or 'both'
    hover_columns=['age', 'gender_male', 'major_donor_2024'],
    plot_filename='interest_probabilities.html'
)

# Multiclass-specific analysis
if len(analyzer.class_names) > 2:
    prob_analysis_custom = analyzer.analyze_probabilities(
        target_class='Interested',  # Specify class to analyze
        n_cases=100,
        analysis_type='highest',
        hover_columns=['age', 'gender_male', 'major_donor_2024'],
        plot_filename='class_probabilities.html'
    )

## Installation

```bash
pip install milieudefensie .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

milieudefensie-0.0.2.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

milieudefensie-0.0.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file milieudefensie-0.0.2.tar.gz.

File metadata

  • Download URL: milieudefensie-0.0.2.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for milieudefensie-0.0.2.tar.gz
Algorithm Hash digest
SHA256 0e309714c3a2092433dd5e20bee3b83e05432adfd68624e821b08c253a1e7616
MD5 8fb53d85290232077349150da71f8e91
BLAKE2b-256 1f933b906e1004d81f337d3f878266f53cb925d8538d4cce042496e8b1754172

See more details on using hashes here.

File details

Details for the file milieudefensie-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: milieudefensie-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for milieudefensie-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6d24b08c9895bb6dec96fb069fa8a2e0bb615c51e0327096551fe3c6e4411d71
MD5 a7519f2e7f7268695b6270d27c00e10f
BLAKE2b-256 dc015bae83d7a4c581b7ab02885f34de275045b303b3b7b1b6ca1b0e9e87bea3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page