whjxsgdy 2017-10-15 12:03
浏览 788

python数学计算中内存释放问题,劳烦各位大牛赐教

本人刚开始学python,主要是想做数据计算、分析和处理。在计算过程中,遇到了Memory error
网上也查了不少资料,主要是内存耗尽,每次计算时计算到固定次数就提示Memory error.下面
是我的代码,麻烦各位大牛帮忙看看:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
import time
from scipy import special
import xlwt
from itertools import product

def R(t):
confidence = 0
for num in range(0, 5):
confidence = confidence + np.power(t / 400, num) * np.exp(-t / 400) / special.factorial(num)
return confidence

def g(a, t):
return 1 - a * R(t)

def q(β, t):
return 1 - β * R(4000 - t)

def A(a, β, t):
if t < 500:
return quad(lambda t: R(t), 0, t)[0] / 3500
elif (t > 499 and t < 2907):
return quad(lambda t: a * R(t) ** 2, 0, t)[0] / 3500
elif t == 2907:
return quad(lambda t: a * R(t) ** 2, 0, 2907)[0] / 3500
elif (t > 2907 and t < 3460):
return (quad(lambda t: a * R(t) ** 2, 0, 2907)[0] +
quad(lambda t: β * R(4000 - t) * R(t), 2907, t)[0]) / 3500
elif t == 3460:
return (quad(lambda t: a * R(t) ** 2, 0, 2907)[0] +
quad(lambda t: β * R(4000 - t) * R(t), 2907, 3460)[0]) / 3500
else:
print("超出寿命周期边界")

def MAX(l):
first = [0]
t = list(range(1, 3461))
for i in range(0, len(l)):
if l[i] > first[0]:
first[0] = l[i]
else:
continue
for i in range(0, len(l)):
if l[i] == first[0]:
print("可靠度最大值是%0.3f" % (l[i]))
print("可靠度最大值是对应的时间等于:%0.2f" % (t[i]))
tmax = t[i]
return first[0], tmax

start = time.clock()

初始化抽样参数

N1 = int(input("你想对a和β抽样多少次"))
n1 = 1

xishus = []

At = []

Atjifen = 0

book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet1 = book.add_sheet("可用度", cell_overwrite_ok=False)
sheet2 = book.add_sheet("可用度积分", cell_overwrite_ok=False)
sheet1.write(0, 0, '抽样次数')
sheet1.write(0, 1, "可用度最大值")
sheet1.write(0, 2, "对应时间")
sheet1.write(0, 3, "过评估系数a")
sheet1.write(0, 4, "过评估系数β")
sheet2.write(0, 0, '抽样次数')
sheet2.write(0, 1, "可用度积分")
sheet2.write(0, 2, "过评估系数a")
sheet2.write(0, 3, "欠评估系数β")

amiddle = np.arange(1, 100, 25)

βmiddle = np.arange(1, 100, 25)
aβ = list(product(amiddle, βmiddle))
for i in aβ:
a = i[0] / 100
β = i[1] / 100
ts = list(range(1, 3461))

for t in ts:
    y = A(a, β, t)
    At.append(y)
AT = MAX(At)

sheet1.write(n1, 0, n1)
sheet1.write(n1, 1, AT[0])
sheet1.write(n1, 2, AT[1])
sheet1.write(n1, 3, a)
sheet1.write(n1, 4, β)


for middleA in At:
    Atjifen = Atjifen + middleA

sheet2.write(n1, 0, n1)
sheet2.write(n1, 1, Atjifen)
sheet2.write(n1, 2, a)
sheet2.write(n1, 3, β)
At = []
print("计算次数:" + str(n1))
n1 = n1 + 1
Atjifen = 0

print(time.clock() - start)
book.save("E:/1.xls")

以上就是我的代码,期间尝试用过del删除变量,用过gc.collect(),发现都没起作用。
也想使用numpy.array,但是条件函数A出错,实在无计可施了。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样