我正在开发一个 NLP 应用程序,其中有一个文本文件语料库。我想使用Gensim word2vec 算法创建词向量。
我做了 90% 的训练和 10% 的测试拆分。我在适当的集上训练了模型,但我想评估模型在测试集上的准确性。
我在互联网上浏览过任何关于准确性评估的文档,但我找不到任何允许我这样做的方法。有谁知道进行精度分析的函数?
我处理测试数据的方式是从测试文件夹中的文本文件中提取所有句子,并将其变成一个巨大的句子列表。在那之后,我使用了一个我认为是正确的函数(事实证明它不是因为它给了我这个错误:TypeError: 不知道如何处理 uri)。这是我如何去做的:
test_filenames = glob.glob('./testing/*.txt')
print("Found corpus of %s safety/incident reports:" %len(test_filenames))
test_corpus_raw = u""
for text_file in test_filenames:
txt_file = open(text_file, 'r')
test_corpus_raw += unicode(txt_file.readlines())
print("Test Corpus is now {0} characters long".format(len(test_corpus_raw)))
test_raw_sentences = tokenizer.tokenize(test_corpus_raw)
def sentence_to_wordlist(raw):
clean = re.sub("[^a-zA-Z]"," ", raw)
words = clean.split()
return words
test_sentences = []
for raw_sentence in test_raw_sentences:
if len(raw_sentence) > 0:
test_sentences.append(sentence_to_wordlist(raw_sentence))
test_token_count = sum([len(sentence) for …Run Code Online (Sandbox Code Playgroud) 我正在对我拥有的数据集运行拼写校正功能。我曾经from pathos.multiprocessing import ProcessingPool as Pool做过这项工作。处理完成后,我想实际访问结果。这是我的代码:
import codecs
import nltk
from textblob import TextBlob
from nltk.tokenize import sent_tokenize
from pathos.multiprocessing import ProcessingPool as Pool
class SpellCorrect():
def load_data(self, path_1):
with codecs.open(path_1, "r", "utf-8") as file:
data = file.read()
return sent_tokenize(data)
def correct_spelling(self, data):
data = TextBlob(data)
return str(data.correct())
def run_clean(self, path_1):
pool = Pool()
data = self.load_data(path_1)
return pool.amap(self.correct_spelling, data)
if __name__ == "__main__":
path_1 = "../Data/training_data/training_corpus.txt"
SpellCorrect = SpellCorrect()
result = SpellCorrect.run_clean(path_1)
print(result)
result = " ".join(temp …Run Code Online (Sandbox Code Playgroud) python multiprocessing python-3.x python-multiprocessing pathos
我正在使用 xgboost 执行二进制分类。我正在使用 GridSearchCV 来查找最佳参数。但是,一旦发现具有最佳参数的模型,我不知道如何保存最佳模型。我该怎么做?
这是我的代码:
import xgboost as xgb
from sklearn.model_selection import StratifiedKFold, GridSearchCV
xgb_model = xgb.XGBClassifier(objective = "binary:logistic")
params = {
'eta': np.arange(0.1, 0.26, 0.05),
'min_child_weight': np.arange(1, 5, 0.5).tolist(),
'gamma': [5],
'subsample': np.arange(0.5, 1.0, 0.11).tolist(),
'colsample_bytree': np.arange(0.5, 1.0, 0.11).tolist()
}
scorers = {
'f1_score':make_scorer(f1_score),
'precision_score': make_scorer(precision_score),
'recall_score': make_scorer(recall_score),
'accuracy_score': make_scorer(accuracy_score)
}
skf = StratifiedKFold(n_splits=10, shuffle = True)
grid = GridSearchCV(xgb_model,
param_grid = params,
scoring = scorers,
n_jobs = -1,
cv = skf.split(x_train, y_train),
refit = "accuracy_score") …Run Code Online (Sandbox Code Playgroud) 我有以下 PyTorch 张量:
predicted = torch.tensor([4, 4, 4, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 4, 1, 1, 4, 0, 4, 4, 1, 4, 1])
target = torch.tensor([3, 0, 0, 1, 1, 0, 1, 1, 1, 3, 2, 4, 1, 1, 1, 0, 1, 1, 2, 1, 1, 1, 1, 1,])
Run Code Online (Sandbox Code Playgroud)
我想用以下几行计算它们之间的交叉熵损失(作为逻辑回归实现的一部分):
loss = nn.CrossEntropyLoss()
computed_loss = loss(predicted, target)
Run Code Online (Sandbox Code Playgroud)
但是,当我的代码运行时,我得到以下IndexError:
IndexError: Dimension out of range (expected to be in range of [-1, …Run Code Online (Sandbox Code Playgroud) python ×3
python-3.x ×3
gensim ×1
nlp ×1
pathos ×1
pickle ×1
pytorch ×1
scikit-learn ×1
word2vec ×1
xgboost ×1