linear
September 20, 2023
[1]: print(7+3)
10
[2]: print(5+3)
[7]: import seaborn as sns
[8]: iris=sns.load_dataset('iris')
[9]: iris
[9]: sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
.. … … … … …
145 6.7 3.0 5.2 2.3 virginica
146 6.3 2.5 5.0 1.9 virginica
147 6.5 3.0 5.2 2.0 virginica
148 6.2 3.4 5.4 2.3 virginica
149 5.9 3.0 5.1 1.8 virginica
[150 rows x 5 columns]
[10]: iris=iris[['petal_length','petal_width']]
[11]: iris
[11]: petal_length petal_width
0 1.4 0.2
1 1.4 0.2
2 1.3 0.2
3 1.5 0.2
1
4 1.4 0.2
.. … …
145 5.2 2.3
146 5.0 1.9
147 5.2 2.0
148 5.4 2.3
149 5.1 1.8
[150 rows x 2 columns]
[12]: x=iris['petal_length']
y=iris['petal_width']
[18]: import [Link] as plt
[Link](x,y)
[Link]("petal length")
[Link]("petal width")
[18]: Text(0, 0.5, 'petal width')
2
[52]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.
↪4,random_state=23)
[53]: import numpy as np
x_train=[Link](x_train).reshape(-1,1)
x_test=[Link](x_test).reshape(-1,1)
[54]: from sklearn.linear_model import LinearRegression
[55]: lr=LinearRegression()
[56]: [Link](x_train,y_train)
[56]: LinearRegression()
[57]: c=lr.intercept_
c
[57]: -0.35113274221437507
[58]: m=lr.coef_
m
[58]: array([0.41684538])
[48]: y_pred_train=m*x_train+c
y_pred_train.flatten()
[48]: array([1.73309416, 0.31581987, 1.31624878, 0.3575044 , 1.98320139,
1.31624878, 1.64972508, 1.98320139, 1.7747787 , 1.69140962,
0.23245079, 0.31581987, 1.98320139, 0.23245079, 0.31581987,
1.94151685, 1.7747787 , 1.31624878, 0.23245079, 1.35793332,
1.85814777, 1.52467147, 2.06657046, 2.40004677, 1.44130239,
0.19076625, 1.31624878, 1.69140962, 1.69140962, 1.31624878,
0.27413533, 1.52467147, 1.52467147, 1.27456424, 1.73309416,
1.64972508, 1.2328797 , 1.7747787 , 2.27499315, 2.19162408,
0.14908171, 2.02488593, 0.8994034 , 0.27413533, 2.108255 ,
1.64972508, 0.23245079, 1.52467147, 1.39961786, 1.81646324,
0.19076625, 0.06571264, 1.10782609, 0.10739718, 1.60804055,
1.39961786, 0.14908171, 2.06657046, 1.44130239, 1.52467147,
0.31581987, 2.52510038, 1.56635601, 1.7747787 , 1.98320139,
1.60804055, 0.27413533, 0.31581987, 1.94151685, 2.06657046,
1.48298693, 0.19076625, 1.81646324, 1.02445701, 2.02488593,
1.10782609, 0.19076625, 0.27413533, 0.27413533, 1.7747787 ,
0.23245079, 0.23245079, 1.69140962, 0.23245079, 1.48298693,
0.27413533, 1.56635601, 0.27413533, 0.19076625, 1.7747787 ])
3
[59]: y_pred=[Link](x_train)
y_pred
[59]: array([1.73309416, 0.31581987, 1.31624878, 0.3575044 , 1.98320139,
1.31624878, 1.64972508, 1.98320139, 1.7747787 , 1.69140962,
0.23245079, 0.31581987, 1.98320139, 0.23245079, 0.31581987,
1.94151685, 1.7747787 , 1.31624878, 0.23245079, 1.35793332,
1.85814777, 1.52467147, 2.06657046, 2.40004677, 1.44130239,
0.19076625, 1.31624878, 1.69140962, 1.69140962, 1.31624878,
0.27413533, 1.52467147, 1.52467147, 1.27456424, 1.73309416,
1.64972508, 1.2328797 , 1.7747787 , 2.27499315, 2.19162408,
0.14908171, 2.02488593, 0.8994034 , 0.27413533, 2.108255 ,
1.64972508, 0.23245079, 1.52467147, 1.39961786, 1.81646324,
0.19076625, 0.06571264, 1.10782609, 0.10739718, 1.60804055,
1.39961786, 0.14908171, 2.06657046, 1.44130239, 1.52467147,
0.31581987, 2.52510038, 1.56635601, 1.7747787 , 1.98320139,
1.60804055, 0.27413533, 0.31581987, 1.94151685, 2.06657046,
1.48298693, 0.19076625, 1.81646324, 1.02445701, 2.02488593,
1.10782609, 0.19076625, 0.27413533, 0.27413533, 1.7747787 ,
0.23245079, 0.23245079, 1.69140962, 0.23245079, 1.48298693,
0.27413533, 1.56635601, 0.27413533, 0.19076625, 1.7747787 ])
[60]: import [Link] as plt
[Link](x_train,y_train)
[Link](x_train,y_pred,color="red")
[Link]("petal length")
[Link]("petal width")
[60]: Text(0, 0.5, 'petal width')
4
[61]: y_pred_test=[Link](x_test)
y_pred_test
[61]: array([1.89983231, 2.14993954, 1.35793332, 0.27413533, 1.73309416,
1.69140962, 0.3575044 , 1.94151685, 0.3575044 , 1.14951063,
1.60804055, 0.31581987, 2.108255 , 0.27413533, 0.27413533,
1.7747787 , 1.52467147, 1.60804055, 2.19162408, 0.23245079,
1.85814777, 0.23245079, 0.31581987, 0.19076625, 1.98320139,
0.23245079, 0.44087348, 1.64972508, 1.48298693, 1.27456424,
0.27413533, 1.27456424, 0.19076625, 2.44173131, 0.27413533,
0.3575044 , 1.56635601, 1.02445701, 1.39961786, 2.14993954,
2.02488593, 0.44087348, 1.19119517, 0.23245079, 1.48298693,
1.73309416, 1.52467147, 2.31667769, 0.27413533, 1.35793332,
2.19162408, 1.89983231, 0.23245079, 1.98320139, 1.52467147,
1.60804055, 2.44173131, 1.39961786, 0.23245079, 1.7747787 ])
[62]: import [Link] as plt
[Link](x_test,y_test)
[Link](x_test,y_pred_test,color="red")
[Link]("petal length")
[Link]("petal width")
5
[62]: Text(0, 0.5, 'petal width')
[ ]: