Python 中的半监督情感分析?
Semi-supervised sentiment analysis in Python?
我一直在关注这个教程
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如果我不先这样做的话。
how do I use the training I did on the labeled data to then apply to
unlabeled data?
这确实是监督式 ML 试图解决的问题:将 已知 标记 数据作为 [=12= 形式的输入],一个模型试图发现这些数据中存在的 通用模式 。这些模式有望有助于预测 unseen unlabeled 数据的标签。
例如在情感分析(悲伤、快乐)问题中,模型在训练过程后可能发现的模式:
存在一个或多个这样的词表示悲伤:
("misery" , 'sad', 'displaced people', 'homeless'...)
存在一个或多个这样的词表示快乐:
("win" , "delightful", "wedding", ...)
如果给出新的文本文档,我们将在该文档中搜索这些模式,并相应地对其进行标记。
作为旁注:我们通常不会在训练过程中使用整个标记数据集,而是从数据集中(训练集除外)中取出一小部分来验证我们的模型,并验证它发现了一个真正通用的模式,而不是专门为训练数据量身定制的模式。
我一直在关注这个教程 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如果我不先这样做的话。
how do I use the training I did on the labeled data to then apply to unlabeled data?
这确实是监督式 ML 试图解决的问题:将 已知 标记 数据作为 [=12= 形式的输入],一个模型试图发现这些数据中存在的 通用模式 。这些模式有望有助于预测 unseen unlabeled 数据的标签。
例如在情感分析(悲伤、快乐)问题中,模型在训练过程后可能发现的模式:
存在一个或多个这样的词表示悲伤:
("misery" , 'sad', 'displaced people', 'homeless'...)
存在一个或多个这样的词表示快乐:
("win" , "delightful", "wedding", ...)
如果给出新的文本文档,我们将在该文档中搜索这些模式,并相应地对其进行标记。
作为旁注:我们通常不会在训练过程中使用整个标记数据集,而是从数据集中(训练集除外)中取出一小部分来验证我们的模型,并验证它发现了一个真正通用的模式,而不是专门为训练数据量身定制的模式。