在训练集中仅在一列上的tfidfvectorizer

发布于 2025-01-30 04:30:00 字数 1731 浏览 4 评论 0原文

关于TFIDFECTORIZER,我有一个问题。 我的问题是我有3列,一个是需要矢量化的文本,另外两个已经是数字,因此我只需要矢量化其中一个即可。 我已经读到,将数据拆分为培训和测试集后,您需要对数据进行矢量化,因此我已经将数据集分开了:

X = df[['cleaned_tweet_text', 'polarity', 'subjectivity']] # The Tweets
y = df['cyberbullying_type'] # The Label

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 42)

需要对矢量化的是Cleaned_tweet_text,

我已经尝试过(见下文),但我不确定这是正确的方法。

tfidf = TfidfVectorizer(max_features = 1000)
X_train_tfidf = tfidf.fit_transform(X_train.cleaned_tweet_text)
X_test_tfidf = tfidf.transform(X_test.cleaned_tweet_text)

它不会给我一个错误,如果我打印出x_train_tfidf,我会得到这个:

  (0, 217)  0.41700972853730645
  (0, 118)  0.16283369998713235
  (0, 758)  0.16948694862672925
  (0, 404)  0.20143376247898365
  (0, 626)  0.4426572817169202
  (0, 356)  0.20217167680038242
  (0, 871)  0.4634256150008882
  (0, 65)   0.3606189681792524
  (0, 565)  0.38556256201243433
  (1, 719)  0.29478675756557454
  (1, 919)  0.30596230567496185
  (1, 698)  0.36538974359723864
  (1, 485)  0.816429056367109
  (1, 118)  0.13936199719971182
  (2, 342)  0.17134974750083107
  (2, 256)  0.18449190025596335
  (2, 110)  0.3604602574432005
  (2, 290)  0.39210201833562014
  (2, 648)  0.3538174461369334
  (2, 161)  0.2742199778844052
  (2, 251)  0.3864257748655211
  (2, 128)  0.26063790594719993
  (2, 599)  0.18251158997125277
  (2, 123)  0.39339155686431243
  (2, 360)  0.21729849596293152

这意味着它有效吗?所以现在我可以将其放入分类器中?

I have a problem concerning the tfidfVectorizer.
My problem is that I have 3 columns, one is the text that needs to be vectorized and the two others are already numbers, so I only need to vectorize one of them.
I have read that you need to vectorize your data after you have split it into training and test set, so I have split my data set like so:

X = df[['cleaned_tweet_text', 'polarity', 'subjectivity']] # The Tweets
y = df['cyberbullying_type'] # The Label

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 42)

It is the cleaned_tweet_text that needs to be vectorized

I have tried this(see below) but I am not sure this is the right way.

tfidf = TfidfVectorizer(max_features = 1000)
X_train_tfidf = tfidf.fit_transform(X_train.cleaned_tweet_text)
X_test_tfidf = tfidf.transform(X_test.cleaned_tweet_text)

It does not give me an error, and if I print out X_train_tfidf I get this:

  (0, 217)  0.41700972853730645
  (0, 118)  0.16283369998713235
  (0, 758)  0.16948694862672925
  (0, 404)  0.20143376247898365
  (0, 626)  0.4426572817169202
  (0, 356)  0.20217167680038242
  (0, 871)  0.4634256150008882
  (0, 65)   0.3606189681792524
  (0, 565)  0.38556256201243433
  (1, 719)  0.29478675756557454
  (1, 919)  0.30596230567496185
  (1, 698)  0.36538974359723864
  (1, 485)  0.816429056367109
  (1, 118)  0.13936199719971182
  (2, 342)  0.17134974750083107
  (2, 256)  0.18449190025596335
  (2, 110)  0.3604602574432005
  (2, 290)  0.39210201833562014
  (2, 648)  0.3538174461369334
  (2, 161)  0.2742199778844052
  (2, 251)  0.3864257748655211
  (2, 128)  0.26063790594719993
  (2, 599)  0.18251158997125277
  (2, 123)  0.39339155686431243
  (2, 360)  0.21729849596293152

does that mean it works? so now I can put it into a classifier?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文