Skip to main content

Nous: A Neuro-Symbolic Library for Interpretable and Causal Reasoning AI

Project description

Nous: A Neuro-Symbolic Library for Interpretable AI

PyPI version License: MIT

Nous is a PyTorch-based library for building "white-box" machine learning models for both regression and classification. It enables models that don't just predict, but also reason and explain their decisions in human-understandable terms.

Key Features

  • Deeply Interpretable: Generate a complete, step-by-step logical trace (fact -> rule -> concept -> prediction) for any decision.
  • Supports Regression & Classification: A unified API for predicting both continuous values and class probabilities.
  • Causal by Design: Natively supports counterfactual analysis ("What if?") to provide actionable recommendations for both regression and classification tasks.
  • High Performance: Achieves accuracy competitive with traditional black-box models.
  • Scalable & Flexible: Choose between a high-performance beta activation, a robust sigmoid, or a maximally transparent exhaustive fact layer.

Installation

pip install nous

Quickstart: A 5-Minute Example (Regression)

Let's predict a house price and understand the model's reasoning.

import torch
import pandas as pd
from sklearn.datasets import make_regression
from nous.models import NousNet
from nous.interpret import trace_decision_graph, explain_fact
from nous.causal import find_counterfactual

# 1. Prepare Data
X_raw, y = make_regression(n_samples=1000, n_features=5, n_informative=3, noise=20, random_state=42)
feature_names = ['area_sqft', 'num_bedrooms', 'dist_to_center', 'age_years', 'renovation_quality']
X = torch.tensor(X_raw, dtype=torch.float32)
y = torch.tensor(y, dtype=torch.float32).unsqueeze(1)

# 2. Define and Train a NousNet for Regression
model = NousNet(
    input_dim=5,
    output_dim=1, # Single output for regression
    feature_names=feature_names,
    fact_layer_type='beta'
)
# Training: Use a regression loss like nn.MSELoss
# loss_fn = torch.nn.MSELoss()
# ... (standard training loop omitted)
model.eval()

# 3. Analyze a specific house
x_sample = X[50]
predicted_price = model(x_sample).item()
print(f"Model's predicted price for house #50: ${predicted_price:,.2f}")

# 4. Get the Step-by-Step Reasoning
graph = trace_decision_graph(model, x_sample)
top_facts = sorted(graph['trace']['Atomic Facts'].items(), key=lambda i: i[1]['value'], reverse=True)
fact_to_analyze = top_facts[0][0]
print(f"\nTop activated fact influencing the price: '{fact_to_analyze}'")

# 5. Decode the Learned Fact
details_df = explain_fact(model, fact_name=fact_to_analyze)
print(f"\nDecoding '{fact_to_analyze}':")
display(details_df.head())

# 6. Get an Actionable Recommendation
# What's the smallest change to increase the predicted price to $150,000?
recommendation = find_counterfactual(
    model,
    x_sample,
    target_output=150.0, # Target value for regression
    task='regression'
)
print("\nRecommendation to increase value to $150k:")
for feature, old_val, new_val in recommendation['changes']:
    print(f"- Change '{feature}' from {old_val:.2f} to {new_val:.2f}")

Choosing a fact_layer_type

  • 'beta' (Default, Recommended): Best performance and flexibility.
  • 'sigmoid': A robust and reliable alternative.
  • 'exhaustive': Maximum transparency. Best for low-dimensional problems (<15 features).

License

This project is licensed under the MIT License.

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

nous-0.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

nous-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file nous-0.1.0.tar.gz.

File metadata

  • Download URL: nous-0.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for nous-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5bcfddfd12e2e00b140ea1d7d0aff6089d47653d68f1c1480551b6daa5c8923b
MD5 fe87e767c5437c1b8d4c9476cc4815b8
BLAKE2b-256 0cb8d60ba725ba8f858da3f282e999b840c1fdc99b267ace5c5151841d3e12be

See more details on using hashes here.

File details

Details for the file nous-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nous-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for nous-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2aadf0dbe5236cc24ee1dbd000d61a1d613eb4c19db6c7d4d357223ba478d161
MD5 8fd15e976d536b89eaa97ede386c8360
BLAKE2b-256 45e8bbb87c26f5c7c1dd306ef651de51b27030d48e53e7587f5815eaac79fb08

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