fit 和 predict 契约
每个估计器共用的 API
fit 和 predict 契约 是 CoddyKit 上的免费 Data Science Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Data Science Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Data Science Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Meet the Estimator
In scikit-learn, every model is an estimator: one object you create, teach, and then ask for answers. Same shape, every time. 🤖
Two Verbs to Remember
The whole library rests on two methods: fit to learn from data, and predict to use what it learned. Master these and you can use almost any model.
fit Means Learn
Calling fit shows the model your examples so it can find patterns. Nothing is predicted yet, the model is simply studying the data.
model.fit(X, y)predict Means Answer
Once trained, predict takes fresh inputs and returns the model's best guesses. This is where the learning finally pays off.
predictions = model.predict(X_new)One Consistent Contract
This fit-then-predict pattern is a contract every estimator honors. Swap a tree for a linear model and your code barely changes.
Create Before You Train
You always build the estimator first, often with settings, before any data touches it. That blank model is ready to learn.
from sklearn.linear_model import LinearRegression
model = LinearRegression()Order Always Matters
You must fit before you predict. Asking an untrained model for answers raises an error, since it has learned nothing yet.
fit Returns the Model
The fit call also returns the model itself, so you can chain steps in one line when you want compact, readable code.
model = LinearRegression().fit(X, y)Learned State Lives Inside
After fitting, the model stores what it learned in attributes ending with an underscore, like coef_. They appear only once training is done.
model.coef_Same API, Many Models
Because the API is shared, you can try several models by changing one line. The fit and predict calls stay identical.
from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor()Why This Design Wins
One predictable interface means less to memorize and faster experiments. You focus on the problem, not on each library's quirks.
Quick Check
Let's lock in the core contract every estimator follows.
Recap
Every estimator follows one contract: create it, call fit to learn, then predict to answer. One pattern unlocks the whole library. 🎯
常见问题解答
「fit 和 predict 契约」课时是免费的吗?
是的 — 「fit 和 predict 契约」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Data Science Academy 课程的其余内容,请升级到 CoddyKit PRO。 Data Science Academy 课程共包含 4 节课。
「fit 和 predict 契约」这节课中我会学到什么?
每个估计器共用的 API 你通过在浏览器中直接运行的动手代码来练习 Data Science Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Data Science Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Data Science Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「fit 和 predict 契约」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Data Science Academy 课中编写并运行代码吗?
能。每节 Data Science Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- fit 和 predict 契约
- 特征 X 和目标 y
- 训练线性回归模型
- 为您的第一个模型评分