【发布时间】:2018-08-05 04:47:17
【问题描述】:
我有一个 Panda 数据框,其中一列包含列表的值。我将其中一个列值提供给 kfold。
filtered_labels = filtered_df['labels']
filtered_sentences = filtered_df.drop('labels', axis=1)
kf = KFold(n_splits=5) # Define the split - into 5 folds
kf.get_n_splits(filtered_sentences)
for train_index, test_index in kf.split(filtered_sentences.shape[0]):
X_train, X_test = filtered_sentences.loc[train_index,filtered_sentences.columns], filtered_sentences.loc[test_index,filtered_sentences.columns]
y_train, y_test = filtered_labels[train_index], filtered_labels[test_index]
tdif_vectorizer = TfidfVectorizer(max_df=5,norm='l2',smooth_idf=True,use_idf=True,ngram_range=(1,1))
train_corpus_as_string = [get_string_representation_from_tokens(sentence_tokens)
for sentence_tokens in X_train['setenceTokens']]
tdif_train_features = tdif_vectorizer.fit_transform(train_corpus_as_string)
tdif_test_features = tdif_vectorizer.transform(X_test)
vModel = LogisticRegression()
vModel.fit(tdif_train_features,y_train)
tdif_predicted_data_set = vModel.predict(tdif_test_features)
当我打印它显示如下的内容时,
X_train, X_test = filtered_sentences.loc[train_index,filtered_sentences.columns], filtered_sentences.loc[test_index,filtered_sentences.columns]
X_train['setenceTokens']
Out[642]:
2171 [catastrophic, effect, hiroshima, nagasaki, at...
2172 [iraq, catastrophic, need, replace, constant, ...
2173 [learn, legacy, catastrophic, eruption, via]
2174 [catastrophic, effect, hiroshima, nagasaki, at...
2175 [wish, go, custom, werent, catastrophic]
2176 [best, part, old, baseball, manager, wear, uni...
2177 [learn, event, u, history, year, later]
2178 [catastrophic, effect, hiroshima, nagasaki, at...
2179 [catastrophic, effect, hiroshima, nagasaki, at...
2180 [society, respond, crisis, catastrophic]
2181 [british, upper, class, cause, catastrophic, s...
2182 [dear, anyone, family, alive, 2040]
2183 [scientist, believe, catastrophic, manmade, gl...
2184 [everything, seem, catastrophic, feel, bad, hi...
2185 [jim, blog, catastrophic, outcome, may, come, ...
2186 [u, want, lead, united, state, catastrophic, w...
2187 [stop, extreme, hurt, middle, class]
2188 [learn, legacy, catastrophic, eruption, new, y...
2189 [learn, legacy, catastrophic, eruption, via]
2190 [catastrophic, effect, hiroshima, nagasaki, at...
2191 [good, look, catastrophic, rain, flooding]
...
由于这些值在列表列表中,我想将它们转换为以下格式的数组,['社会,响应,危机,灾难性','某事,灾难性,来,调整'..] 这样我就可以将它提供给我的 tdif_vectorizer.fit_transform(array_of_strings)。
当使用以下迭代标记时,
train_corpus_as_string = [get_string_representation_from_tokens(sentence_tokens)
for sentence_tokens in X_train['setenceTokens']]
在函数中,我打印了我得到的列表,我得到了 nan 作为一个值。请看下文,
....
['escape', 'place', 'hide', 'time', 'space', 'collide']
['niggra', 'first', 'time', 'hear', 'song', 'sky', 'collide']
['even', 'star', 'moon', 'collide', 'oh', 'oh', 'never', 'want', 'back', 'life', 'take', 'word']
nan
and error : TypeError: 'float' object is not iterable
以下是我的 get_string_representation_from_tokens 方法,
def get_string_representation_from_tokens(tokens):
string_tokens = ""
print(tokens)
for token in tokens:
string_tokens += str(token) + " "
return string_tokens
我的最终目标是运行 kfold 5 次并获取训练数据,并使用 TfidfVectorizer 获取向量并提供给逻辑回归模型并预测值。 TfidfVectorizer 期望数据位于字符串数组中。这就是为什么我要迭代上面的列表以获得所需的数组,如上所述。
如何检查一个值是否为 nan 并分配一个空字符串。我尝试了很多方法,但都没有成功。
问题二
我正在尝试创建一个示例,以便轻松运行该想法,但我有一个单独的问题(请原谅我最后问这个问题)。当我拆分数据时,问题就在这里,它引入了 nan 值,
我的原始数据框列值没有任何 null/nan 值,因为见下文,
filtered_sentences.isnull().sum()
Out[652]:
setenceTokens 0
dtype: int64
但是当我使用以下行拆分时,
X_train, X_test = filtered_sentences.loc[train_index,filtered_sentences.columns], filtered_sentences.loc[test_index,filtered_sentences.columns]
并且 X_train 包含 null/nan 值,见下文
X_train.isnull().sum()
Out[653]:
setenceTokens 21
dtype: int64
有 21 个值。我在NaNs suddenly appearing for sklearn KFolds 中看到了类似的问题,但我使用了相同的问题,但我仍然得到了 nan 值。如果我能通过这个,我不需要检查值 nan。抱歉把帖子拖了这么久。
【问题讨论】:
-
这可能是一个简单的解决方法。但请提供minimal reproducible example,以便更轻松地为您提供帮助。提供
get_string_representation_from_tokens()代码、真实示例数据以及您的预期输出。 -
andrew 我已经编辑了我的问题。我已经展示了我得到的输出。希望它有助于理解这个问题
-
这是一个好的开始 - 但您的输入并不是真正可用的输入,它只是您的系列的打印输出,被截断了。您能否仅提供完整的前几行,以及
NA的一个案例,以及确切的预期输出?创建 MCVE 的一部分是您不需要提供确切数据的想法 - 只需提供一个捕捉您遇到的问题的玩具用例。 (如果足够简单,您也可以使用真实数据,但带有...的打印输出不允许其他人使用您的代码来重现问题。) -
我放了点,因为它打印了一个巨大的列表,因为我在 get_string_representation_from_tokens 方法中打印 print(tokens)。我拥有的是熊猫数据框,我将其中一个列值提供给 kfold 以拆分数据,并且我需要在 for 循环中执行上述操作的每个拆分数据集。请查看 for 循环中的内容。我已经提到了我的预期输出,即字符串数组。我会尽量简化问题。
-
请听从 Andrew 的建议,给我们一个完整的玩具示例,我们可以在我们自己的计算机上运行它并显示相同的问题。否则,很难猜测问题出在哪里。
标签: python pandas dataframe vectorization