-2

I create a function which return a score and print a message concern a result . Here the code :

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 

My question is should I add an assumption in the "return " bloc? Or I keep it like this?

Thanks

Nasri
  • 525
  • 1
  • 10
  • 22

2 Answers2

1

You should add something in the return line if you want to get a result to assign in a variable, if not you should erase the return keyword.

AdForte
  • 305
  • 2
  • 12
0

That depends. If you want to use the results (that you calculated) down the line, then yes, you should return it in some format (for example, a dict of your results) If printing it is enough for your needs, then you can just leave it as it is.

[EDITED: Following This answer, I think that in your case you should indeed erase the return. Functionally, it makes no difference, but it helps the reader understand that the function was indeed not meant to return anything in any case (just print).

Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32