Paul's Python CoursePPC

scikit-learn: make, fit, predict

Not met yet

scikit-learn holds the classical models in one library, and every one is used the same way. Make it, with its settings in the brackets: KNeighborsClassifier(n_neighbors=5). model.fit(X, y) learns from readings and answers; model.predict(X) gives one answer per row of new readings; model.score(X, y) measures it, as accuracy for a classifier. X is always a table, one row per example and one column per reading, even with a single column: x.reshape(-1, 1). What fit learns is kept in attributes ending in an underscore, like coef_, and fit returns the model itself.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks scikit-learn accepts a 1-D array of one reading as X, rather than a table with one column.
  • Thinks the readings go into a model's brackets when it is made, rather than into fit.
  • Thinks fit returns predictions, rather than the fitted model itself.
  • Thinks a newly made model can predict before it has been fitted.
  • Thinks what a model learns is stored under the names of its settings, rather than in attributes ending in an underscore.

Where it comes up