[FAQ] : Homework 6 Q3 - my best n_estimators is 150, not 100: check DictVectorizer(sparse=...)

Abierto
#528 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

  • #529 de @github-actions — cerrado sin fusionar

Evaluación

Dificultad
1/5
Tiempo estimado
Menos de una hora
Aptitud para principiantes
25/100
Tipo de issue
Documentación
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python, scikit-learn

Línea de trabajo

An open linked pull request (#529) is already addressing this FAQ, and the payload names no files. Check the repository’s existing FAQ format and compare the proposed change with the issue’s answer. Done means the FAQ follows the project’s conventions and clearly explains the sparse=True setting and its Q3 result.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

faq-proposal
Course

machine-learning-zoomcamp

Question

Homework 6 Q3: I sweep n_estimators over [10, 50, 100, 150] and my lowest validation RMSE is at 150, not 100. My random_state is 1 and my split calls are exactly as written. What am I doing wrong?

Answer

Almost certainly nothing — check your DictVectorizer. The homework says:

Use DictVectorizer(sparse=True) to turn the dataframes into matrices.

If you used the default sparse=False (which is what the 06-trees lecture notebook uses, so
it is a very easy thing to copy), you get different numbers. Both runs below are identical code,
same random_state=1, same splits — only the sparse argument differs:

sparse=True   (csr_matrix)
  n_estimators=10   1.837054
  n_estimators=50   1.771567
  n_estimators=100  1.768136   <-- lowest  -> answer 100
  n_estimators=150  1.768804

sparse=False  (ndarray)
  n_estimators=10   1.839221
  n_estimators=50   1.772875
  n_estimators=100  1.768158
  n_estimators=150  1.767692   <-- lowest  -> answer 150

The Q3 answer flips: 100 with sparse=True, 150 with sparse=False. Use sparse=True, since
that is what the homework specifies, and submit 100.

Why do the numbers differ at all? scikit-learn's tree builder takes a different code path for CSR
sparse matrices than for dense arrays. The algorithm is the same, but floating-point summation
order and tie-breaking in split search differ slightly, and those tiny differences change which
samples land in which leaf. The effect is small (~0.002 RMSE here) but it is larger than the 100-vs-150
margin, which is only 0.0007 — so it flips the ranking.

The margin is genuinely tight, which is why this question is worth double-checking:

dv = DictVectorizer(sparse=True)          # <- required by the homework
X_train = dv.fit_transform(df_train.to_dict(orient='records'))
X_val = dv.transform(df_val.to_dict(orient='records'))

For reference, the other questions are not sensitive to this setting — Q1 (model_year), Q4
(10) and Q5 (vehicle_weight) come out the same either way. Q2 shifts from 1.837 to 1.839, which
still rounds to the 1.837 option. Only Q3 actually changes.

Checklist
  • I have searched existing FAQs and this question is not already answered
  • The answer provides accurate, helpful information
  • I have included any relevant code examples or links
Lenguaje dominante
Python
Estrellas
8
Forks
20
Merge medio
4 d 9 h
PR fusionados (30 d)
27

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de DataTalksClub/faq

Todos los issues de DataTalksClub/faq

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.