在机器学习中使用2列作为训练数据

2024-09-21 00:44:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为ds的数据框架,其中有3列数据,如下所示

         text     count      label
0   I have...        12        pos   
1   You sh...         8        neg
2   In thi...         9        neg
.
.

我给出了一个例子,只使用text创建测试和培训数据,代码如下

X = ds['text']
y = ds['label']
train_X, test_X, train_Y, test_Y = train_test_split(X, y, test_size=0.25, random_state=0)

df_train75 = pd.DataFrame()
df_train75['text'] = train_X
df_train75['label'] = train_Y

df_test25 = pd.DataFrame()
df_test25['text'] = test_X
df_test25['label'] = test_Y

from sklearn.feature_extraction.text import TfidfVectorizer

tfidf_vect_7525 = TfidfVectorizer(ngram_range = (1, 1))
tfidf_vect_7525.fit(ds['text'])
train_X_tfidf_7525 = tfidf_vect_7525.transform(df_train75['text'])
test_X_tfidf_7525 = tfidf_vect_7525.transform(df_test25['text'])

from sklearn.svm import SVC

model = SVC(kernel='linear')
model.fit(train_X_tfidf_7525,train_Y)

====================================================================================================================================

我试图通过简单地用修改第一行来包含textcount

X = ds[['text', 'count']

这给了我一个错误

from sklearn.svm import SVC

model = SVC(kernel='linear')
model.fit(train_X_tfidf_9010,train_Y)

====================================================================================================================================

我的问题是,我应该如何处理这个问题?我试图研究另一个问题,但没有找到答案。我发现的一个“解决方案”是使用

X = ds['text'].astype(str) + ' ' + ds['count'].astype(str)

但我认为这不是解决这个问题的正确选择。 提前谢谢你


Tags: 数据textfromtestdfmodelcountds
1条回答
网友
1楼 · 发布于 2024-09-21 00:44:04

您可以运行一个serviceschedules执行一个runnable

context.getSharedPreferences("pref_file", 0).edit().clear().commit();

24小时后。 比如:

    //...
    private ScheduledFuture mHandle;
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    //...
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Runnable deleteIt = new Runnable() {
            public void run() {
                getSharedPreferences("pref_file", 0).edit().clear().commit();
                mHandle.cancel(false); //don't cancel here if you want it to run every 24 hours
            }
        };
        if(mHandle == null)
            mHandle = scheduler.scheduleAtFixedRate(deleteIt, 60 * 60 * 24, 60 * 60 * 24, SECONDS);
        return START_STICKY;
    }
    //...

你也可以使用Timer

相关问题 更多 >

    热门问题