"""Entrenamiento local supervisado: dígitos UCI, kNN y regresión logística.""" import argparse,json from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.pipeline import make_pipeline from sklearn.linear_model import LogisticRegression from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score,f1_score,confusion_matrix def run(): X,y=load_digits(return_X_y=True); xtr,xte,ytr,yte=train_test_split(X,y,test_size=.25,random_state=42,stratify=y) models={'logistic':make_pipeline(StandardScaler(),LogisticRegression(max_iter=1500,random_state=42)), 'knn':KNeighborsClassifier(n_neighbors=3)} results={} for name,model in models.items(): model.fit(xtr,ytr);pred=model.predict(xte);results[name]={'accuracy':float(accuracy_score(yte,pred)),'macro_f1':float(f1_score(yte,pred,average='macro')),'confusion_matrix':confusion_matrix(yte,pred,labels=list(range(10))).tolist(),'prediction_first':int(pred[0])} return {'train':len(xtr),'test':len(xte),'actual_first':int(yte[0]),'models':results,'seed':42} if __name__=='__main__':print(json.dumps(run(),indent=2))