CoolFace
Apppublic

Jeol28/Taller3_SVM_Olaya

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
script.js135 linesDownload Raw Back to root
1// ============================================================2// COEFICIENTES REALES - REGRESIÓN LOGÍSTICA3// ============================================================4 5const intercepto = 2.01892713;6 7const coeficientes = {8    Age: -0.031860823700854135,9    Fare: 0.005905064725514673,10    Pclass: -0.2661272974260936,11    SibSp: -0.05316540410036518,12    Parch: -0.312424411308533913};14 15 16// ============================================================17// FUNCIÓN LOGÍSTICA18// ============================================================19 20function sigmoid(z) {21    return 1 / (1 + Math.exp(-z));22}23 24 25// ============================================================26// FUNCIÓN DE PREDICCIÓN27// ============================================================28 29function predecir() {30 31    // Obtener datos del formulario32 33    const age =34        Number(document.getElementById("age").value);35 36    const fare =37        Number(document.getElementById("fare").value);38 39    const pclass =40        Number(document.getElementById("pclass").value);41 42    const sibsp =43        Number(document.getElementById("sibsp").value);44 45    const parch =46        Number(document.getElementById("parch").value);47 48 49    // Validación50 51    if (52        age < 0 ||53        fare < 0 ||54        sibsp < 0 ||55        parch < 056    ) {57        alert("Por favor, ingrese valores válidos.");58        return;59    }60 61 62    // ========================================================63    // CALCULAR LA FUNCIÓN LINEAL64    // ========================================================65 66    const z =67        intercepto +68 69        coeficientes.Age * age +70 71        coeficientes.Fare * fare +72 73        coeficientes.Pclass * pclass +74 75        coeficientes.SibSp * sibsp +76 77        coeficientes.Parch * parch;78 79 80    // ========================================================81    // CALCULAR PROBABILIDAD82    // ========================================================83 84    const probabilidad = sigmoid(z);85 86 87    // ========================================================88    // REALIZAR PREDICCIÓN89    // ========================================================90 91    const sobrevivio = probabilidad >= 0.5;92 93 94    // Elementos de la interfaz95 96    const resultSection =97        document.getElementById("result-section");98 99    const result =100        document.getElementById("result");101 102    const resultIcon =103        document.getElementById("result-icon");104 105    const probability =106        document.getElementById("probability");107 108 109    // Mostrar resultado110 111    resultSection.classList.remove("hidden");112 113 114    if (sobrevivio) {115 116        resultIcon.textContent = "🟢";117 118        result.textContent = "Sobrevive";119 120    } else {121 122        resultIcon.textContent = "🔴";123 124        result.textContent = "No sobrevive";125 126    }127 128 129    // Mostrar probabilidad130 131    probability.textContent =132        "Probabilidad de supervivencia: " +133        (probabilidad * 100).toFixed(2) +134        "%";135}