m0_61104322 2022-04-25 16:45 采纳率: 89.5%
浏览 845
已结题

AttributeError: 'float' object has no attribute 'lower'

在跑LR分类的时候,遇到了以下的问题:

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

# Defining the vectorizer function. 
def cv(data):
    count_vectorizer = CountVectorizer()

    emb = count_vectorizer.fit_transform(data)

    return emb, count_vectorizer

# Here I define my corpus and lables. These are used throughout all the other models/processes.
list_corpus = df_clean_list
list_labels = df_clean_text["sentiment"].values

X_train, X_test, y_train, y_test = train_test_split(list_corpus, list_labels, test_size=0.2, 
                                                                                random_state=40)

X_train_counts, count_vectorizer = cv(X_train)
X_test_counts = count_vectorizer.transform(X_test)

AttributeError                            Traceback (most recent call last)
<ipython-input-10-de04ef9a7d29> in <module>()
     17                                                                                 random_state=40)
     18 
---> 19 X_train_counts, count_vectorizer = cv(X_train)
     20 X_test_counts = count_vectorizer.transform(X_test)

4 frames
/usr/local/lib/python3.7/dist-packages/sklearn/feature_extraction/text.py in _preprocess(doc, accent_function, lower)
     69     """
     70     if lower:
---> 71         doc = doc.lower()
     72     if accent_function is not None:
     73         doc = accent_function(doc)

AttributeError: 'float' object has no attribute 'lower'

请教大家该如何解决,非常感谢!

  • 写回答

3条回答 默认 最新

  • 关注

    float型数据不支持lower()方法,报错的位置先转成str,再使用lower()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月3日
  • 已采纳回答 4月25日
  • 创建了问题 4月25日

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作