本文整理匯總了Python中pandas.core.frame.DataFrame.transpose方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.transpose方法的具體用法?Python DataFrame.transpose怎麽用?Python DataFrame.transpose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.frame.DataFrame
的用法示例。
在下文中一共展示了DataFrame.transpose方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: generate_input_df
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import transpose [as 別名]
def generate_input_df(self, n_topics, vocab_size, document_length, n_docs,
previous_vocab=None, vocab_prefix=None,
df_outfile=None, vocab_outfile=None,
n_bags=1):
print "Generating input DF"
# word_dists is the topic x document_length matrix
word_dists = self.generate_word_dists(n_topics, vocab_size, document_length)
# generate each document x terms vector
docs = np.zeros((vocab_size, n_docs), dtype=int64)
for i in range(n_docs):
docs[:, i] = self.generate_document(word_dists, n_topics, vocab_size, document_length)
if previous_vocab is not None:
width = vocab_size/n_topics
high = int(document_length / width)
# randomly initialises the previous_vocab part
additional = np.random.randint(high, size=(len(previous_vocab), n_docs))
docs = np.vstack((additional, docs))
df = DataFrame(docs)
df = df.transpose()
print df.shape
if self.make_plot:
self._plot_nicely(df, 'Documents X Terms', 'Terms', 'Docs')
if df_outfile is not None:
df.to_csv(df_outfile)
print "Generating vocabularies"
# initialises vocab to either previous vocab or a blank list
if previous_vocab is not None:
vocab = previous_vocab.tolist()
else:
vocab = []
# add new words
for n in range(vocab_size):
if vocab_prefix is None:
word = "word_" + str(n)
else:
word = vocab_prefix + "_word_" + str(n)
# if more than one bag, then initialise word type too
if n_bags > 1:
word_type = np.random.randint(n_bags)
tup = (word, word_type)
vocab.append(tup)
else:
vocab.append(word)
# save to txt
vocab = np.array(vocab)
if vocab_outfile is not None:
np.savetxt(vocab_outfile, vocab, fmt='%s')
return df, vocab