预处理类错误,“ attributeError:' function'对象没有属性。

发布于 2025-01-30 09:13:16 字数 1753 浏览 5 评论 0原文

因此,我现在早些时候做了一个NLP项目,现在我已经腌制了该模型,并试图将其应用于新的数据集中,数据集是我从Twitter取消的内容。因此,当然,新的DataFrame没有与旧数据集相同的列,因此我正在上一堂课来预处理数据,以使其更靠近NLP项目的旧数据框架。这就是我所做的

  def __init__(self):
    pass
  def fit(self, text_column):
    df = pd.DataFrame(text_column)
    df.text_length = self.text_length(text_column)
    df.num_capital_letters = self.num_capital_letters(text_column)
    df.percentage_of_capital_letters = self.percentage_of_capital_letters(text_column)
    df.greater_than_50_percent = self.greater_than_50_percent(text_column)
    df.reading_level = self.reading_level(text_column)
    #df =pd.DataFrame(Text.df_user_tweets
    return df

  def text_length(self,column):
    return column.apply(lambda x: len(x))
  def num_capital_letters(self,column):
    return column.apply.str.findall(r"[A-Z]").str.len()
  def percentage_of_capital_letters(self,column):
    return column.apply.str.findall(r"[A-Z]").str.len()/column.apply(lambda x: len(x))
  def greater_than_50_percent(self,column): 
    return column.apply(lambda x:  x>= .5 )
  def reading_level(self,column):
    return column.apply(lambda x :textstat.flesch_reading_ease(x))

pre = Preprocesser()

pre.fit(text_column = df_user_tweets.Text)

就是我遇到的错误

<ipython-input-136-3b74ba5d2425> in num_capital_letters(self, column)
     17     return column.apply(lambda x: len(x))
     18   def num_capital_letters(self,column):
---> 19     return column.apply.str.findall(r"[A-Z]").len()
     20   def percentage_of_capital_letters(self,column):
     21     return column.apply.str.findall(r"[A-Z]").str.len()/column.apply(lambda x: len(x))

AttributeError: 'function' object has no attribute 'str'

,听起来我的错误在第19行,但不确定我需要做什么修复,请感谢任何帮助

So I did an nlp project earlier now I have pickled the model and trying to apply it to a new data set, the data set is something I scrapped from twitter. So of course the new dataframe doesn't have the same columns as the old dataset, so I am making a class to preprocess the data to make closer the old dataframe which was used for the nlp project. This is what I did

  def __init__(self):
    pass
  def fit(self, text_column):
    df = pd.DataFrame(text_column)
    df.text_length = self.text_length(text_column)
    df.num_capital_letters = self.num_capital_letters(text_column)
    df.percentage_of_capital_letters = self.percentage_of_capital_letters(text_column)
    df.greater_than_50_percent = self.greater_than_50_percent(text_column)
    df.reading_level = self.reading_level(text_column)
    #df =pd.DataFrame(Text.df_user_tweets
    return df

  def text_length(self,column):
    return column.apply(lambda x: len(x))
  def num_capital_letters(self,column):
    return column.apply.str.findall(r"[A-Z]").str.len()
  def percentage_of_capital_letters(self,column):
    return column.apply.str.findall(r"[A-Z]").str.len()/column.apply(lambda x: len(x))
  def greater_than_50_percent(self,column): 
    return column.apply(lambda x:  x>= .5 )
  def reading_level(self,column):
    return column.apply(lambda x :textstat.flesch_reading_ease(x))

pre = Preprocesser()

pre.fit(text_column = df_user_tweets.Text)

This is the error that I got

<ipython-input-136-3b74ba5d2425> in num_capital_letters(self, column)
     17     return column.apply(lambda x: len(x))
     18   def num_capital_letters(self,column):
---> 19     return column.apply.str.findall(r"[A-Z]").len()
     20   def percentage_of_capital_letters(self,column):
     21     return column.apply.str.findall(r"[A-Z]").str.len()/column.apply(lambda x: len(x))

AttributeError: 'function' object has no attribute 'str'

It sounds like my error is in line 19 but not sure what I need to do fix it, appreciate any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2025-02-06 09:13:16

df_user_tweets.text是类型pd.series,它具有方法apply。此方法采用lambda函数来完成该值series(这是列)的一些工作,并且它没有str> str属性。

因此,代替column.apply.findall do column.str.findall

df_user_tweets.Text is of type pd.Series and it has a method apply. this method takes a lambda function to do some work on values of that Series (which is a column), and it does not have an str attribute.

So instead of column.apply.findall do column.str.findall.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文