64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
# Common imports
|
|
import os
|
|
import numpy as np
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.utils import resample
|
|
from sklearn.metrics import mean_squared_error
|
|
from IPython.display import display
|
|
from pylab import plt, mpl
|
|
plt.style.use('seaborn')
|
|
mpl.rcParams['font.family'] = 'serif'
|
|
|
|
# Where to save the figures and data files
|
|
PROJECT_ROOT_DIR = "Results"
|
|
FIGURE_ID = "Results/FigureFiles"
|
|
DATA_ID = "DataFiles/"
|
|
|
|
if not os.path.exists(PROJECT_ROOT_DIR):
|
|
os.mkdir(PROJECT_ROOT_DIR)
|
|
|
|
if not os.path.exists(FIGURE_ID):
|
|
os.makedirs(FIGURE_ID)
|
|
|
|
if not os.path.exists(DATA_ID):
|
|
os.makedirs(DATA_ID)
|
|
|
|
def image_path(fig_id):
|
|
return os.path.join(FIGURE_ID, fig_id)
|
|
|
|
def data_path(dat_id):
|
|
return os.path.join(DATA_ID, dat_id)
|
|
|
|
def save_fig(fig_id):
|
|
plt.savefig(image_path(fig_id) + ".png", format='png')
|
|
|
|
infile = open(data_path("chddata.csv"),'r')
|
|
|
|
# Read the chd data as csv file and organize the data into arrays with age group, age, and chd
|
|
chd = pd.read_csv(infile, names=('ID', 'Age', 'Agegroup', 'CHD'))
|
|
chd.columns = ['ID', 'Age', 'Agegroup', 'CHD']
|
|
output = chd['CHD']
|
|
age = chd['Age']
|
|
agegroup = chd['Agegroup']
|
|
numberID = chd['ID']
|
|
display(chd)
|
|
|
|
plt.scatter(age, output, marker='o')
|
|
plt.axis([18,70.0,-0.1, 1.2])
|
|
plt.xlabel(r'Age')
|
|
plt.ylabel(r'CHD')
|
|
plt.title(r'Age distribution and Coronary heart disease')
|
|
plt.show()
|
|
|
|
agegroupmean = np.array([0.1, 0.133, 0.250, 0.333, 0.462, 0.625, 0.765, 0.800])
|
|
group = np.array([1, 2, 3, 4, 5, 6, 7, 8])
|
|
plt.plot(group, agegroupmean, "r-")
|
|
plt.axis([0,9,0, 1.0])
|
|
plt.xlabel(r'Age group')
|
|
plt.ylabel(r'CHD mean values')
|
|
plt.title(r'Mean values for each age group')
|
|
plt.show()
|