Linear Regression On Iris Dataset: Sklearn - Linear - Model Sklearn - Model - Selection Sklearn - Metrics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

05/09/2020 IRIS Model

Linear Regression on Iris Dataset


In [38]:

from sklearn.linear_model import LinearRegression


from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error

In [39]:

from sklearn.datasets import load_iris

In [40]:

iris = load_iris() #['sepal length (cm)','sepal width (cm)','petal length (c


m)','petal width (cm)'],

In [41]:

x = iris.data # iris['data']
y = iris.target # iris['target']

In [42]:

iris['target_names']

Out[42]:

array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

In [43]:

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0)

In [44]:

Lin = LinearRegression()

In [45]:

Lin.fit(x_train,y_train)

Out[45]:

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, norma


lize=False)

In [46]:

Lin.coef_ # Here we find b1 of equation b0+b1*x

Out[46]:

array([-0.17009418, -0.01856621, 0.27900206, 0.56061274])

localhost:8889/nbconvert/html/IRIS Model.ipynb?download=false 1/2


05/09/2020 IRIS Model

In [47]:

Lin.intercept_ # intercept (b0)

Out[47]:

0.35017224206863307

In [48]:

Pred_y = Lin.predict(x_test)

In [49]:

acc = r2_score(y_test,Pred_y)
acc

Out[49]:

0.8998261101639005

In [50]:

#MAE measures the average magnitude of the errors in a set of predictions,withou


t considering their direction.
#The Mean Absolute Error(MAE) is the average of all absolute errors.
mean_absolute_error(y_test,Pred_y)

Out[50]:

0.1978144333979198

In [37]:

#RMSE is a quadratic scoring rule that also measures the average magnitude of th
e error.
# The average squared difference between the estimated values and the actual val
ue
mean_squared_error(y_test,Pred_y)

Out[37]:

0.05886761921232515

localhost:8889/nbconvert/html/IRIS Model.ipynb?download=false 2/2

You might also like