逻辑回归系数用于编写Python函数

我刚刚完成了逻辑回归。数据可以从下面的链接下载:请点击此链接下载数据

以下是逻辑回归的代码。

from sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.metrics import roc_auc_scoreimport pandas as pdscaler = 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.valuesX_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 classpredicted_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)

我得到了奇怪的结果。我知道函数计算中出了问题。

由于我对机器学习和统计学是新手,我可以寻求您的建议吗?


回答:

我想你在xG中漏掉了intercept_。你可以从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))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注