python ml scikit‐learn - ghdrako/doc_snipets GitHub Wiki
The package revolves around a few central concepts that allow users to efficiently build ML workflows.
Estimators
Estimators are the main objects for training models. Each algorithm in scikit-learn is an estimator with a .fit() method for training and a .predict() method for making predictions. Refer to the following code:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Transformers
Transformers are used to preprocess data. They also follow the fit-transform convention with .fit() to learn the parameters and .transform() to apply the transformations.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
Pipelines
Pipelines allow chaining multiple steps together and allow you to dictate the order of execution of the steps.
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
('scaler', StandardScaler()),
('regressor', LinearRegression())
])
pipeline.fit(X_train, y_train)
Evaluation metrics
Scikit-learn offers several metrics to evaluate your models’ performance, such as accuracy for classification and mean square error (MSE) for regression.
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_true, y_pred)