AttributeError:'功能'对象没有属性'标签_'

时间:2016-12-09 16:54:41

标签: python error-handling attributes

当我执行此代码时,出现以下错误:

  
    
      

属性错误:'功能'对象没有属性'标签_'

    
  

我知道此消息是由以下方法触发的: clusterWithFewestSamples

通过以下行:

minSamples = len(model.labels _)

任何关于错误或缺乏什么的暗示?

非常感谢提前。

霍尔迪阿

import pandas as pd
from datetime import timedelta
import matplotlib.pyplot as plt
import matplotlib
from sklearn.cluster import KMeans

matplotlib.style.use('ggplot')


def model(self, num):
    return

def showandtell(title=None):
    if title != None: plt.savefig(title + ".png", bbox_inches='tight', dpi=300)
    plt.show()
    exit()

def clusterInfo(model):
    print "Cluster Analysis Inertia: ", model.inertia_  
    print '------------------------------------------'
    for i in range(len(model.cluster_centers_)):
      print "\n  Cluster ", i
      print "    Centroid ", model.cluster_centers_[i]
      print "    #Samples ", (model.labels_==i).sum() # NumPy Power

def clusterWithFewestSamples(model):
    minSamples = len(model.labels_)
    minCluster = 0
    for i in range(len(model.cluster_centers_)):
        if minSamples > (model.labels_==i).sum():
            minCluster = i
            minSamples = (model.labels_==i).sum()
        print "\n Cluster With Fewest Samples: ", minCluster
    return (model.labels_==minCluster)

def doKMeans(data, clusters=0):
    df_user1 = data.loc[:, ['TowerLon', 'TowerLat']]
    kmeans = KMeans(n_clusters=clusters)
    kmeans.fit(df_user1)
    labels = kmeans.predict(df_user1)
    centroids = kmeans.cluster_centers_
    ax.scatter(centroids[:,0], centroids[:,1], marker='x', c='red', alpha=0.5, linewidths=3, s=169)
    print centroids
    return model

df = pd.read_csv('Datasets/CDR.csv')
df.CallDate = pd.to_datetime(df.CallDate, errors='coerce')
df.CallTime = pd.to_timedelta(df.CallTime, errors='coerce')

PhoneList = df["In"].unique()

print "\n\nExamining person: ", 0

user1 = df[df.In == PhoneList[0]]
user1 = user1[(user1.DOW != 'Sat') & (user1.DOW != 'Sun')]
user1 = user1[(user1.CallTime > '09:00:00') | (user1.CallTime < '17:00:00')]

fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(user1.TowerLon,user1.TowerLat, c='g', marker='o', alpha=0.2)
ax.set_title('Weekday Calls (>7:306am or <17p)')

model = doKMeans(user1, 2)

midWayClusterIndices = clusterWithFewestSamples(model)
midWaySamples = user1[midWayClusterIndices]
print " Its Waypoint Time: ", midWaySamples.CallTime.mean()

ax.scatter(model.cluster_centers_[:,1], model.cluster_centers_[:,0], s=169, c='r', marker='x', alpha=0.8, linewidths=2)

showandtell('Weekday Calls Centroids')

1 个答案:

答案 0 :(得分:0)

model来自哪里?

def doKMeans(data, clusters=0):
    ...
    return model

model = doKMeans(user1, 2)

doKMeans()中,没有任何内容被分配给model,我很惊讶它没有抛出NameError异常。也许它应该是

def doKMeans(data, clusters=0):
    ...
    return kmeans

sklearn.cluster.KMeans对象确实具有labels_属性。

您希望model具有labels_属性,但您已将model定义为返回None的函数

def model(self, num):
    return

调用doKMeans时,它将返回名为model

的函数
def doKMeans(data, clusters=0):
    ...
    return model

所以声明model = doKMeans(user1, 2)什么都不做:

>>> def f(a,b):
    return
>>> f
<function f at 0x00000000034A9840>

>>> def g(a,b):
    return f

>>> f = g(1,2)
>>> f
<function f at 0x00000000034A9840>

函数model或其返回值None都没有labels_属性。