- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
您好,我有以下评论列表:
comments = ['I am very agry','this is not interesting','I am very happy']
这些是相应的标签:
sents = ['angry','indiferent','happy']
我正在使用 tfidf 对这些评论进行矢量化,如下所示:
tfidf_vectorizer = TfidfVectorizer(analyzer='word')
tfidf = tfidf_vectorizer.fit_transform(comments)
from sklearn import preprocessing
我正在使用标签编码器对标签进行矢量化:
le = preprocessing.LabelEncoder()
le.fit(sents)
labels = le.transform(sents)
print(labels.shape)
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.model_selection import train_test_split
with open('tfidf.pickle','wb') as idxf:
pickle.dump(tfidf, idxf, pickle.HIGHEST_PROTOCOL)
with open('tfidf_vectorizer.pickle','wb') as idxf:
pickle.dump(tfidf_vectorizer, idxf, pickle.HIGHEST_PROTOCOL)
这里我使用被动攻击来拟合模型:
clf2 = PassiveAggressiveClassifier()
with open('passive.pickle','wb') as idxf:
pickle.dump(clf2, idxf, pickle.HIGHEST_PROTOCOL)
with open('passive.pickle', 'rb') as infile:
clf2 = pickle.load(infile)
with open('tfidf_vectorizer.pickle', 'rb') as infile:
tfidf_vectorizer = pickle.load(infile)
with open('tfidf.pickle', 'rb') as infile:
tfidf = pickle.load(infile)
在这里,我尝试使用三个新注释及其相应的标签来测试部分拟合的用法,如下所示:
new_comments = ['I love the life','I hate you','this is not important']
new_labels = [1,0,2]
vec_new_comments = tfidf_vectorizer.transform(new_comments)
print(clf2.predict(vec_new_comments))
clf2.partial_fit(vec_new_comments, new_labels)
问题是部分拟合后我没有得到正确的结果,如下所示:
print('AFTER THIS UPDATE THE RESULT SHOULD BE 1,0,2??')
print(clf2.predict(vec_new_comments))
但是我得到了这个输出:
[2 2 2]
因此,我非常感谢您的支持,如果我使用与训练时相同的示例来测试模型,为什么模型没有被更新,所需的输出应该是:
[1,0,2]
我希望感谢您对调整超参数以查看所需输出的支持。
这是完整的代码,用于显示部分拟合:
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
import sys
from sklearn.metrics.pairwise import cosine_similarity
import random
comments = ['I am very agry','this is not interesting','I am very happy']
sents = ['angry','indiferent','happy']
tfidf_vectorizer = TfidfVectorizer(analyzer='word')
tfidf = tfidf_vectorizer.fit_transform(comments)
#print(tfidf.shape)
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(sents)
labels = le.transform(sents)
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.model_selection import train_test_split
with open('tfidf.pickle','wb') as idxf:
pickle.dump(tfidf, idxf, pickle.HIGHEST_PROTOCOL)
with open('tfidf_vectorizer.pickle','wb') as idxf:
pickle.dump(tfidf_vectorizer, idxf, pickle.HIGHEST_PROTOCOL)
clf2 = PassiveAggressiveClassifier()
clf2.fit(tfidf, labels)
with open('passive.pickle','wb') as idxf:
pickle.dump(clf2, idxf, pickle.HIGHEST_PROTOCOL)
with open('passive.pickle', 'rb') as infile:
clf2 = pickle.load(infile)
with open('tfidf_vectorizer.pickle', 'rb') as infile:
tfidf_vectorizer = pickle.load(infile)
with open('tfidf.pickle', 'rb') as infile:
tfidf = pickle.load(infile)
new_comments = ['I love the life','I hate you','this is not important']
new_labels = [1,0,2]
vec_new_comments = tfidf_vectorizer.transform(new_comments)
clf2.partial_fit(vec_new_comments, new_labels)
print('AFTER THIS UPDATE THE RESULT SHOULD BE 1,0,2??')
print(clf2.predict(vec_new_comments))
但是我得到了:
AFTER THIS UPDATE THE RESULT SHOULD BE 1,0,2??
[2 2 2]
最佳答案
您的代码存在多个问题。我将首先阐述显而易见的问题和更复杂的问题:
clf2
学习任何内容之前就对其进行了 pickle。 (即,一旦定义它就将其腌制,它没有任何作用)。如果您只是进行测试,那就没问题了。否则,应在 fit()
或等效调用之后对它们进行 pickle。您在 clf2.partial_fit()
之前调用 clf2.fit()
。这违背了 partial_fit()
的全部目的。当您调用 fit()
时,您实质上是修复了模型将学习的类(标签)。在您的情况下,这是可以接受的,因为在您随后调用 partial_fit()
时,您将给出相同的标签。但这仍然不是一个好的做法。
在partial_fit() 场景中,永远不要调用fit()
。始终使用您的起始数据和新的数据调用 partial_fit()
。但请确保您在第一次调用参数 classes
中的 parital_fit() 时提供了您希望模型学习的所有标签。
现在是最后一部分,关于您的 tfidf_vectorizer
。您可以在 tfidf_vectorizer
上调用 fit_transform()
(本质上是 fit()
和 transformed()
组合) comments
数组。这意味着它在后续调用 transform()
时(就像您在 transform(new_comments)
中所做的那样),它不会从 new_comments 中学习新单词,而只会使用这些单词它在调用 fit()
期间看到了它(comments
中存在的单词)。
LabelEncoder
和 sents
也是如此。
这在在线学习场景中同样不是优选的。您应该立即拟合所有可用数据。但由于您尝试使用 partial_fit()
,我们假设您有非常大的数据集,可能无法立即装入内存。因此,您也希望将某种partial_fit 应用于TfidfVectorizer。但 TfidfVectorizer 不支持 partial_fit()
。事实上,它并不是为大数据而设计的。所以你需要改变你的方法。请参阅以下问题了解更多详细信息:-
抛开一切不谈,如果您仅更改拟合整个数据的 tfidf 部分(comments
和 new_comments
一次),您将获得所需的结果。
请参阅以下代码更改(我可能对其进行了一些整理,并将vec_new_comments
重命名为new_tfidf
,请仔细阅读):
comments = ['I am very agry','this is not interesting','I am very happy']
sents = ['angry','indiferent','happy']
new_comments = ['I love the life','I hate you','this is not important']
new_sents = ['happy','angry','indiferent']
tfidf_vectorizer = TfidfVectorizer(analyzer='word')
le = preprocessing.LabelEncoder()
# The below lines are important
# I have given the whole data to fit in tfidf_vectorizer
tfidf_vectorizer.fit(comments + new_comments)
# same for `sents`, but since the labels dont change, it doesnt matter which you use, because it will be same
# le.fit(sents)
le.fit(sents + new_sents)
下面是不太优选的代码(您正在使用它,我在第 2 点中谈到过它),但只要您进行上述更改,结果就很好。
tfidf = tfidf_vectorizer.transform(comments)
labels = le.transform(sents)
clf2.fit(tfidf, labels)
print(clf2.predict(tfidf))
# [0 2 1]
new_tfidf = tfidf_vectorizer.transform(new_comments)
new_labels = le.transform(new_sents)
clf2.partial_fit(new_tfidf, new_labels)
print(clf2.predict(new_tfidf))
# [1 0 2] As you wanted
正确的方法,或者partial_fit()的使用方式:
# Declare all labels that you want the model to learn
# Using classes learnt by labelEncoder for this
# In any calls to `partial_fit()`, all labels should be from this array only
all_classes = le.transform(le.classes_)
# Notice the parameter classes here
# It needs to present first time
clf2.partial_fit(tfidf, labels, classes=all_classes)
print(clf2.predict(tfidf))
# [0 2 1]
# classes is not present here
clf2.partial_fit(new_tfidf, new_labels)
print(clf2.predict(new_tfidf))
# [1 0 2]
关于machine-learning - 为什么以下部分拟合不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43421889/
gnuplot 中拟合函数的正确方法是什么 f(x)有下一个表格吗? f(x) = A*exp(x - B*f(x)) 我尝试使用以下方法将其拟合为任何其他函数: fit f(x) "data.txt
(1)首先要建立数据集 ? 1
测量显示一个信号,其形式类似于具有偏移量和因子的平方根函数。如何找到系数并在一个图中绘制原始数据和拟合曲线? require(ggplot2) require(nlmrt) # may be thi
我想将以下函数拟合到我的数据中: f(x) = Offset+Amplitudesin(FrequencyT+Phase), 或根据 Wikipedia : f(x) = C+alphasin(ome
我正在尝试使用与此工具相同的方法在 C# 中拟合 Akima 样条曲线:https://www.mycurvefit.com/share/4ab90a5f-af5e-435e-9ce4-652c95c
问题:开放层适合 map ,只有在添加特征之后(视觉),我该如何避免这种情况? 我在做这个 第 1 步 - 创建特征 var feature = new ol.Feature({...}); 第 2
我有一个数据变量,其中包含以下内容: [Object { score="2.8", word="Blue"}, Object { score="2.8", word="Red"}, Objec
我正在尝试用中等大小的 numpy float 组来填充森林 In [3]: data.shape Out[3]: (401125, 5) [...] forest = forest.fit(data
我想用洛伦兹函数拟合一些数据,但我发现当我使用不同数量级的参数时拟合会出现问题。 这是我的洛伦兹函数: function [ value ] = lorentz( x,x0,gamma,amp )
我有一些数据,我希望对其进行建模,以便能够在与数据相同的范围内获得相对准确的值。 为此,我使用 polyfit 来拟合 6 阶多项式,由于我的 x 轴值,它建议我将其居中并缩放以获得更准确的拟合。 但
我一直在寻找一种方法来使数据符合 beta 二项分布并估计 alpha 和 beta,类似于 VGAM 库中的 vglm 包的方式。我一直无法找到如何在 python 中执行此操作。有一个 scipy
我将 scipy.optimize.minimize ( https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html ) 函数与
在过去的几天里,我一直在尝试使用 python 绘制圆形数据,方法是构建一个范围从 0 到 2pi 的圆形直方图并拟合 Von Mises 分布。我真正想要实现的是: 具有拟合 Von-Mises 分
我有一个简单的循环,它在每次迭代中都会创建一个 LSTM(具有相同的参数)并将其拟合到相同的数据。问题是迭代过程中需要越来越多的时间。 batch_size = 10 optimizer = opti
我有一个 Python 系列,我想为其直方图拟合密度。问题:是否有一种巧妙的方法可以使用 np.histogram() 中的值来实现此结果? (请参阅下面的更新) 我目前的问题是,我执行的 kde 拟
我有一个简单的 keras 模型(正常套索线性模型),其中输入被移动到单个“神经元”Dense(1, kernel_regularizer=l1(fdr))(input_layer) 但是权重从这个模
我正在尝试解决 Boston Dataset 上的回归问题在random forest regressor的帮助下.我用的是GridSearchCV用于选择最佳超参数。 问题一 我是否应该将 Grid
使用以下函数,可以在输入点 P 上拟合三次样条: def plotCurve(P): pts = np.vstack([P, P[0]]) x, y = pts.T i = np.aran
我有 python 代码可以生成数字 x、y 和 z 的三元组列表。我想使用 scipy curve_fit 来拟合 z= f(x,y)。这是一些无效的代码 A = [(19,20,24), (10,
我正在尝试从 this answer 中复制代码,但是我在这样做时遇到了问题。我正在使用包 VGAM 中的gumbel 发行版和 fitdistrplus . 做的时候出现问题: fit = fi
我是一名优秀的程序员,十分优秀!