aggiunto percettrone da hub scuola

This commit is contained in:
2026-05-14 08:55:40 +02:00
commit e52972b461
4 changed files with 145 additions and 0 deletions

46
.gitignore vendored Normal file
View File

@@ -0,0 +1,46 @@
# Python bytecode and caches
__pycache__/
*.py[cod]
*$py.class
# Virtual environments
.venv/
venv/
env/
ENV/
# Packaging and build output
build/
dist/
*.egg-info/
.eggs/
# Test, coverage, and type-checker caches
.pytest_cache/
.coverage
htmlcov/
.mypy_cache/
.ruff_cache/
.pyre/
# Jupyter notebooks
.ipynb_checkpoints/
# IDE and editor files
.idea/
.vscode/
*.swp
*.swo
*~
# OS files
.DS_Store
Thumbs.db
# Local environment files
.env
.env.*
# Codex/agent workspace metadata
.agents/
.codex/

39
main.py Normal file
View File

@@ -0,0 +1,39 @@
import sys
import percettrone
def main():
successo, weights, bias = percettrone.carica_pesi("pesi_concerto.txt")
if not successo:
print("Terminazione del programma a causa di un errore nel caricamento.")
sys.exit(1)
print("Inserisci i dati:")
input_data = []
domande = [
"Artista famoso? (1=Si, 0=No): ",
"Bel meteo? (1=Si, 0=No): ",
"Amici presenti? (1=Si, 0=No): ",
"Cibo buono? (1=Si, 0=No): ",
"Alcool disponibile? (1=Si, 0=No): "
]
for domanda in domande:
while True:
try:
risposta = int(input(domanda))
input_data.append(risposta)
break
except ValueError:
print("Errore: Inserisci solo numeri interi (1 o 0).")
decisione = percettrone.prevedi(weights, bias, input_data)
if decisione == 1:
print("\nVai al concerto!")
else:
print("\nResta a casa!")
if __name__ == "__main__":
main()

54
percettrone.py Normal file
View File

@@ -0,0 +1,54 @@
FEATURES = 5
THRESHOLD = 0.5
def activation(x: float) -> int:
if x > THRESHOLD:
return 1
else:
return 0
def carica_pesi(filename: str):
weights = []
try:
with open(filename, "r") as file:
for i in range(FEATURES):
line = file.readline().strip()
if line.startswith("Peso"):
try:
val = float(line.split(':')[1].strip())
weights.append(val)
except (IndexError, ValueError):
print(f"Errore nella lettura del peso {i}")
return False, None, None
else:
print(f"Errore nella lettura del peso {i}")
return False, None, None
line = file.readline().strip()
if line.startswith("Bias:"):
try:
bias = float(line.split(':')[1].strip())
except (IndexError, ValueError):
print("Errore nella lettura del bias")
return False, None, None
else:
print("Errore nella lettura del bias")
return False, None, None
return True, weights, bias
except FileNotFoundError:
print(f"Errore: file {filename} non trovato!")
return False, None, None
except Exception as e:
print(f"Errore imprevisto: {e}")
return False, None, None
def prevedi(weights: list, bias: float, input_data: list) -> int:
if len(weights) != FEATURES or len(input_data) != FEATURES:
raise ValueError(f"Attesi {FEATURES} pesi e {FEATURES} input.")
somma = bias + sum(w * i for w, i in zip(weights, input_data))
return activation(somma)

6
pesi_concerto.txt Normal file
View File

@@ -0,0 +1,6 @@
Peso 1: 0.7
Peso 2: 0.6
Peso 3: 0.5
Peso 4: 0.3
Peso 5: 0.4
Bias: 0.0