Python 2.6代码未运行Python 2.7

时间:2017-12-06 20:49:53

标签: python python-2.7 python-2.6

我编写了一个小的Python脚本建模脚本。当我使用Python 2.6解释器运行代码时,它工作得很好,但是当我使用Python 2.7解释器运行代码时,它会抛出三个运行时警告并且完全具有不同的输出!!

我真的把头发拉了出来。任何帮助都赞赏。我是Python的新手,但在我的生活中不能理解为什么相同的代码适用于较旧版本的Python而不是较新版本。

以下是使用Python 2.7运行时的运行时错误和输出:

/beta_test.py:57: RuntimeWarning: overflow encountered in exp
  + (np.dot(items, np.diag(items_coef))))

/beta_test.py:60: RuntimeWarning: invalid value encountered in divide
calc_rate = np.divide(util, sum_util)

/beta_test.py:61: RuntimeWarning: divide by zero encountered in log
lklihd = -sum(sum(np.multiply(np.log(calc_rate), volume)))

RSME for model  nan with a log likelihood of nan

以下是使用Python 2.6运行时的输出:

对于0.00型的RSME,对数似然性为134259.237914

最后,代码:

import numpy as np
import math
import scipy.optimize as op
import matplotlib.pyplot as plt
import pylab


# ---------------------------------------------------------------------    # Reading in data
volume = np.genfromtxt('data_volume.csv', delimiter=",", skip_header=1)
price = np.genfromtxt('data_price.csv', delimiter=',', skip_header=1)
ad = np.genfromtxt('data_ad.csv', delimiter=',', skip_header=1)
addisplay = np.genfromtxt('data_addisplay.csv', delimiter=',', skip_header=1)
display = np.genfromtxt('data_display.csv', delimiter=',', skip_header=1)
adfront = np.genfromtxt('data_adfront.csv', delimiter=',', skip_header=1)
admid = np.genfromtxt('data_admid.csv', delimiter=',', skip_header=1)
adback = np.genfromtxt('data_adback.csv', delimiter=',', skip_header=1)
dist = np.genfromtxt('data_dist.csv', delimiter=',', skip_header=1)
items = np.genfromtxt('data_items.csv', delimiter=',', skip_header=1)

(n, m) = np.shape(volume)

sum_vol = np.dot(np.dot(volume, np.ones((m, 1))), np.ones((1, m)))
emp_rate = np.divide(volume, sum_vol)

# ------------------------------------------------------------------------------
# Likelihood Function definition


def likelihood_function(coefs, flag=False):

    coefs = coefs.reshape(-1, (np.size(coefs)/10))

    intercept = np.reshape(coefs[0], (1, np.size(coefs[0])))
    price_coef = coefs[1]
    ad_coef = coefs[2]
    addisplay_coef = coefs[3]
    display_coef = coefs[4]
    adfront_coef = coefs[5]
    admid_coef = coefs[6]
    adback_coef = coefs[7]
    dist_coef = coefs[8]
    items_coef = coefs[9]

    (a, b) = np.shape(price)
    temp = np.ones((1, a))

    util = (np.exp((np.dot(temp.T, intercept))
                + (np.dot(price, np.diag(price_coef))))
                + (np.dot(ad, np.diag(ad_coef)))
                + (np.dot(addisplay, np.diag(addisplay_coef)))
                + (np.dot(display, np.diag(display_coef)))
                + (np.dot(adfront, np.diag(adfront_coef)))
                + (np.dot(admid, np.diag(admid_coef)))
                + (np.dot(adback, np.diag(adback_coef)))
                + (np.dot(dist, np.diag(dist_coef)))
                + (np.dot(items, np.diag(items_coef))))

    sum_util = np.dot((np.dot(util, np.ones((b, 1)))), np.ones((1, b)))
    calc_rate = np.divide(util, sum_util)
    print(sum_util)
    lklihd = -sum(sum(np.multiply(np.log(calc_rate), volume)))
    if flag:
        return lklihd, calc_rate
    else:
        return lklihd
# ------------------------------------------------------------------------------
# Preparing for the model


# Define the needed coefficients based on the input data of the same size
initial_theta = np.ones((10, m))
# ------------------------------------------------------------------------------
# Likelihood Estimation of the model

# Call Likelihood Function
result = op.minimize(fun=likelihood_function, x0=initial_theta, options={'maxiter':4000})

# ------------------------------------------------------------------------------
# Error Calculations Model
(likelihood, rate) = likelihood_function(result.x, flag=True)
error = (emp_rate - rate)
RMSE = math.sqrt((sum(sum(np.multiply(error, error))))/m)*100

# ------------------------------------------------------------------------------


summary = "RSME for model %4.2f with a log likelihood of %f" % (RMSE, likelihood)
print summary

0 个答案:

没有答案