Scikit-Learn
21
normalize=False)
In Scikit-learn, the
fit()
process have some trailing underscores.
For this example, the below parameter shows the slope of the simple linear fit of the data:
model.coef_
Output
array([1.99839352])
The below parameter represents the intercept of the simple linear fit to the data:
model.intercept_
Output
-0.9895459457775022
Applying the model to new data
After
training the model, we can apply it to new data. As
the main task of supervised
machine learning is to evaluate the model based on new data that is not the part of the
training set. It can be done with the help of
predict()
method as follows:
xfit = np.linspace(-1, 11)
Xfit = xfit[:, np.newaxis]
yfit = model.predict(Xfit)
plt.scatter(x, y)
plt.plot(xfit, yfit);
Output
Scikit-Learn
22
Complete working/executable example
%matplotlib inline
import matplotlib.pyplot as plt
import
numpy as np
import
seaborn as sns
iris = sns.load_dataset('iris')
X_iris = iris.drop('species', axis = 1)
X_iris.shape
y_iris = iris['species']
y_iris.shape
rng = np.random.RandomState(35)
x = 10*rng.rand(40)
y = 2*x-1+rng.randn(40)
plt.scatter(x,y);
from sklearn.linear_model
import LinearRegression
model = LinearRegression(fit_intercept=True)
model
X = x[:, np.newaxis]
X.shape
model.fit(X, y)
Scikit-Learn
23
model.coef_
model.intercept_
xfit = np.linspace(-1, 11)
Xfit = xfit[:, np.newaxis]
yfit = model.predict(Xfit)
plt.scatter(x, y)
plt.plot(xfit, yfit);
Dostları ilə paylaş: