运行时异常。python回调函数求值异常:

2024-04-25 21:39:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为Coursera的机器学习:回归课程做作业。我在用kc峎的房子_数据.gl/创建数据集和GraphLab。我添加新的变量来训练和测试旧变量的组合数据。然后取所有变量的平均值。以下是我要添加的变量:

bedrooms_squared = bedrooms * bedrooms

bed_bath_rooms = bedrooms*bathrooms

log_sqft_living = log(sqft_living)

lat_plus_long = lat + long 

这是我的代码:

^{pr2}$

这是我得到的错误:

RuntimeError: Runtime Exception. Exception in python callback function evaluation: 
ValueError('math domain error',): 
Traceback (most recent call last):
  File "graphlab\cython\cy_pylambda_workers.pyx", line 426, in graphlab.cython.cy_pylambda_workers._eval_lambda
  File "graphlab\cython\cy_pylambda_workers.pyx", line 169, in graphlab.cython.cy_pylambda_workers.lambda_evaluator.eval_simple
  File "<ipython-input-13-1cdbcd5f5d9b>", line 5, in <lambda>
ValueError: math domain error

我不知道这意味着什么。你知道是什么原因造成的吗?我怎么解决它?谢谢。在


Tags: 数据lambdainloglinecythonfileworkers
2条回答

请添加/学习异常以使代码更健壮:

try:
    train_data['log_sqft_living'] = train_data['sqft_living'].apply(lambda x: log(x))
    test_data['log_sqft_living'] = test_data['bedrooms'].apply(lambda x: log(x))
    train_data['lat_plus_long'] = train_data.apply(lambda row: row['lat'] + row['long'])
    train_data['lat_plus_long'] = train_data.apply(lambda row: row['lat'] + row['long'])
    test_data['bedrooms_squared'].mean()
    test_data['bed_bath_rooms'].mean()
    test_data['log_sqft_living'].mean()
    test_data['lat_plus_long'].mean()
except e as Exception:
    print "ERROR in function:", e

您的问题是log接收到一个负数。在

log仅为大于零的数字定义。在

你需要检查你的价值观。在

相关问题 更多 >