请帮我看看这段代码怎么改?
import pandas as pdimport numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame,Series
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
df = pd.read_excel('dd.xlsx',sheet_name = 'dd') #修改
for year in range(2007,2021):
for semi in :
select = df.loc[(df['year'] == year) & (df['semi'] == semi)]#先筛选出2007年上半年的数据
row_num = select.shape #求筛选出的行数
col_num = select.shape#求筛选出的列数
print('列数:',col_num,'行数:',row_num)
for bank in range(3,col_num+1):#从第三列开始是银行
Y_reg = df.iloc#因变量只取2007年上半年的数据
Y_reg.diff() #差分
X_column = range(3,col_num+1)
X_column = X_column.remove(bank)#出错
X_index = df.iloc
X_reg = X_index.mean(1)#把其他银行的dd取均值对因变量回归
X_reg.diff()
Y_reg = Y_reg.values.reshape(-1,1)
X_reg = X_reg.values.reshape(-1,1)
model = LinearRegression()
model.fit(X_reg,Y_reg)
score = model.score(X_reg,Y_reg)
print(score)
报错内容为:Traceback (most recent call last):
File "C:\Users\swift\Desktop\systemic_risk\code\总的R2回归.py", line 22, in <module>
X_column = X_column.remove(bank)#出错
AttributeError: 'range' object has no attribute 'remove'
X_column = range(3,col_num+1)
你这句话,是什么意思?X_column 打印看下? 本帖最后由 笨鸟学飞 于 2020-10-29 08:56 编辑
>>> x = range(3,10)
>>> type(x)
<class 'range'> #range返回的不是一个列表,所以没有remove这个方法属性
=========你可以这样修改============
X_column = range(3,col_num+1)
X_column = list(X_column ) #把它转为列表
X_column = X_column.remove(bank)
页:
[1]