I am performing model selection in Python. Unfortunately, I got error "TypeError: 'Series' object is not callable". I don't understand what this means and how I could solve this issue. Any suggestions?
This part of the code runs without problems:
def model_selection(X, *args):
# Init
scores = list(itertools.repeat(np.zeros((0,2)), len(args)))
# Categorical variables
categ_cols = {"Gender", "Student", "Married", "Ethnicity"}
# Loop over all admissible number of regressors
K = np.shape(X)[1]
for k in range(K+1):
print("Computing k=%1.0f" % k, end ="")
# Loop over all combinations
for i in combinations(range(K), k):
# Subset X
X_subset = X.iloc[:,list(i)]
# Get dummies for categorical variables
if k>0:
categ_subset = list(categ_cols & set(X_subset.columns))
X_subset = pd.get_dummies(X_subset, columns=categ_subset, drop_first=True)
# Regress
reg = OLS(y,add_constant(X_subset)).fit()
# Metrics
for i,metric in enumerate(args):
score = np.reshape([k,metric(reg)], (1,-1))
scores[i] = np.append(scores[i], score, axis=0)
print("", end="\r")
return scores
# Set metrics
rss = lambda reg : reg.ssr
r2 = lambda reg : reg.rsquared
The error pops up after running the following chunk of code:
# Compute scores
scores = model_selection(X, y, rss, r2)
ms_RSS = scores[0]
ms_R2 = scores[1]
change your function definition to
def model_selection(X,y,*args)
because y
was part of *args
which is causing this issue when it is called.
ie: the error was doing y(reg)
which is an error.