我一直在关注本教程 https://stackabuse.com/python-for-nlp-sentiment-analysis-with-scikit-learn/ 在python中创建情感分析。但是,这是我不了解的地方:在我看来,他们使用的数据已经被标记了?那么,如何使用我对标记数据进行的训练然后应用于未标记数据?
我想做某事:
假设我有2个数据框: df1是带有标签数据的小数据,df2是带有未标签数据的大数据。我刚刚完成了df1的培训。然后我该如何预测df2的值?
我认为这将像text_classifier.predict(df2.iloc [:,1] .values)一样直接,但这对我不起作用。
如果这个问题看起来很愚蠢,请原谅我,但是我在机器学习和nltk方面没有很多经验...
编辑: 这是我正在处理的代码:
enc = preprocessing.LabelEncoder()
//chat_data = chat_data[:180]
//chat_labels = chat_labels[:180]
chat_labels = enc.fit_transform(chat_labels)
vectorizer = TfidfVectorizer (max_features=2500, min_df=1, max_df=1, stop_words=stopwords.words('english'))
features = vectorizer.fit_transform(chat_data).toarray()
print(chat_data)
X_train, X_test, y_train, y_test = train_test_split(features, chat_labels, test_size=0.2, random_state=0)
text_classifier = RandomForestClassifier(n_estimators=200, random_state=0)
text_classifier.fit(X_train, y_train)
predictions = text_classifier.predict(X_test)
print(confusion_matrix(y_test,predictions))
print(classification_report(y_test,predictions))
print(accuracy_score(y_test, predictions))
chatData = pd.read_csv(r"C:\Users\jgott\OneDrive\Dokumente\Thesis\chat.csv")
unlabeled = chatData.iloc[:,1].values
unlabeled = vectorizer.fit_transform(unlabeled.astype('U'))
print(unlabeled)
//features = vectorizer.fit_transform(unlabeled).toarray()
predictions = text_classifier.predict(unlabeled)
大部分内容完全来自本教程,除了其中包含astype的行,我曾经将其转换为未标记的数据,因为我遇到了valueError告诉我,如果我无法将其从String转换为float不要先这样做。
答案 0 :(得分:0)
我如何使用对标签数据进行的培训然后应用于 未标记的数据?
这确实是受监督的ML试图解决的问题:将已知 标签数据作为(sample, label)
形式的输入,模型试图发现这些数据中存在的通用模式。这些模式有望有助于预测未看见 未标记数据的标签。
例如在情绪分析(悲伤,快乐)问题中,模型可能在训练过程后被发现:
其中一个或多个单词的存在意味着悲伤:
("misery" , 'sad', 'displaced people', 'homeless'...)
一个或多个单词的存在表示幸福:
("win" , "delightful", "wedding", ...)
如果给出了新的文本文档,我们将在文档中搜索这些模式,并相应地对其进行标记。
请注意:我们通常在训练过程中不使用整个标注的数据集,而是从数据集中获取一小部分(训练集除外)以验证我们的模型,并验证它是否发现了一个真正通用的模型模式,而不是专门为训练数据量身定制的模式。