我的一个朋友问我这个线性回归代码,我也无法解决,所以现在也是我的问题。
我们得到的错误: ValueError:endog 和 exog 矩阵的大小不同
当我从 ind_names 中删除“Tech”时,它工作正常。这可能毫无意义,但为了消除语法错误的可能性,我尝试这样做。
技术和金融行业标签在 DataFrame 中分布不均,所以这可能导致大小不匹配?但我无法进一步调试,所以决定问你们。
对错误和解决方案的想法得到一些确认真是太好了。请在下面找到代码。
#We have a portfolio constructed of 3 randomly generated factors (fac1, fac2, fac3).
#Python code provides the following message
#ValueError: The indices for endog and exog are not aligned
import pandas as pd
from numpy.random import rand
import numpy as np
import statsmodels.api as sm
fac1, fac2, fac3 = np.random.rand(3, 1000) #Generate random factors
#Consider a collection of hypothetical stock portfolios
#Generate randomly 1000 tickers
import random; …Run Code Online (Sandbox Code Playgroud) 为什么我的yield关键字没有产生预期的输出?
我正在使用递归算法(合并排序)并使用,yield以便每次更改(排序)时都可以遍历列表。
def MergeSort(lst):
if len(lst) > 1:
middle = len(lst)//2
lefthalf = lst[:middle]
righthalf = lst[middle:]
MergeSort(lefthalf)
MergeSort(righthalf)
i,j,k= 0,0,0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i] < righthalf[j]:
lst[k] = lefthalf[i]
i+=1
else:
lst[k] = righthalf[j]
j+=1
k+=1
while i<len(lefthalf):
lst[k]=lefthalf[i]
i+=1
k+=1
while j<len(righthalf):
lst[k]=righthalf[j]
j+=1
k+=1
yield lst
Run Code Online (Sandbox Code Playgroud)
a = MergeSort([2,3,566,78,8])
for i in a:
print(i)
[2, 3, 566, 78, 8]
Run Code Online (Sandbox Code Playgroud)
相反,我希望实现如下目标:(随着算法的工作)
[2, 3, 566, 78, 8]
[2, 3, 566, 8, 78]
[2, …Run Code Online (Sandbox Code Playgroud)