如何修复sklearn.model_selection的GridSearchCV库中已启动的运行时错误

时间:2019-04-23 14:25:06

标签: python machine-learning scikit-learn

我正在使用svm对新闻组主题进行分类。我正在提供最好的解决方案来对这些数据进行分类。请帮助消除此错误,它阻止了我的整个项目。在这里,当我调用fit methond thourgh GridSearchCV库时,它显示的错误是我提到的代码,之后将出现错误。

from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import names
from nltk.stem import WordNetLemmatizer
from sklearn.svm import SVC

categories=None

tfidf_vectorizer=TfidfVectorizer(stop_words="english",max_features=None)

all_names = set(names.words())
lemmatizer = WordNetLemmatizer()

def letters_only(astr):
    return astr.isalpha()

def clean_text(docs):
    cleaned_docs = []
    for doc in docs:
        cleaned_docs.append(' '.join([lemmatizer.lemmatize(word.lower())
                                        for word in doc.split()
                                        if letters_only(word)
                                        and word not in all_names]))
    return cleaned_docs

data_train=fetch_20newsgroups(subset='train',categories=categories,random_state=42)
data_test=fetch_20newsgroups(subset='test',categories=categories,random_state=42)
cleaned_train=clean_text(data_train.data)
label_train=data_train.target
cleaned_test=clean_text(data_test.data)
label_test=data_test.target
term_docs_train=tfidf_vectorizer.fit_transform(cleaned_train)
term_docs_test=tfidf_vectorizer.fit_transform(cleaned_test)


svc_libsvm=SVC(kernel='linear')
parameters={'C':(0.1,1,10,100)}

from sklearn.model_selection import GridSearchCV
grid_search=GridSearchCV(svc_libsvm,parameters,n_jobs= -1,cv=5)

import timeit
start_time=timeit.default_timer()
print(start_time)
grid_search.fit(term_docs_train,label_train)'''the error come after this statement'''

print("--- %0.3fs seconds ---"% (timeit.default_timer() - start_time))

grid_search.best_params_
grid_search.best_score_
svc_libsvm_best=grid_search.best_estimator_
accuracy=svc_libsvm_best.score(term_docs_test,label_test)
print('the accuracy of 20_class classification is:{0:.1f}%'.format(accuracy*100))```

E00115.534: Exception escaped from start_client

            Traceback (most recent call last):
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/log.py", line 110, in g
                return f(*args, **kwargs)
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/pydevd_hooks.py", line 74, in start_client
                sock, start_session = daemon.start_client((host, port))
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/daemon.py", line 214, in start_client
                with self.started():
              File "/home/alok/anaconda3/lib/python3.7/contextlib.py", line 112, in __enter__
                return next(self.gen)
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/daemon.py", line 110, in started
                self.start()
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/daemon.py", line 145, in start
                raise RuntimeError('already started')
            RuntimeError: already started


Traceback (most recent call last):

Traceback (most recent call last):
/home/alok/anaconda3/lib/python3.7/site-packages/sklearn/externals/joblib/externals/loky/backend/semaphore_tracker.py:74: UserWarning: semaphore_tracker: process died unexpectedly, relaunching.  Some semaphores might leak.
  warnings.warn('semaphore_tracker: process died unexpectedly, '
E00115.575: Exception escaped from start_client

            Traceback (most recent call last):
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/log.py", line 110, in g
                return f(*args, **kwargs)
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/pydevd_hooks.py", line 74, in start_client
                sock, start_session = daemon.start_client((host, port))
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/daemon.py", line 214, in start_client
                with self.started():
              File "/home/alok/anaconda3/lib/python3.7/contextlib.py", line 112, in __enter__
                return next(self.gen)
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/daemon.py", line 110, in started
                self.start()
              File "/home/alok/.vscode/extensions/ms-python.python-2019.3.6558/pythonFiles/lib/python/ptvsd/daemon.py", line 145, in start
                raise RuntimeError('already started')
            RuntimeError: already started


Traceback (most recent call last):

Traceback (most recent call last):
Terminated

1 个答案:

答案 0 :(得分:4)

我个人发现,仅当我在调试模式下通过VSCode运行代码时,才会发生此问题。如果我只是从VSCode内的终端运行它,它将毫无问题地运行