1+ import pandas as pd
2+ import numpy as np
3+ import matplotlib .pyplot as plt
4+
5+ np .random .seed (111 )
6+
7+ data = {
8+ 'Age' : np .random .randint (29 , 77 , 300 ),
9+ 'Cholesterol' : np .random .randint (150 , 300 , 300 ),
10+ 'RestingBP' : np .random .randint (90 , 180 , 300 ),
11+ 'MaxHeartRate' : np .random .randint (70 , 190 , 300 ),
12+ 'ChestPainType' : np .random .choice (['Typical' , 'Atypical' , 'Non-Anginal' , 'Asymptomatic' ], 300 ),
13+ 'Disease' : np .random .choice ([0 , 1 ], 300 , p = [0.45 , 0.55 ])
14+ }
15+
16+ df = pd .DataFrame (data )
17+
18+ df ['RiskScore' ] = (df ['Cholesterol' ] * 0.5 ) + (df ['RestingBP' ] * 0.3 ) + (df ['MaxHeartRate' ] * 0.2 )
19+
20+ print ("Average Cholesterol:" , df ['Cholesterol' ].mean ())
21+ print ("Average Resting BP:" , df ['RestingBP' ].mean ())
22+ print ("Average Heart Rate:" , df ['MaxHeartRate' ].mean ())
23+
24+ df ['AgeGroup' ] = pd .cut (df ['Age' ], bins = [29 , 40 , 55 , 80 ], labels = ['Young' , 'Middle' , 'Old' ])
25+
26+ print ("\n Patients per Age Group:" )
27+ print (df ['AgeGroup' ].value_counts ())
28+
29+ plt .figure (figsize = (6 ,4 ))
30+ plt .hist (df ['Age' ], bins = 10 )
31+ plt .title ("Age Distribution" )
32+ plt .xlabel ("Age" )
33+ plt .ylabel ("Count" )
34+ plt .show ()
35+
36+ plt .figure (figsize = (6 ,4 ))
37+ df ['ChestPainType' ].value_counts ().plot (kind = 'bar' )
38+ plt .title ("Chest Pain Type Frequency" )
39+ plt .xlabel ("Type" )
40+ plt .ylabel ("Count" )
41+ plt .show ()
42+
43+ plt .figure (figsize = (6 ,4 ))
44+ df ['Disease' ].value_counts ().plot (kind = 'pie' , autopct = '%1.1f%%' )
45+ plt .title ("Disease Ratio" )
46+ plt .ylabel ("" )
47+ plt .show ()
48+
49+ print ("\n Top 5 High Risk Patients:" )
50+ print (df [['Age' , 'Cholesterol' , 'RestingBP' , 'MaxHeartRate' , 'RiskScore' ]].sort_values ('RiskScore' , ascending = False ).head ())
0 commit comments