python - 莳萝能记住一个类使用的库吗?
问题描述
如果我创建一个导入库并用于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 库来使其工作?
解决方案
我是dill
作者。简单的事情就是把import
函数放在里面。此外,如果您将导入放在函数内部和外部,那么您在第一次调用函数时不会受到速度影响。
推荐阅读
- python-3.x - 如何停用 pyenv 虚拟环境
- python - 在远程主机上运行带有 args 的本地 python 脚本
- python - 使用容器进行 Numpy 切片(例如元组或列表)
- java - 浮动服务覆盖将点击从 textView 传递到活动
- flutter - const 变量必须用常量值初始化 || const 与 var || 扑
- sql - 将 SQL 子查询转换为连接以获得性能提升
- r - 使用 gtsummary 包的宽格式多项逻辑回归结果表
- javascript - HTML 显示在实时服务器上,但未在我的烧瓶应用程序上呈现。只有索引显示在烧瓶上
- r - 在 R Shiny 的表中插入 ggplot barplot
- c# - 如何更改 WorkflowApplication 的线程(优先级)