我创建了一个函数,该函数返回分数并打印有关结果的消息。 这里的代码:
def compute_score(X_cv, clf):
score = clf.predict_proba(X_cv[84582].reshape(1,-1))[0][1] # Prob of a Win
df_top10_feat = pd.DataFrame(data={"Feature":df_cv.columns[:-1],
"Coefficient":clf.coef_[0],
"Value":X_cv[84582],
"Importance":list(clf.coef_ * X_cv[84582])[0]}). \
sort_values("Importance",
ascending=False)
##extract positif importance
df_top10_feat_positif= df_top10_feat[df_top10_feat['Importance']>0]
#extract negatif importance
df_top10_feat_negatif= df_top10_feat[df_top10_feat['Importance']<0]
#print
print("The average of ", df_top10_pos_sort['Feature'].iloc[0], "is", format(df_cv[df_top10_pos_sort['Feature'].iloc[1]].mean(), '.3f'),". The", df_top10_pos_sort['Feature'].iloc[0], "for this opportunity line is", format(df_top10_pos_sort['Value'].iloc[0], '.3f'), "Therefore, the", df_top10_pos_sort['Feature'].iloc[0], "is lower than other similar opportunity lines.")
print("The average of", df_top10_pos_sort['Feature'].iloc[1], "is", format(df_cv[df_top10_pos_sort['Feature'].iloc[1]].mean(), '.3f'),". The ",df_top10_pos_sort['Feature'].iloc[1], "for this opportunity line is", format(df_top10_pos_sort['Value'].iloc[1], '.3f'), "Therefore, the", df_top10_pos_sort['Feature'].iloc[1], " is lower than other similar opportunity lines.")
print("The average of", df_top10_pos_sort['Feature'].iloc[2], "is", format(df_cv[df_top10_pos_sort['Feature'].iloc[2]].mean(), '.3f'),". The", df_top10_pos_sort['Feature'].iloc[1], "for this opportunity line is", format(df_top10_pos_sort['Value'].iloc[2], '.3f'), "Therefore, the", df_top10_pos_sort['Feature'].iloc[2], "is lower than other similar opportunity lines.")
print("The average of", df_top10_neg_sort['Feature'].iloc[0], "is", format(df_cv[df_top10_neg_sort['Feature'].iloc[0]].mean(), '.3f'), ". The",df_top10_neg_sort['Feature'].iloc[0], "for this opportunity line is", format(df_top10_neg_sort['Value'].iloc[0], '.3f'), "Therefore,", df_top10_neg_sort['Feature'].iloc[0], "is lower than other similar opportunity lines.")
print("The average of", df_top10_neg_sort['Feature'].iloc[1], "is", format(df_cv[df_top10_neg_sort['Feature'].iloc[1]].mean(), '.3f'),". The", df_top10_neg_sort['Feature'].iloc[1], "for this opportunity line is", format(df_top10_neg_sort['Value'].iloc[1], '.3f'), "Therefore,", df_top10_neg_sort['Feature'].iloc[1], "is lower than other similar opportunity lines.")
print("The average of", df_top10_neg_sort['Feature'].iloc[2],"is", format(df_cv[df_top10_neg_sort['Feature'].iloc[2]].mean(), '.3f'),". The", df_top10_neg_sort['Feature'].iloc[2], "for this opportunity line is", format(df_top10_neg_sort['Value'].iloc[2], '.3f'), "Therefore,", df_top10_neg_sort['Feature'].iloc[2], "is lower than other similar opportunity lines.")
return
我的问题是我应该在“返回”集团中添加一个假设吗?还是我这样保留它?
谢谢
答案 0 :(得分:1)
如果要获得分配给变量的结果,则应在返回行中添加一些内容;否则,请清除return关键字。
答案 1 :(得分:0)
这取决于。如果您想使用(计算出的)结果,那么可以,您应该以某种格式返回它(例如,结果的dict
)
如果打印足以满足您的需要,则可以按原样保留它。
[编辑:在This answer之后,我认为您确实应该擦除return
。从功能上讲,它没有什么区别,但可以帮助读者理解,该函数的确在任何情况下都不打算返回任何内容(仅打印)。