(The handwritten formula was uploaded several times but could not be uploaded.)
The following is an example of logistic regression for male heart disease in South Africa:
The traditional logistic regression code should add the offset to X, and then add 1 to the first position of weights to facilitate matrix operations. I calculate it separately here.
import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt
"""
强烈建议一步一步来,每一步看看输出的结果是不是自己想要的。
"""
#载入数据南非男性心脏病数据
SAHeart_df = pd.read_csv('SAHeart.csv')
SAHeart_df['famhist'] = pd.get_dummies(SAHeart_df['famhist'])#把类型变量famihst变成哑变量
#获得X,Y
#选择特征X
X = (SAHeart_df[['famhist','sbp','tobacco','ldl','adiposity','typea','obesity','alcohol','age']])
Y = np.array(SAHeart_df[['chd']])
#归一化特征
cols_to_Normal = ['sbp','tobacco','ldl','adiposity','typea','obesity','alcohol','age']
X_new = X[cols_to_Normal]
X_new = X_new[cols_to_Normal].apply(lambda rec:(rec