Page 131 - Artificial Intellegence_v2.0_Class_12
P. 131
# Sp lit the d ata into tr aining and testing sets
y
X _ tr ain, X _ test, y _ tr ain, y _ test = tr ain_ test_ sp lit( X , , test_ siz e= 0 .2 , r and om_ state= 4 2 )
# Create and fit the linear regression model
mod el = L inear Regr ession( )
model.fit(X_train, y_train)
# M ak e p r ed ictions on the test set
y _ p r ed = mod el.p r ed ict( X _ test)
# E v alu ate the mod el
mse = mean_ sq u ar ed _ er r or ( y _ test, y _ p r ed )
p r int( " M ean Sq u ar ed E r r or : " , mse)
# P r ed ict cr op y ield w hen amou nt of f er tiliz er ( x ) is 2 5
x _ new = np .ar r ay ( [ [ 2 5 ] ] )
y _ new = mod el.p r ed ict( x _ new )
p r int( " P r ed icted cr op y ield f or x = 2 5 : " , y _ new )
Output
ean quared rror .
redicted crop yield for .
o check the e ecution of the above code, click on the follo ing link
https colab.research.google.com drive p m p g m d o m tqa usp sharing
C r eating an A I Model using L og istic R eg r ession in Python
f r om p and as imp or t r ead _ csv
f r om sk lear n.mod el_ selection imp or t tr ain_ test_ sp lit
f r om sk lear n.linear _ mod el imp or t L ogisticRegr ession
# load d ataset
d ata= " http s: //r aw .githu b u ser content.com/j b r ow nlee/D atasets/master /ir is.csv "
lab els= [ ' sep al- length' , ' sep al- w id th' , ' p etal- length' , ' p etal- w id th' , ' class' ]
d ataset= r ead _ csv ( d ata, names= lab els)
(
p r int d ataset.shap e)
(
p r int d ataset.head ( 2 0 ) )
p r int d ataset.d escr ib e( ) )
(
Output
( 1 5 0 , 5 )
sep al- length sep al- w id th p etal- length p etal- w id th class
0 5 .1 3 .5 1 .4 0 .2 I r is- setosa
1 4 .9 3 .0 1 .4 0 .2 I r is- setosa
2 4 .7 3 .2 1 .3 0 .2 I r is- setosa
3 4 .6 3 .1 1 .5 0 .2 I r is- setosa
C apstone P roj e ct

