fit_transform() method learns from the input data and then, transforms the data as per the calculated parameters.
What are predictors in sklearn?
Predictors are objects that make predictions. When we give a dataset, the estimators first learn from the input data and then, use the calculated parameters to make predictions.
Let’s look into the following example of linear regression.
import seaborn from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression df = seaborn.load_dataset("tips") X_train, X_test, y_train, y_test = train_test_split(df[["total_bill"]], df["tip"], train_size=0.8, shuffle=True, random_state=1) linear_regressor = LinearRegression() linear_regressor.fit(X_train, y_train) print(“Coefficients: “, linear_regressor.coef_) print(“Intercept: “, linear_regressor.intercept_) y_predicted = linear_regressor.predict(X_test) print(y_predicted)
Here, the estimator uses the fit() method to learn the coefficients and intercept of the linear regression problem. After that, the predict() method is called. The predict method make predictions about the output. We can compare this output with y_test to know how well our model performed.
The output of the above program will be:
Coefficients: [0.09450883] Intercept: 1.0891075415709737 [1.37924964 2.8639833 3.60209723 2.4368034 3.0889143 1.82060586 2.15138675 3.06434201 2.45759535 3.41307958 2.23833487 2.0427016 3.91775671 3.47167505 5.891101 3.23162263 3.29399846 2.06160336 2.28842455 3.65785744 3.93760356 2.76947447 2.63149159 3.27887705 2.26385225 2.09562654 3.02559339 3.47829067 2.8554775 3.93004286 2.31772228 3.96217586 2.13059481 4.47535879 4.38084996 2.35741599 4.04439854 2.8233445 2.69292232 2.14477113 3.36488008 3.0407148 3.21461104 2.50768502 3.04449516 3.82702824 3.3629899 4.42148875 2.94620598]






0 Comments