Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnboundLocalError:局部变量';x';转让前参考(房地产价格预测项目)_Python - Fatal编程技术网

Python UnboundLocalError:局部变量';x';转让前参考(房地产价格预测项目)

Python UnboundLocalError:局部变量';x';转让前参考(房地产价格预测项目),python,Python,我是python新手。我在网上找到了一个房地产价格预测项目。但我不断地遇到这个错误,不知道如何修复它。 请帮帮我!谢谢 def predict_price(location, sqft, bath, bhk): loc_index = np.where(x.columns==location)[0][0] x = np.zeros(len(x.columns)) x[0] = sqft x[1] = bath x[2] = bhk if loc_

我是python新手。我在网上找到了一个房地产价格预测项目。但我不断地遇到这个错误,不知道如何修复它。 请帮帮我!谢谢

def predict_price(location, sqft, bath, bhk): 
    loc_index = np.where(x.columns==location)[0][0]
    x = np.zeros(len(x.columns))
    x[0] = sqft
    x[1] = bath
    x[2] = bhk
    if loc_index >=0: 
        x[loc_index] = 1

    return lr_clf.predict(x)[0]
predict_price('1st Phase JP Nagar',1000, 2, 2)

根据您在评论中发送的视频,您在代码的第一行键入了
x
,而不是
x
。尝试:

def predict_price(location, sqft, bath, bhk): 
    loc_index = np.where(X.columns==location)[0][0]
    x = np.zeros(len(x.columns))
    x[0] = sqft
    x[1] = bath
    x[2] = bhk
    if loc_index >=0: 
        x[loc_index] = 1

    return lr_clf.predict(x)[0]
predict_price('1st Phase JP Nagar',1000, 2, 2)
def预测价格(地点、平方英尺、巴斯、bhk): 全球x loc_index=np.where(x.columns==位置)[0][0]


在函数的第一行中,
x
的值是多少?您是在谈论这个吗?x=df12.drop('price',axis='columns')x.head()此处:
loc_index=np.where(x.columns==location)[0][0]
x从何而来?它是一个全局变量吗@13.15. 我是新手,我不确定它是否是一个全局变量。你能正确格式化你的代码并做一个简要的解释吗?
x = np.zeros(len(x.columns))
x[0] = sqft
x[1] = bath
x[2] = bhk
if loc_index >= 0:
    x[loc_index] = 1
    
return lr_clf.predict([x])[0]