Przeglądaj źródła

Checking basic statistics about data

Martin Horvat 3 miesięcy temu
rodzic
commit
966d6a7c57

+ 33 - 0
python/logistic/logit_boots.ipynb

@@ -112,6 +112,39 @@
    ]
   },
   {
+   "cell_type": "code",
+   "execution_count": 5,
+   "id": "3362cf9c",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Predictor (x) statistics:\n",
+      "  Mean: 1.3076\n",
+      "  Std: 0.2609\n",
+      "  Min: 0.7728\n",
+      "  Max: 2.4812\n",
+      "\n",
+      "Predictor (x) statistics:\n",
+      "  Mean: 2.4512\n",
+      "  Std: 0.9736\n",
+      "  Min: 1.6746\n",
+      "  Max: 4.1518\n",
+      "\n",
+      "Response (y) statistics:\n",
+      "  Class 0: 53 samples (91.38%)\n",
+      "  Class 1: 5 samples (8.62%)\n"
+     ]
+    }
+   ],
+   "source": [
+    "# basic descriptive stats\n",
+    "report_utils.basic_stats(x, y)"
+   ]
+  },
+  {
    "cell_type": "markdown",
    "id": "35e9bb7b-5a50-4cae-9af4-261c11453dd4",
    "metadata": {},

+ 37 - 0
python/logistic/src/report_utils.py

@@ -6,6 +6,43 @@ import matplotlib.pyplot as plt
 import seaborn as sns
 import scipy
 
+def basic_stats(x, y):
+    """
+    Print basic descriptive statistics for the predictor and binary response.
+
+    Parameters
+    ----------
+    x : array-like
+        Predictor values.
+    y : array-like
+        Binary response values, expected to contain 0/1.
+    """
+    x = np.asarray(x)
+    y = np.asarray(y)
+
+    if x.ndim != 1:
+        raise ValueError("x must be a 1D array")
+    if y.ndim != 1:
+        raise ValueError("y must be a 1D array")
+    if len(x) != len(y):
+        raise ValueError("x and y must have the same length")
+
+    for cls in np.unique(y):
+        if cls not in (0, 1):
+            raise ValueError("y must contain only binary values (0 and 1)")
+            
+        print("Predictor (x) statistics:")
+        print(f"  Mean: {np.mean(x[y==cls]):.4f}")
+        print(f"  Std: {np.std(x[y==cls], ddof=1):.4f}")
+        print(f"  Min: {np.min(x[y==cls]):.4f}")
+        print(f"  Max: {np.max(x[y==cls]):.4f}")
+        print()
+        
+    print("Response (y) statistics:")
+    unique, counts = np.unique(y, return_counts=True)
+    for cls, count in zip(unique, counts):
+        print(f"  Class {cls}: {count} samples ({100 * count / len(y):.2f}%)")
+
 def plot_pdf(
     data1,
     data2,