AttributeError:'功能'对象没有属性' ravel'

时间:2017-11-01 12:48:24

标签: python scikit-learn

运行程序时出现以下错误。我附上我的错误和程序如下。我的计划有什么问题?

  

属性错误:'功能'对象没有属性' ravel'

def logistic_mod(df, logProb = 1.0):
    from sklearn import linear_model
    ## Prepare data for model
    nrow = df.shape[0]
    X = df[['x', 'y']].as_matrix().reshape(nrow,2)
    Y = df.z.as_matrix.ravel() #reshape(nrow,1)
    ## Compute the logistic regression model
    lg = linear_model.LogisticRegression()
    logr = lg.fit(X, Y)
    ## Compute the y values
    temp = logr.predict_log_proba(X)
    df['predicted'] = [1 if (logProb > p[1]/p[0]) else 0 for p in temp]
    return df

def sim_log_data(x1, y1, n1, sd1, x2, y2, n2, sd2):  
    import pandas as pd
    import numpy.random as nr

    wx1 = nr.normal(loc = x1, scale = sd1, size = n1)
    wy1 = nr.normal(loc = y1, scale = sd1, size = n1)
    z1 = [1]*n1
    wx2 = nr.normal(loc = x2, scale = sd2, size = n2)
    wy2 = nr.normal(loc = y2, scale = sd2, size = n2)
    z2 = [0]*n2

    df1 = pd.DataFrame({'x': wx1, 'y': wy1, 'z': z1})
    df2 = pd.DataFrame({'x': wx2, 'y': wy2, 'z': z2})
    return pd.concat([df1, df2], axis = 0, ignore_index = True)
mod = logistic_mod(sim_data)
sim_data = sim_log_data(1, 1, 50, 1, -1, -1, 50, 1)

1 个答案:

答案 0 :(得分:2)

错误给你一个线索。 你打电话

df.z.as_matrix.ravel()

错误说

'function' object has no attribute 'ravel'

as_matrix是一个功能。 你打算调用函数并调用ravel

df.z.as_matrix().ravel()