Write a predictions table with to_csv and a figure with savefig. Both land under /uploads. Click ↓ on the chips.
Goal
Export pred.csv and cm.png from a fitted classifier.
Predictions CSV
import os
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X, y = make_classification(n_samples=120, n_features=4, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
out = pd.DataFrame(
{
"true": y_test,
"pred": clf.predict(X_test),
"p1": clf.predict_proba(X_test)[:, 1].round(3),
}
)
out.to_csv("pred.csv", index=False)
print(out.head())
print("uploads:", os.listdir("/uploads"))Confusion-matrix PNG
import os
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
X, y = make_classification(n_samples=200, n_features=4, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
cm = confusion_matrix(y_test, clf.predict(X_test))
fig, ax = plt.subplots()
image = ax.imshow(cm, cmap="Blues")
fig.colorbar(image, ax=ax)
ax.set_xlabel("predicted")
ax.set_ylabel("true")
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, str(cm[i, j]), ha="center", va="center")
ax.set_title("Confusion matrix")
plt.savefig("cm.png", dpi=120, bbox_inches="tight")
plt.show()
print("uploads:", os.listdir("/uploads"))bbox_inches="tight" trims leftover margin. dpi=120 is enough for this panel.
Tip
Call savefig before or after show — both work here. Use a bare filename such as cm.png.