# p r 4   l o g i s t i c   r e g r e s s i o n 
 
 
 
 
 
 i m p o r t   p a n d a s   a s   p d 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   s k l e a r n . m o d e l _ s e l e c t i o n   i m p o r t   t r a i n _ t e s t _ s p l i t 
 
 f r o m   s k l e a r n . p r e p r o c e s s i n g   i m p o r t   S t a n d a r d S c a l e r 
 
 f r o m   s k l e a r n . l i n e a r _ m o d e l   i m p o r t   L o g i s t i c R e g r e s s i o n 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   a c c u r a c y _ s c o r e ,   c o n f u s i o n _ m a t r i x ,   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   s e a b o r n   a s   s n s 
 
 i m p o r t   m a t p l o t l i b . p y p l o t   a s   p l t 
 
 
 
 d a t a   =   p d . r e a d _ c s v ( " B a n k _ P e r s o n a l _ L o a n _ M o d e l l i n g . c s v " ) 
 
 p r i n t ( " '  D a t a   L o a d e d   S u c c e s s f u l l y ! \ n " ) 
 
 p r i n t ( d a t a . h e a d ( ) ) 
 
 
 
 
 
 
 
 p r i n t ( " \ n M i s s i n g   V a l u e s : \ n " ,   d a t a . i s n u l l ( ) . s u m ( ) ) 
 
 
 
 
 
 d a t a   =   d a t a . d r o p ( [ ' I D ' ] ,   a x i s = 1 ) 
 
 d a t a   =   d a t a . d r o p ( [ ' Z I P   C o d e ' ] ,   a x i s = 1 ) 
 
 d a t a   =   d a t a . d r o p ( [ ' C C A v g ' ] ,   a x i s = 1 ) 
 
 
 
 
 
 X   =   d a t a . d r o p ( [ ' P e r s o n a l   L o a n ' ] ,   a x i s = 1 )     
 
 y   =   d a t a [ ' P e r s o n a l   L o a n ' ]       
 
 
 
 
 
 s c a l e r   =   S t a n d a r d S c a l e r ( ) 
 
 X _ s c a l e d   =   s c a l e r . f i t _ t r a n s f o r m ( X ) 
 
 p r i n t ( X _ s c a l e d ) 
 
 
 
 
 
 X _ t r a i n ,   X _ t e s t ,   y _ t r a i n ,   y _ t e s t   =   t r a i n _ t e s t _ s p l i t ( 
 
         X _ s c a l e d ,   y ,   t e s t _ s i z e = 0 . 2 5 ,   r a n d o m _ s t a t e = 4 2 
 
 ) 
 
 
 
 m o d e l   =   L o g i s t i c R e g r e s s i o n ( m a x _ i t e r = 1 0 0 0 ) 
 
 m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
 
 
 y _ p r e d   =   m o d e l . p r e d i c t ( X _ t e s t ) 
 
 
 
 
 
 p r i n t ( " <د  A c c u r a c y : " ,   r o u n d ( a c c u r a c y _ s c o r e ( y _ t e s t ,   y _ p r e d )   *   1 0 0 ,   2 ) ,   " % " ) 
 
 p r i n t ( " \ n =  C o n f u s i o n   M a t r i x : \ n " ,   c o n f u s i o n _ m a t r i x ( y _ t e s t ,   y _ p r e d ) ) 
 
 p r i n t ( " \ n >  C l a s s i f i c a t i o n   R e p o r t : \ n " ,   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   y _ p r e d ) ) 
 
 
 
 s n s . h e a t m a p ( c o n f u s i o n _ m a t r i x ( y _ t e s t ,   y _ p r e d ) ,   a n n o t = T r u e ,   f m t = ' d ' ,   c m a p = ' B l u e s ' ) 
 
 p l t . t i t l e ( " C o n f u s i o n   M a t r i x   -   L o g i s t i c   R e g r e s s i o n " ) 
 
 p l t . x l a b e l ( " P r e d i c t e d " ) 
 
 p l t . y l a b e l ( " A c t u a l " ) 
 
 p l t . s h o w ( ) 