An AI-enhanced fuzzy logic-based item recommendation system for SQLite databases.
Project description
AIFuzzyRec
A Python library for generating AI-powered item recommendations using fuzzy logic and SQLite databases. AIFuzzyRec leverages fuzzy set theory and artificial intelligence to compute item similarities based on user-item interactions, providing flexible and AI-enhanced recommendations for applications like e-commerce, content platforms, or personalized systems.
Features
-
Fuzzy Logic-Based Recommendations: Uses fuzzy membership functions to compute item similarities, allowing nuanced recommendation scores.
-
Flexible Database Schema: Supports any SQLite database with user-item interaction data, with customizable table and column names.
-
Context Manager Support: Ensures automatic cleanup of database connections using Python's with statement.
-
Configurable Fuzzy Weights: Adjust weights for low, medium, and high similarity to fine-tune recommendation behavior.
-
Error Handling: Robust handling of invalid user/item IDs and empty databases.
-
Logging: Optional logging of recommendations to a file for debugging and analysis.
-
Easy Integration: Lightweight and compatible with Python 3.6+.
Installation
Install AIFuzzyRec using pip:
pip install ai-fuzzy-rec
Or, if using Poetry, add it to your project:
poetry add ai-fuzzy-rec
Requirements
Python >= 3.6 pandas >= 2.0.0 scikit-fuzzy >= 0.4.2 numpy >= 1.25.0 scipy >= 1.10.0 packaging >= 21.3
Usage
Basic Example
Generate recommendations using a SQLite database (purchases.db) containing user-item interactions:
from ai_fuzzy_rec import FuzzyRecommender
# Initialize with default settings
with FuzzyRecommender(db_path='purchases.db') as recommender:
# Recommend items similar to item ID 2
item_rec = recommender.recommend_items_for_item(2, top_n=3)
print(f"Items recommended for item 2: {[(item, round(score, 3)) for item, score in item_rec]}")
# Recommend items for user ID 1
user_rec = recommender.recommend_items_for_user(1, top_n=3)
print(f"Items recommended for user 1: {[(item, round(score, 3)) for item, score in user_rec]}")
Expected Output:
Items recommended for item 2: [(3, 0.5), (4, 0.5), (1, 0.5)]
Items recommended for user 1: [(4, 1.0), (5, 0.5)]
Custom Schema and Parameters
Use a custom database schema and adjust fuzzy weights:
from ai_fuzzy_rec import FuzzyRecommender
with FuzzyRecommender(
db_path='custom.db',
table_name='orders',
user_col='customer_id',
item_col='product_id',
fuzzy_weights=(0.1, 0.4, 0.5), # Emphasize high similarity
log_file='custom_recommendations.log'
) as recommender:
item_rec = recommender.recommend_items_for_item(2, top_n=2)
print(f"Top 2 items for item 2: {item_rec}")
Database Setup
Create a SQLite database with user-item interactions:
import sqlite3
# Create a sample database
conn = sqlite3.connect('purchases.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS purchases
(user_id INTEGER, item_id INTEGER)''')
# Insert sample data
data = [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (2, 4),
(3, 3), (3, 4), (3, 5), (4, 1), (4, 4), (5, 1), (5, 2)]
cursor.executemany('INSERT INTO purchases VALUES (?, ?)', data)
conn.commit()
conn.close()
Testing
Run tests to verify functionality:
poetry run pytest
This executes tests in tests/test_ai_fuzzy_rec.py, ensuring recommendations and error handling work as expected.
Project Structure
/home/m/pyprj/
├── ai_fuzzy_rec/
│ ├── __init__.py
│ ├── ai_fuzzy_rec.py
├── create_database.py
├── purchases.db
├── pyproject.toml
├── README.md
├── LICENSE
├── test_recommender.py
└── tests/
└── test_ai_fuzzy_rec.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_fuzzy_rec-0.1.0.tar.gz.
File metadata
- Download URL: ai_fuzzy_rec-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-35-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7992a4399de8cd1205901cd5c3fbe910c7e3ec1f546c343381f9303606ba8786
|
|
| MD5 |
d3192780b7a0dc90d80ce9f6558fcdbe
|
|
| BLAKE2b-256 |
2a775045383c3b835590c71d506dea27741c78aba13d1beb53750d3b6928ac1f
|
File details
Details for the file ai_fuzzy_rec-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ai_fuzzy_rec-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-35-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51e5adc61635a51d5b4dbfbc6320349ac69cfd03e9c3d92d509b0e0e2d4bb0ad
|
|
| MD5 |
91d5620e5f023cde430fd8d115a9eefc
|
|
| BLAKE2b-256 |
833f542e244ef33eb5357b1d77c7cabce456d9c1e66bb629b320b7406a4370bb
|