我刚完成逻辑回归。可以从以下链接下载数据:
pleas click this link to download the data
以下是逻辑回归的代码。
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_auc_score
import pandas as pd
scaler = StandardScaler()
data = pd.read_csv('data.csv')
dataX = data.drop('outcome',axis =1).values.astype(float)
X = scaler.fit_transform(dataX)
dataY = data[['outcome']]
Y = dataY.values
X_train,X_test,y_train,y_test = train_test_split (X,Y,test_size = 0.25, random_state = 33)
lr = LogisticRegression()
lr.fit(X_train,y_train)
# Predict the probability of the testing samples to belong to 0 or 1 class
predicted_probs = lr.predict_proba(X_test)
print(predicted_probs[0:3])
print(lr.coef_)
我可以打印逻辑回归系数,也可以计算事件发生1或0的概率。
当我使用这些系数编写python函数并计算出现概率1时,使用此:lr.predict_proba(X_test)进行比较时,我没有得到答案。
我写的功能如下:
def xG(bodyPart,shotQuality,defPressure,numDefPlayers,numAttPlayers,shotdist,angle,chanceRating,type):
coeff = [0.09786083,2.30523761, -0.05875112,0.07905136,
-0.1663424 ,-0.73930942,-0.10385882,0.98845481,0.13175622]
return (coeff[0]*bodyPart+ coeff[1]*shotQuality+coeff[2]*defPressure+coeff[3]*numDefPlayers+coeff[4]*numAttPlayers+coeff[5]*shotdist+ coeff[6]*angle+coeff[7]*chanceRating+coeff[8]*type)
我得到了奇怪的答案。我知道函数计算中有误。
我是机器学习和统计学的新手,请问您的建议。
最佳答案
我认为您错过了intercept_
中的xG
。您可以从lr.intercept_
检索它,并且应该将其加到最终公式中:
return 1/(1+e**(-(intercept + coeff[0]*bodyPart+ coeff[1]*shotQuality+coeff[2]*defPressure+coeff[3]*numDefPlayers+coeff[4]*numAttPlayers+coeff[5]*shotdist+ coeff[6]*angle+coeff[7]*chanceRating+coeff[8]*type))
关于python - 从逻辑回归到python写函数的系数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51417007/