代码之家  ›  专栏  ›  技术社区  ›  ojunk

dill能记住一个类使用的库吗?

  •  0
  • ojunk  · 技术社区  · 7 年前

    dill 为了腌制它,当我解开它时,我找不到图书馆:

    import dill
    from sklearn.metrics.cluster import adjusted_rand_score
    import pandas as pd
    import random
    class Test1():
        def __init__(self, df):
            self.genomes = df
    
        @staticmethod
        def percentageSimilarityDistance(genome1, genome2):
            if len(genome1) != len(genome2):
                raise ValueError('Genome1 and genome2 must have the same length!')
    
            is_gene_correct = [1 if genome1[idx] == genome2[idx] else 0 for idx in range(len(genome1))]
    
            return (1 - sum(is_gene_correct)/(len(is_gene_correct) * 1.0))
    
        def createDistanceMatrix(self, distance_function):
            """Takes a dictionary of KO sets and returns a distance (or similarity) matrix which is basically how many genes do they have in common."""
            genomes_df = self.genomes.copy()
            no_of_genes, no_of_genomes = genomes_df.shape
            list_of_genome_names = list(genomes_df.columns)
            list_of_genomes = [list(genomes_df.loc[:, name]) for name in list_of_genome_names]
            distance_matrix = [[distance_function(list_of_genomes[i], list_of_genomes[j]) for j in range(no_of_genomes)] for i in range(no_of_genomes)]
            distance_matrix = pd.DataFrame(distance_matrix, columns = list_of_genomes, index = list_of_genomes)
    
    
            return distance_matrix
    
    # create fake data
    df = pd.DataFrame({'genome' + str(idx + 1): [random.randint(0, 1) for lidx in range(525)] for idx in range(10)})
    test1 = Test1(df)
    test2 = Test2(df)
    
    # save pickles
    with open('test1.pkl', 'wb') as pkl:
        dill.dump(test1, pkl)
    

    我成功地解开了文件,但当我试图使用其中一个方法时,它找不到 Pandas .

    $ ipython
    Python 3.5.4 |Anaconda custom (64-bit)| (default, Nov 20 2017, 18:44:38) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: import dill
    
    In [2]: with open('test1.pkl', 'rb') as pkl:
       ...:     test1 = dill.load(pkl)
       ...:     
    
    In [3]: test1.createDistanceMatrix(test1.percentageSimilarityDistance)
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-3-5918638722b1> in <module>()
    ----> 1 test1.createDistanceMatrix(test1.percentageSimilarityDistance)
    
    /space/oc13378/myprojects/python/dill_tests/dill_tests.py in createDistanceMatrix(self, distance_function)
         29         return distance_matrix
         30 
    ---> 31 class Test2():
         32     import dill
         33     from sklearn.metrics.cluster import adjusted_rand_score
    
    NameError: name 'pd' is not defined
    

    仅仅通过导入dill库就可以实现这一点吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Mike McKerns    7 年前

    我是 dill 作者。最简单的事情就是把 import

    推荐文章