Iterating over arrays in Python 3

后端 未结 4 761
忘了有多久
忘了有多久 2020-12-06 09:38

I haven\'t been coding for awhile and trying to get back into Python. I\'m trying to write a simple program that sums an array by adding each array element value to a sum. T

4条回答
  •  情歌与酒
    2020-12-06 10:13

    You can use

        nditer
    

    Here I calculated no. of positive and negative coefficients in a logistic regression:

    b=sentiment_model.coef_
    pos_coef=0
    neg_coef=0
    for i in np.nditer(b):
        if i>0:
        pos_coef=pos_coef+1
        else:
        neg_coef=neg_coef+1
    print("no. of positive coefficients is : {}".format(pos_coef))
    print("no. of negative coefficients is : {}".format(neg_coef))
    

    Output:

    no. of positive coefficients is : 85035
    no. of negative coefficients is : 36199
    

提交回复
热议问题