Skip to content

Instantly share code, notes, and snippets.

@josinovmota
Created December 11, 2023 21:06
Show Gist options
  • Save josinovmota/43501fa61127f760e9a749235b077df8 to your computer and use it in GitHub Desktop.
Save josinovmota/43501fa61127f760e9a749235b077df8 to your computer and use it in GitHub Desktop.
FINALMENTEEEEEEEEEEEEEEEEEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
features = np.array([X**i for i in range(1, 11)]).T
best_rss_set = []
best_features_idx = []
best_cp = np.inf
best_model_features = []
# rever
full_model = LinearRegression().fit(features, Y)
sigma_hat_squared = mean_squared_error(Y, full_model.predict(features))
features_idx = [i for i in range(features.shape[1])]
while features_idx:
best_rss = None
best_idx = None
for feature_idx in features_idx:
features_set = features[:, best_features_idx + [feature_idx]]
model = LinearRegression().fit(features_set,Y)
y_pred = model.predict(features_set)
Rss = np.sum((Y - y_pred)**2)
if best_rss is None or Rss < best_rss:
best_rss = Rss
best_idx = feature_idx
best_rss_set.append(best_rss)
best_features_idx.append(best_idx)
features_idx.remove(best_idx)
p = len(best_features_idx) + 1
# rever como é o cálculo do c_p statistics que o livro pede
c_p = (best_rss / sigma_hat_squared) - (n - 2 * p)
if c_p < best_cp:
best_cp = c_p
best_model_features = list(best_features_idx)
final_model = LinearRegression().fit(features[:, best_features_idx], Y)
coefficients = final_model.coef_
print(f'O c_p statistics de {best_features_idx} é {c_p}')
print(f'\nO modelo selecionado de acordo com o c_p statistics é:{best_model_features}, e os coeficientes são: {coefficients}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment