【问题标题】:Nonetype object has no lower attributeNonetype 对象没有 lower 属性
【发布时间】:2021-08-23 04:55:17
【问题描述】:

AttributeError: 'NoneType' 对象没有属性 'lower'

谁能帮我弄清楚如何解决这个错误!?

搜索了很多次,但似乎没有任何效果。

for feature in features:
  df[feature] = df[feature].replace('-',None)

def combine_features(row):
  try: 
    return row['genre']+ " " + row['star'] +" " + row['name'] +" " +row[' rating'] +" "+ row['director']
  except:
    print ("Error",row)

df["Combined_features"] = df.apply(combine_features,axis = 1)
print("Combined Features",df["Combined_features"].head())

cv = CountVectorizer()
count_matrix = cv.fit_transform(df["Combined_features"])
#here it gives error NoneType object has no attribute 'lower'

cosine_sim = cosine_similarity(count_matrix)
movie_user_likes = "Avatar"

【问题讨论】:

  • 验证您的combined_features 是否有任何nan 值并替换/删除它们。

标签: python pandas numpy matplotlib sklearn-pandas


【解决方案1】:
In [52]: None.lower()
Traceback (most recent call last):
  File "<ipython-input-52-bdce891aab80>", line 1, in <module>
    None.lower()
AttributeError: 'NoneType' object has no attribute 'lower'

cv.fit_transform(df["Combined_features"]) 显然这个fit 需要可以转换为小写的字符串值(因此忽略大小写的区别)。并且 df 列/系列必须有多个字符串,至少有一个 None 元素。您有责任确保提供给 fit 的数据符合其预期。

当您创建该列时,您是否收到任何“错误”报告?在这种情况下,combine 函数将返回 None。

【讨论】:

  • 不,创建此函数时没有任何此类错误报告。这是我收到的错误/usr/local/lib/python3.7/dist-packages/sklearn/feature_extraction/text.py in _preprocess(doc, accent_function, lower) 66 """ 67 if lower: ---&gt; 68 doc = doc.lower() 69 if accent_function is not None: 70 doc = accent_function(doc) AttributeError: 'NoneType' object has no attribute 'lower' 请帮我弄清楚!
  • 我看到了print... head。你为什么不给我们看?或者您有没有花时间查看该专栏的其余部分?请求帮助时,礼貌的做法是提供完整的信息。不要让我们猜测!我已经告诉过你,根据错误,该框架中有None 元素。你需要找到他们!
  • 给您带来的不便,我深表歉意,但我已经提供了上面的全部代码。
猜你喜欢
  • 2020-12-08
  • 2019-12-04
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 2020-09-22
相关资源
最近更新 更多