當前位置: 首頁>>代碼示例>>Python>>正文


Python DataFrame.transpose方法代碼示例

本文整理匯總了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
開發者ID:sdrogers,項目名稱:MS2LDA,代碼行數:61,代碼來源:lda_generate_data.py


注:本文中的pandas.core.frame.DataFrame.transpose方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。