-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathML_X_data_1.py
150 lines (107 loc) · 3.94 KB
/
ML_X_data_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 24 11:04:24 2019
@author: kendalljohnson
"""
# Linear Regression of the IBI
print('Linear Regression of the IBI')
# Imports :: modules used to create code
import pandas as pd # DataFrame Tool
import numpy as np # Basically a great calculator for now
import matplotlib.pyplot as plt # For 2-D Graphing
from sklearn import linear_model # For our Linear Regression Model // Sklearn is a M.L. modules
#import scipy.optimize as so # Basically a great calculator for now
from sklearn.model_selection import train_test_split # Label Encoder
#from sklearn.cross_validation import train_test_split
# Data (real/training data) :: physical numbers we are analysing
df = pd.read_excel('X_data.xlsx')
cnt = df.values[:,1]
HR = df.values[:,2] # Heart Rate in beats per minute
IBI = df.values[:,3] # IBI is a varible giving from hearts beat anaylsis that is a great indicator of a person's stimulus
Pat_size = HR.size
# DataFrames :: Simply will show all basic data on your set of data for example the mean, median, standard dev
HR_df = pd.DataFrame(HR)
IBI_df = pd.DataFrame(IBI)
# Putting frames together
frames = [HR_df, IBI_df]
Com_df = pd.concat(frames, axis=1)
# Lin Regression Model ::
model = linear_model.LinearRegression()# Linear Reg model
X_train, X_test,y_train, y_test = train_test_split(df[['HR']],df.IBI,test_size=0.1)
model.fit(X_train,y_train)
HH = model.score(X_test,y_test)
X = model.predict(X_test)
#P = model.predict_proba(X_test)
# Varables from real IBI DataFrame for example the mean, median, standard dev
Max = IBI_df.max()
Std = IBI_df.std() # SDNN
Min = IBI_df.min()
Mean = IBI_df.mean()
Sum = IBI_df.sum()
# Important Heart Data and Equations ::
IBI # AVNN
Std = IBI_df.std() # SDNN
Std = Std[0]
preNN50 = []
preNN20 = []
for i in range(Pat_size-1):
A = IBI[i+1]-IBI[i]
preNN50.append(A)
preNN20.append(A)
NN50 = []
for i in range(Pat_size-1):
if np.abs(preNN50[i])>50:
NN50.append(preNN50[i])
pNN50 = (len(NN50))/(len(preNN50))*100
NN20 = []
for i in range(Pat_size-1):
if np.abs(preNN20[i])>20:
NN20.append(preNN20[i])
pNN20 = (len(NN20))/(len(preNN20))*100
if pNN50 > 1 and pNN20 > 1:
print('The relaspe occured')
else:
print('The relaspe has not occured')
print('The SDNN is {}'.format(Std))
print('The NN50 is {}'.format(len(NN50)))
print('The pNN50 is {}'.format(pNN50))
print('The NN20 is {}'.format(len(NN20)))
print('The pNN20 is {}'.format(pNN20))
print('The Model score is {}'.format(HH))
# Predicted data
Pred_IBI = []
for i in range(Pat_size):
C = HR[i]
pred = model.predict([[C]])
pred1 = pred[0]
Pred_IBI.append(pred1)
# For Manual Machine Learning
# the differece between Real and Predicted for Cost function
Diff_IBI = []
for j in range(Pat_size):
D = IBI[j]-Pred_IBI[j]
Diff_IBI.append(D)
# the Cost fucntion root mean square
Cost_IBI = []
for j in range(Pat_size):
D = IBI[j]-Pred_IBI[j]
E = D**2
Cost_IBI.append(E)
Sum_C = sum(Cost_IBI)
Cost = Sum_C / Pat_size
# Optimization curve fit line
def Parab(x, a, b, c):
return (x**2)*a+x*b+c
# Optimization parameters
# Plotting
#print('The Inter_beat Interval is {} miliseconds'.format(A))
plt.scatter(HR,IBI,color = "red",label = 'Training Data') # Plot training Data
plt.title("Linear Reg of Heart rate data") # Plot title
plt.xlabel('Heart Rate in BPM') # Plot x axis name
plt.ylabel('IBI') # Plot y axis label
plt.grid() # Plot grid lines
plt.scatter(HR,Pred_IBI,color = "blue",label = 'Predicted Data') # plot Best fit line
plt.legend() # Plot legend
# Save as CSV
#result.to_csv(r'/Users/kendalljohnson/Desktop/s2019_phys_251_kj/Pat_IBI_1.csv')