我在摆弄一些我正在做的线性回归讲座的代码,并且想知道(实际上已经有一段时间了)为什么我的代码产生了太多的输出。
我将具有7行的csv文件加载到DataFrame中,并创建所有可能的索引组合,其内容如下:
perms = combinations(df.index.tolist(), 2)
然后,因为我还没有python熟练,所以我仍然使用“ append”通过以下代码来计算DataFrame中点的各种组合的斜率和截距:
slopes = []
intercepts = []
for permutation in perms:
slopes.append(calculate_slope(df.loc[permutation[0], ["revenue", "apples sold"]], df.loc[permutation[1], ["revenue", "apples sold"]]))
intercepts.append(calculate_intercept(df.loc[permutation[0]], slopes[-1]))
但是,当我使用slopes = intercepts = []
时,它会创建两倍长度的输出吗?长度非常重要,因为在此示例中AB = BA,所以我不想包含额外的集合。
本质上,我的问题是slopes = intercepts = []
和
slopes = []
intercepts = []
提前谢谢!如果您能够以更智能的方式进行列表理解,请告诉我:-)
答案 0 :(得分:0)
$ cat rlunde.py
slopes = intercepts = []
print id(slopes)
print id(intercepts)
slopes = []
intercepts = []
print id(slopes)
print id(intercepts)
$ python rlunde.py
140181275420648
140181275420648
140181275421728
140181275454280
当您编写斜率=截距= []时,斜率和截距是指相同的列表(相同的ID)
而当你写的时候:lopes = [];拦截= [],您有2个列表。