简体   繁体   English

为什么 Python 会说某个值不存在时明确表示它不存在?

[英]Why does Python say that a value does not exist when it specifically does?

SHORT DESCRIPTION:简短的介绍:

The Main issue is that whenever i run the following code, i get the error below that:主要问题是,每当我运行以下代码时,都会出现以下错误:

import statsmodels.api as sm
from statsmodels.formula.api import ols    
def onewayanaova (csv, vars, x="x-axis", y="y-axis"):
        df = pd.read_csv(csv, delimiter=",") 
        df_melt = pd.melt(df.reset_index(), id_vars=['index'], value_vars=vars)
        df_melt.columns = ['index', {x}, {y}]
        model = ols(f'{y} ~ C({x})', data=df_melt).fit()
        anova_table = sm.stats.anova_lm(model, typ=2)
        print("The One-Way Anova Test Values are:\n")
        print(anova_table)
onewayanaova("Book1.csv", ["a","b","c"])

The error is:错误是:

Traceback (most recent call last):
  File "pandas\_libs\hashtable_class_helper.pxi", line 5231, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'set'
Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
Traceback (most recent call last):
  File "pandas\_libs\hashtable_class_helper.pxi", line 5231, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'set'
Traceback (most recent call last):
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\compat.py", line 36, in call_and_wrap_exc
    return f(*args, **kwargs)
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\eval.py", line 165, in eval
    return eval(code, {}, VarLookupDict([inner_namespace]
  File "<string>", line 1, in <module>
NameError: name 'axis' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\mghaf\Desktop\Python Codes\ReMan Edu\test.py", line 3, in <module>
    mn.onewayanaova("Book1.csv", ["a","b","c"])
  File "c:\Users\mghaf\Desktop\Python Codes\ReMan Edu\maincode.py", line 154, in onewayanaova
    model = ols(f'{y} ~ C({x})', data=df_melt).fit()
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\statsmodels\base\model.py", line 200, in from_formula
    tmp = handle_formula_data(data, None, formula, depth=eval_env,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\statsmodels\formula\formulatools.py", line 63, in handle_formula_data
    result = dmatrices(formula, Y, depth, return_type='dataframe',
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\highlevel.py", line 309, in dmatrices
    (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\highlevel.py", line 164, in _do_highlevel_design
    design_infos = _try_incr_builders(formula_like, data_iter_maker, eval_env,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\highlevel.py", line 66, in _try_incr_builders
    return design_matrix_builders([formula_like.lhs_termlist,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\build.py", line 693, in design_matrix_builders
    cat_levels_contrasts) = _examine_factor_types(all_factors,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\build.py", line 443, in _examine_factor_types
    value = factor.eval(factor_states[factor], data)
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\eval.py", line 564, in eval
    return self._eval(memorize_state["eval_code"],
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\eval.py", line 547, in _eval
    return call_and_wrap_exc("Error evaluating factor",
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\compat.py", line 43, in call_and_wrap_exc
    exec("raise new_exc from e")
  File "<string>", line 1, in <module>
patsy.PatsyError: Error evaluating factor: NameError: name 'axis' is not defined
    y-axis ~ C(x-axis)
             ^^^^^^^^^

I think it is the X and Y variables I set in def onewayanaova (csv, vars, x="x-axis", y="y-axis"): .我认为这是我在def onewayanaova (csv, vars, x="x-axis", y="y-axis"):设置的 X 和 Y 变量。 Maybe I need to change that so I don't get the error?也许我需要改变它,这样我就不会得到错误?

If you want a more detailed description, read below.如果您想要更详细的描述,请阅读以下内容。

LONG DESCRIPTION:详细描述:

I am trying to do a One Way Anova test.我正在尝试进行单向方差分析测试。 However, the main issue is that python keeps saying that there is a NameError, and that one of my values are not defined.但是,主要问题是 python 一直说存在 NameError,并且我的值之一未定义。

I am running the following code:我正在运行以下代码:

import statsmodels.api as sm
from statsmodels.formula.api import ols    
def onewayanaova (csv, vars, x="x-axis", y="y-axis"):
        df = pd.read_csv(csv, delimiter=",") 
        df_melt = pd.melt(df.reset_index(), id_vars=['index'], value_vars=vars)
        df_melt.columns = ['index', {x}, {y}]
        model = ols(f'{y} ~ C({x})', data=df_melt).fit()
        anova_table = sm.stats.anova_lm(model, typ=2)
        print("The One-Way Anova Test Values are:\n")
        print(anova_table)

And:和:

import maincode as mn
mn.onewayanaova("Book1.csv", ["a","b","c"])

I get the following error (The first code is saved to a file named manicode.py , and the second code is saved to a file named test.py . "Book1.csv" is in the same folder as them).我收到以下错误(第一个代码保存到名为manicode.py的文件中,第二个代码保存到名为test.py的文件中。 "Book1.csv"与它们位于同一文件夹中)。 The error is:错误是:

Traceback (most recent call last):
  File "pandas\_libs\hashtable_class_helper.pxi", line 5231, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'set'
Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
Traceback (most recent call last):
  File "pandas\_libs\hashtable_class_helper.pxi", line 5231, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'set'
Traceback (most recent call last):
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\compat.py", line 36, in call_and_wrap_exc
    return f(*args, **kwargs)
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\eval.py", line 165, in eval
    return eval(code, {}, VarLookupDict([inner_namespace]
  File "<string>", line 1, in <module>
NameError: name 'axis' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\mghaf\Desktop\Python Codes\ReMan Edu\test.py", line 3, in <module>
    mn.onewayanaova("Book1.csv", ["a","b","c"])
  File "c:\Users\mghaf\Desktop\Python Codes\ReMan Edu\maincode.py", line 154, in onewayanaova
    model = ols(f'{y} ~ C({x})', data=df_melt).fit()
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\statsmodels\base\model.py", line 200, in from_formula
    tmp = handle_formula_data(data, None, formula, depth=eval_env,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\statsmodels\formula\formulatools.py", line 63, in handle_formula_data
    result = dmatrices(formula, Y, depth, return_type='dataframe',
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\highlevel.py", line 309, in dmatrices
    (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\highlevel.py", line 164, in _do_highlevel_design
    design_infos = _try_incr_builders(formula_like, data_iter_maker, eval_env,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\highlevel.py", line 66, in _try_incr_builders
    return design_matrix_builders([formula_like.lhs_termlist,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\build.py", line 693, in design_matrix_builders
    cat_levels_contrasts) = _examine_factor_types(all_factors,
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\build.py", line 443, in _examine_factor_types
    value = factor.eval(factor_states[factor], data)
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\eval.py", line 564, in eval
    return self._eval(memorize_state["eval_code"],
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\eval.py", line 547, in _eval
    return call_and_wrap_exc("Error evaluating factor",
  File "C:\Users\mghaf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\patsy\compat.py", line 43, in call_and_wrap_exc
    exec("raise new_exc from e")
  File "<string>", line 1, in <module>
patsy.PatsyError: Error evaluating factor: NameError: name 'axis' is not defined
    y-axis ~ C(x-axis)
             ^^^^^^^^^

The main error that I see is that I named the X and Y variables as: x="x-axis", y="y-axis" .我看到的主要错误是我将 X 和 Y 变量命名为: x="x-axis", y="y-axis" But i do not get why that gives me an error, as I made a very neat looking boxplot from it (but I know that X and Y are used as the axis titles):但我不明白为什么这会给我一个错误,因为我从中制作了一个非常整洁的箱线图(但我知道 X 和 Y 被用作轴标题):

def boxplot (csv, vars, x="x-axis", y="y-axis"):
    #https://www.reneshbedre.com/blog/anova.html
    df = pd.read_csv(csv, delimiter=",") 
    df_melt = pd.melt(df.reset_index(), id_vars=['index'], value_vars=vars)
    df_melt.columns = ['index', x, y]
    ax = sns.boxplot(x=x, y=y, data=df_melt, color='#99c2a2')
    ax = sns.swarmplot(x=x, y=y, data=df_melt, color='#7d0013')
    plt.show()

BUT , whenever I write this code from someone else, it gives the output I want:但是,每当我从其他人那里编写这段代码时,它都会给出我想要的输出:

import statsmodels.api as sm
from statsmodels.formula.api import ols
import pandas as pd
df = pd.read_csv("https://reneshbedre.github.io/assets/posts/anova/onewayanova.txt", sep="\t")
df_melt = pd.melt(df.reset_index(), id_vars=['index'], value_vars=['A', 'B', 'C', 'D'])
df_melt.columns = ['index', 'treatments', 'value']
model = ols('value ~ C(treatments)', data=df_melt).fit()
anova_table = sm.stats.anova_lm(model, typ=2)
print(anova_table)

The output that i get with the above code:我用上面的代码得到的输出:

                sum_sq    df         F    PR(>F)
C(treatments)  3010.95   3.0  17.49281  0.000026
Residual        918.00  16.0       NaN       NaN

The main issue is that i need to change values of model = ols('value ~ C(treatments)', data=df_melt).fit() and df_melt.columns = ['index', 'treatments', 'value'] because most datasets do not have 'treatments', 'value' as their database.主要问题是我需要更改model = ols('value ~ C(treatments)', data=df_melt).fit()df_melt.columns = ['index', 'treatments', 'value']因为大多数数据集没有'treatments', 'value'作为他们的数据库。 If your wondering what my .csv file has is this:如果您想知道我的 .csv 文件是这样的:

  1. Column headers of a, b and c a、b 和 c 的列标题
  2. A list of equal amount of numbers in each of them每个列表中的等量数字

My main issue is:我的主要问题是:

Please try and help me understand why I cannot replace 'value ~ C(treatments)' with X and Y!请尝试帮助我理解为什么我不能用 X 和 Y 替换'value ~ C(treatments)'

Source of the code: https://www.reneshbedre.com/blog/anova.html代码来源: https : //www.reneshbedre.com/blog/anova.html

In statsmodels formulae, you need to quote your variables (ie columns in your dataframe) when they contain special characters such as - .在 statsmodels 公式中,当变量包含特殊字符(例如- )时,您需要引用它们(即数据框中的列)。 Have a look at the documentation , your term "x-axis" is interpreted as "x" - "axis".查看文档,您的术语“x 轴”被解释为“x”-“轴”。 Quoting variable can be done with the Q() transformation.引用变量可以通过Q()转换来完成。 Make sure to quote the variable name inside with different (single/double) quotes that you use for the string:确保使用用于字符串的不同(单/双)引号引用变量名:

model = ols(f'Q("{y}") ~ C(Q("{x}"))', data=df_melt).fit()

It seems that model = ols('value ~ C(treatments)', data=df_melt).fit() cannot have a variable subsitute (as i had in model = ols(f'{y} ~ C({x})', data=df_melt).fit() ).似乎model = ols('value ~ C(treatments)', data=df_melt).fit()不能有变量替代(就像我在model = ols(f'{y} ~ C({x})', data=df_melt).fit() )。 This is also the case if i use model = ols(f'Q("{y}") ~ C(Q("{x}"))', data=df_melt).fit() , as mentioned by @Rob.如果我使用model = ols(f'Q("{y}") ~ C(Q("{x}"))', data=df_melt).fit()也是这种情况.

Therefore, to make it work and have my own names, i just have to rename df_melt.columns = ['index', 'treatments', 'value'] in relation to model = ols('value ~ C(treatments)', data=df_melt).fit() (where 'treatments', 'value' are the same thing in teh two lines of code).因此,为了使它工作并有我自己的名字,我只需要重命名df_melt.columns = ['index', 'treatments', 'value']model = ols('value ~ C(treatments)', data=df_melt).fit() (其中'treatments', 'value'在两行代码中是相同的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM