<jupyter_start><jupyter_text>Binary classification example<jupyter_code>try:
    cancer = pd.read_csv("https://raw.githubusercontent.com/changyaochen/MECE4520/master/lectures/lecture_4/breast_cancer_data.csv")
except URLError:
    cancer = pd.read_csv("../lecture_4/breast_cancer_data.csv")

cancer["label"] = cancer["diagnosis"].apply(lambda x: 0 if x == "B" else 1)
cancer.head()
# fit a logistic regression model 
model = smf.glm(
    formula='label ~ radius_mean', 
    data=cancer,
    family=sm.families.Binomial(),
)
result = model.fit()
print(result.summary())
# bootstrap
np.random.seed(42)

B = 1000  # number of bootstrap
beta_1s = []
for _ in tqdm(range(B)):
    # bootstrap the indices and build the bootstrap data
    idx = np.random.randint(low=0, high=len(cancer), size=len(cancer))
    data_bootstrap = cancer.iloc[idx]
    
    # fit the model
    model_bootstrap = LogisticRegression(penalty="none", random_state=42)
    model_bootstrap.fit(X=data_bootstrap[["radius_mean"]], y=data_bootstrap["label"])
    beta_1s.append(model_bootstrap.coef_.flatten()[0])
print(f"The mean of beta_1 is {np.mean(beta_1s):5.3f}.")
print(f"The standard error of beta_1 is {np.std(beta_1s, ddof=1):5.3f}.")

plt.figure()
sns.histplot(beta_1s, bins=50)
plt.xlabel("slope")
plt.tight_layout()
plt.show()<jupyter_output>The mean of beta_1 is 1.044.
The standard error of beta_1 is 0.093.
StarcoderdataJupyterStructuredCleanDedup