bill amount, tip amount, etc. We are using the linear regression algorithm to predict the tip amount from the total bill amount.
Please note that linear_regressor.fit() method learns from the input data and calculates the coefficients and the intercept for the linear regression problem. The output of these two variables will be:
[0.09450883] 1.0891075415709737
What are transformers in sklearn?
Transformers are objects that transform a dataset. And this transformation happens inside the transform() function. For example, let’s look into the StandardScaler class. The StandardScaler class uses the fit() method to estimate the mean and variance of a column that contains numerical data. Then, it uses the transform() method to transform each number in the column in the following way:
We can use the following Python code to understand the same.
import pandas
from sklearn.preprocessing import StandardScaler
df = pandas.DataFrame([1, 5, 19, 34, 76], columns=["Numbers"])
print("df: \n", df)
standard_scaler = StandardScaler()
standard_scaler.fit(df)
print("Estimated mean: ", standard_scaler.mean_)
print("Estimated variance: ", standard_scaler.var_)
df_scaled = standard_scaler.transform(df)
print("df after tranformation: \n", df_scaled)
The output of the above program will be:
df:
Numbers
0 1
1 5
2 19
3 34
4 76
Estimated mean: [27.]
Estimated variance: [734.8]
df after tranformation:
[[-0.95915495]
[-0.81159265]
[-0.2951246 ]
[ 0.25823403]
[ 1.80763818]]
Please note that instead of calling fit() and transform() methods separately, we can also call fit_transform() method. The …








































0 Comments