You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改LinearSVC代码实现基于两列输入的多分类预测?

基于两列文本数据训练LinearSVC的修改方案

你当前的代码仅使用了Title列作为输入特征,要纳入第二列文本数据(比如假设另一列为Content,你可替换成实际列名),需分别对两列文本做TF-IDF处理,再合并特征矩阵后输入模型。以下是修改步骤和完整代码:

关键修改点

  • 同时提取两列文本数据作为特征输入
  • 为每列文本单独构建TF-IDF特征(不同列词汇分布不同,需分别拟合转换器)
  • 使用scipy.sparse.hstack合并稀疏特征矩阵,避免维度不匹配

修改后的完整代码

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import LinearSVC
from scipy.sparse import hstack

# 提取标签和两列特征数据
labels_T = df_T['Asso_Sub']
text_title = df_T['Title']  # 原Title列
text_other = df_T['Your_Second_Column']  # 替换为你的第二列实际列名

# 初始化转换器和模型
count_vect_title = CountVectorizer()
count_vect_other = CountVectorizer()
tf_transformer_title = TfidfTransformer()
tf_transformer_other = TfidfTransformer()
model = LinearSVC()

# 划分训练集和测试集:同时传入两列特征
X_train_title, X_test_title, X_train_other, X_test_other, y_train_T, y_test_T = train_test_split(
    text_title, text_other, labels_T, random_state=0, test_size=0.3
)

# 处理Title列的TF-IDF特征
X_train_counts_title = count_vect_title.fit_transform(X_train_title)
X_train_tfidf_title = tf_transformer_title.fit_transform(X_train_counts_title)
X_test_counts_title = count_vect_title.transform(X_test_title)
X_test_tfidf_title = tf_transformer_title.transform(X_test_counts_title)

# 处理第二列的TF-IDF特征
X_train_counts_other = count_vect_other.fit_transform(X_train_other)
X_train_tfidf_other = tf_transformer_other.fit_transform(X_train_counts_other)
X_test_counts_other = count_vect_other.transform(X_test_other)
X_test_tfidf_other = tf_transformer_other.transform(X_test_counts_other)

# 合并两列的训练集和测试集特征矩阵
X_train_combined = hstack([X_train_tfidf_title, X_train_tfidf_other])
X_test_combined = hstack([X_test_tfidf_title, X_test_tfidf_other])

# 标签编码
label_encoder = LabelEncoder()
y_train_encoded = label_encoder.fit_transform(y_train_T)

# 训练模型
clf_T = model.fit(X_train_combined, y_train_encoded)

# (可选)评估模型
y_pred = clf_T.predict(X_test_combined)
y_test_encoded = label_encoder.transform(y_test_T)
print("模型准确率:", (y_pred == y_test_encoded).mean())

注意事项

  • 如果第二列不是文本类型(比如数值型),无需做TF-IDF处理,直接转换为稀疏矩阵后合并即可
  • 可根据数据特性调整CountVectorizer参数(如stop_words、ngram_range)优化特征效果

内容的提问来源于stack exchange,提问作者FRECEENA FRANCIS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 15:45:44