运行python脚本需要太长时间

时间:2016-02-26 18:48:50

标签: python csv

该剧本基本上是根据他们借来的书(代码)来衡量学生之间的关系。因此,我使用ete2包为不同类型的书籍构建了一棵树。现在我尝试编写一段代码,从树和csv文件中获取数据,并通过函数关系进行一些数据分析.csv文件包含超过50,000行。问题是运行代码需要很长时间(大约7天),而它只占我计算机CPU和内存的10%到20%。

以下是我使用过的csv文件示例:

ID Code    Count 
1    A1...   6
1    A2...   5
2    A....   4
2    D....   1
2    A1...   2
3    D....   5
3    D1...   3
3    D2...   5

以下是代码:

from ete2 import Tree
import pandas as pd
import numpy as np
from __future__ import division
import math


data= pd.read_csv('data.csv', names=['ID','Code', 'Count'])
codes_list= list (set(data['Code']))
total_codes= data.shape[0]
students_list= list (set(data['ID']))



####################################


# generate the tree
t = Tree (".....;", format =0)
for i in codes_list:
    if '....' in i:
        node = t.search_nodes(name = '.....')
        node[0].add_child(name= i)
for i in codes_list:
    if '...' in i and '....' not in i:
        if i[0]+'....' in codes_list:
            node = t.search_nodes(name = i[0]+'....')
            node[0].add_child(name= i)
        else:
            node = t.search_nodes(name = '.....')
            node[0].add_child(name= i)

# save the tree in a file 
t.write( outfile= file_path + "Codes_tree.nh", format =3)
return t.get_ascii(show_internal=True)

####################################

def relationship(code1,code2):

    code1_ancestors= t.search_nodes(name=code1)[0].get_ancestors()
    code2_ancestors=t.search_nodes(name=code2)[0].get_ancestors(
    common_ancestors = []
    for a1 in code1_ancestors:
        for a2 in code2_ancestors:
            if a1==a2:
                common_ancestors.append(a1)
    IC_values = []
    for ca in common_ancestors:
        code_descendants=[]
        for gd in ca.get_descendants():
            code_descendants.append(gd.name)
        code_descendants.append(ca)
        frequency= 0
        for k in code_descendants:
                frequency= frequency + code_count.Count[k]

        IC = - (math.log (frequency / float (total_codes)))
        IC_values.append (IC)

    IC_max= max(IC_values)
    return IC_max

##################

relationship_matrix = pd.DataFrame(index=[students_list], columns=[students_list])
for student in students_list:
p1= list (self.data.Code[data.ID==student])
for student1 in students_list:
    p2= list data.Code[data.PID==student1])
    student_l=[]
    for l in p1:
        for m in p2:
            student_l.append(relationship(l,m))

    max_score = np.max(np.array(student_l).astype(np.float))
    relationship_matrix.loc[student,student1] = max_score

print relationship_matrix

2 个答案:

答案 0 :(得分:0)

您可以执行一些“优化”,以下是我可以快速发现的一些示例(假设extension CollectionType where Generator.Element: Equatable, Generator.Element: NotAnyObject { /*...*/ } code1_ancestors等等是列表或等效内容):

code2_ancestors

可以通过以下方式加快:

common_ancestors = []
for a1 in code1_ancestors:
    for a2 in code2_ancestors:
        if a1==a2:
            common_ancestors.append(a1)

并且提到你的for循环实际上最终可能会有共同祖先的重复。

或者这个:

set(code1_ancestors)&set(code2_ancestors)

可以通过以下方式改进:

code_descendants=[]
for gd in ca.get_descendants():
    code_descendants.append(gd.name)

或者这也是:

code_descendants = [gf.name for in ca.get_descendants()]

可以转为:

frequency= 0
for k in code_descendants:
        frequency= frequency + code_count.Count[k]

基本上试图避免迭代地做事,即frequency = code_count.loc[code_descendants, "Count"].sum() 循环,并尝试使用完成整个numpy数组的操作(pandas数据帧的底层结构)。

答案 1 :(得分:0)

我没有看到Tree类的声明,但是在关系()的前两行中引用了一个。

您应该收到“NameError:name't'未定义”