Part_5 Stroke_Prediction – Tensorflow

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt


df= pd.read_csv('c:/1/Stroke_Prediction_NUM.csv')
df.head(2)
Out[1]:
Unnamed: 0 ID Gender Hypertension Heart_Disease Ever_Married Type_Of_Work Residence Avg_Glucose BMI Smoking_Status Stroke Age_years Age_years_10 Gender_C Ever_Married_C Type_Of_Work_C Residence_C Smoking_Status_C Age_years_10_C
0 0 30650 Male 1 0 Yes Private Urban 87.96 39.2 never smoked 0 58.093151 (53.126, 59.076] 1 1 2 1 1 5
1 1 57008 Female 0 0 Yes Private Rural 69.04 35.9 formerly smoked 0 70.076712 (65.121, 74.11] 0 1 2 0 0 7

Analiza poziomu zbilansowania zmiennej wynikowej

In [2]:
del df['Unnamed: 0']
df.Stroke.value_counts(dropna = False, normalize=True)
Out[2]:
0    0.981144
1    0.018856
Name: Stroke, dtype: float64

Zbiór zmiennych wynikowych wymaga zbilansowania. Do bilansowania zmiennych wynikowych stosuję trzy metody:

Wszystkie trzy metody powinny dać podobne efekty przy klasyfikacji. Dzisiaj do zbilansowania zbioru zastosuje metodę oversampling. Oversampling odbywa się na zbiorze treningowym, więc najpierw trzeba podzielić zbiór na treningowy i testowy.

Podział na zbiór testowy i wynikowy

In [3]:
df2 = df[['Hypertension','Heart_Disease','Avg_Glucose','BMI','Stroke','Age_years','Gender_C','Ever_Married_C','Type_Of_Work_C','Residence_C','Smoking_Status_C','Age_years_10_C']]
In [4]:
y = df2['Stroke']
X = df2.drop('Stroke', axis=1) 
In [5]:
from sklearn.model_selection import train_test_split 
Xtrain, Xtest, ytrain, ytest = train_test_split(X,y, test_size=0.33, stratify = y, random_state = 148)

print ('Zbiór X treningowy: ',Xtrain.shape)
print ('Zbiór X testowy:    ', Xtest.shape)
print ('Zbiór y treningowy: ', ytrain.shape)
print ('Zbiór y testowy:    ', ytest.shape)
Zbiór X treningowy:  (19471, 11)
Zbiór X testowy:     (9591, 11)
Zbiór y treningowy:  (19471,)
Zbiór y testowy:     (9591,)
In [6]:
print("ytrain = 0: ", sum(ytrain == 0))
print("ytrain = 1: ", sum(ytrain == 1))
ytrain = 0:  19104
ytrain = 1:  367
In [7]:
Proporcja = sum(ytrain == 0) / sum(ytrain == 1) 
Proporcja = np.round(Proporcja, decimals=0)
Proporcja = Proporcja.astype(int)
print('Ilość 0 Stroke na 1 Stroke: ', Proporcja)
Ilość 0 Stroke na 1 Stroke:  52
In [8]:
ytrain_OVSA = pd.concat([ytrain[ytrain==1]] * Proporcja, axis = 0) 
ytrain_OVSA.count()
Out[8]:
19084

Powiękrzyliśmy ilość zmiennych wynikowych 1. Teraz mamy tę samą liczbę wierszy zmiennych wynikowych i zmiennych niezależnych. Teraz wprowadzamy nowe, dodatkowe zmienne 1 do zbioru treningowego.

In [9]:
Xtrain_OVSA = pd.concat([Xtrain.loc[ytrain==1, :]] * Proporcja, axis = 0)
ytrain_OVSA.count()
Out[9]:
19084
In [10]:
ytrain_OVSA = pd.concat([ytrain, ytrain_OVSA], axis = 0).reset_index(drop = True)
Xtrain_OVSA = pd.concat([Xtrain, Xtrain_OVSA], axis = 0).reset_index(drop = True)

print("ilość elementów w zbiorze Xtrain:     ", Xtrain.BMI.count())
print("ilość elementów w zbiorze Xtrain_OVSA: ", Xtrain_OVSA.BMI.count())
print("ilość elementów w zbiorze ytrain:     ", ytrain.count())
print("ilość elementów w zbiorze ytrain_OVSA: ", ytrain_OVSA.count())
ilość elementów w zbiorze Xtrain:      19471
ilość elementów w zbiorze Xtrain_OVSA:  38555
ilość elementów w zbiorze ytrain:      19471
ilość elementów w zbiorze ytrain_OVSA:  38555

Poziom zbilansowania zbioru wynikowego:

In [11]:
ytrain_OVSA.value_counts(dropna = False, normalize=True).plot(kind='pie')
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x1898e757438>

Oversampling dla zbioru testowego

In [12]:
print("ytest = 0: ", sum(ytest == 0))
print("ytest = 1: ", sum(ytest == 1))
ytest = 0:  9410
ytest = 1:  181
In [13]:
ProporcjaT = sum(ytrain == 0) / sum(ytrain == 1) 
ProporcjaT = np.round(ProporcjaT, decimals=0)
ProporcjaT = Proporcja.astype(int)
print('Ilość 0 Stroke na 1 Stroke: ', ProporcjaT)
Ilość 0 Stroke na 1 Stroke:  52
In [14]:
ytest_OVSA = pd.concat([ytest[ytest==1]] * ProporcjaT, axis = 0) 
ytest_OVSA.count()
Out[14]:
9412
In [15]:
Xtest_OVSA = pd.concat([Xtest.loc[ytest==1, :]] * ProporcjaT, axis = 0)
ytest_OVSA.count()
Out[15]:
9412
In [16]:
ytest_OVSA = pd.concat([ytest, ytest_OVSA], axis = 0).reset_index(drop = True)
Xtest_OVSA = pd.concat([Xtest, Xtest_OVSA], axis = 0).reset_index(drop = True)

print("ilość elementów w zbiorze Xtrain:     ", Xtest.BMI.count())
print("ilość elementów w zbiorze Xtrain_OVSA: ", Xtest_OVSA.BMI.count())
print("ilość elementów w zbiorze ytrain:     ", ytest.count())
print("ilość elementów w zbiorze ytrain_OVSA: ", ytest_OVSA.count())
ilość elementów w zbiorze Xtrain:      9591
ilość elementów w zbiorze Xtrain_OVSA:  19003
ilość elementów w zbiorze ytrain:      9591
ilość elementów w zbiorze ytrain_OVSA:  19003

Tensorflow neural network

In [17]:
import tensorflow as tf
import matplotlib.pyplot as plt

C:ProgramDataAnaconda3envsOLD_TFlibsite-packagestensorflowpythonframeworkdtypes.py:493: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:ProgramDataAnaconda3envsOLD_TFlibsite-packagestensorflowpythonframeworkdtypes.py:494: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:ProgramDataAnaconda3envsOLD_TFlibsite-packagestensorflowpythonframeworkdtypes.py:495: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:ProgramDataAnaconda3envsOLD_TFlibsite-packagestensorflowpythonframeworkdtypes.py:496: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:ProgramDataAnaconda3envsOLD_TFlibsite-packagestensorflowpythonframeworkdtypes.py:497: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:ProgramDataAnaconda3envsOLD_TFlibsite-packagestensorflowpythonframeworkdtypes.py:502: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
In [18]:
print("Using tensorflow Version 
Using tensorflow Version 1.5.0
In [19]:
df2.shape
Out[19]:
(29062, 12)
In [20]:
feat_column = tf.contrib.layers.real_valued_column('features', dimension=12)

Definiowanie estymatora

In [21]:
estimator = tf.estimator.LinearClassifier(feature_columns=[feat_column],
                                          n_classes=2,
                                          model_dir = "kernel_e"
                                         )
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': 'kernel_e', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x000001899EA56278>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

Zmiana wszystkich zmiennych numerycznych na zmienne TensorFlow

In [22]:
df2.columns
Out[22]:
Index(['Hypertension', 'Heart_Disease', 'Avg_Glucose', 'BMI', 'Stroke',
       'Age_years', 'Gender_C', 'Ever_Married_C', 'Type_Of_Work_C',
       'Residence_C', 'Smoking_Status_C', 'Age_years_10_C'],
      dtype='object')
In [23]:
FEATURES  = ['Hypertension', 'Heart_Disease', 'Avg_Glucose', 'BMI', 'Stroke',
       'Age_years', 'Gender_C', 'Ever_Married_C', 'Type_Of_Work_C',
       'Residence_C', 'Smoking_Status_C', 'Age_years_10_C']

continuous_features = [tf.feature_column.numeric_column(k) for k in FEATURES]
continuous_features
Out[23]:
[_NumericColumn(key='Hypertension', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Heart_Disease', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Avg_Glucose', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='BMI', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Stroke', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Age_years', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Gender_C', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Ever_Married_C', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Type_Of_Work_C', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Residence_C', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Smoking_Status_C', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 _NumericColumn(key='Age_years_10_C', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)]

Definiuje klasyfikator liniowy

In [24]:
model = tf.estimator.LinearClassifier(
    n_classes = 2,
    model_dir="ongoing/ATOS7", 
    feature_columns=continuous_features)
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': 'ongoing/ATOS7', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x000001899CBC0400>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

Tworzę funkcję wprowadzania

In [25]:
FEATURES = ['Hypertension', 'Heart_Disease', 'Avg_Glucose', 'BMI', 'Stroke',
       'Age_years', 'Gender_C', 'Ever_Married_C', 'Type_Of_Work_C',
       'Residence_C', 'Smoking_Status_C', 'Age_years_10_C']
LABEL = 'Stroke'

def get_input_fn(data_set, num_epochs=None, n_batch = 128, shuffle=True):
    return tf.estimator.inputs.pandas_input_fn(
       x=pd.DataFrame({k: data_set[k].values for k in FEATURES}),
       y = pd.Series(data_set[LABEL].values),
       batch_size=n_batch,   
       num_epochs=num_epochs,
       shuffle=shuffle)

Trenowanie modelu

In [26]:
df_train = pd.concat([Xtrain_OVSA, ytrain_OVSA], axis=1, sort=False) 
df_test = pd.concat([Xtest_OVSA, ytest_OVSA], axis=1, sort=False) 
In [27]:
model.train(input_fn=get_input_fn(df_train, 
                                num_epochs=None,
                                n_batch = 128,
                                shuffle=False),
                                steps=10000)
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from ongoing/ATOS7model.ckpt-41000
INFO:tensorflow:Saving checkpoints for 41001 into ongoing/ATOS7model.ckpt.
INFO:tensorflow:loss = 0.09737757, step = 41001
INFO:tensorflow:global_step/sec: 259.654
INFO:tensorflow:loss = 0.045715436, step = 41101 (0.388 sec)
INFO:tensorflow:global_step/sec: 299.305
INFO:tensorflow:loss = 0.08216881, step = 41201 (0.335 sec)
INFO:tensorflow:global_step/sec: 294.039
INFO:tensorflow:loss = 0.033824317, step = 41301 (0.341 sec)
INFO:tensorflow:global_step/sec: 264.722
INFO:tensorflow:loss = 0.06005334, step = 41401 (0.382 sec)
INFO:tensorflow:global_step/sec: 261.798
INFO:tensorflow:loss = 0.04598384, step = 41501 (0.380 sec)
INFO:tensorflow:global_step/sec: 279.258
INFO:tensorflow:loss = 0.019679055, step = 41601 (0.358 sec)
INFO:tensorflow:global_step/sec: 179.451
INFO:tensorflow:loss = 0.07859419, step = 41701 (0.559 sec)
INFO:tensorflow:global_step/sec: 156.504
INFO:tensorflow:loss = 0.08064161, step = 41801 (0.635 sec)
INFO:tensorflow:global_step/sec: 277.75
INFO:tensorflow:loss = 0.034247227, step = 41901 (0.360 sec)
INFO:tensorflow:global_step/sec: 261.794
INFO:tensorflow:loss = 0.077103004, step = 42001 (0.383 sec)
INFO:tensorflow:global_step/sec: 201.328
INFO:tensorflow:loss = 0.064075656, step = 42101 (0.504 sec)
INFO:tensorflow:global_step/sec: 205.466
INFO:tensorflow:loss = 0.019486569, step = 42201 (0.479 sec)
INFO:tensorflow:global_step/sec: 286.435
INFO:tensorflow:loss = 0.08544526, step = 42301 (0.350 sec)
INFO:tensorflow:global_step/sec: 285.663
INFO:tensorflow:loss = 0.058666773, step = 42401 (0.350 sec)
INFO:tensorflow:global_step/sec: 298.415
INFO:tensorflow:loss = 0.028867826, step = 42501 (0.334 sec)
INFO:tensorflow:global_step/sec: 258.599
INFO:tensorflow:loss = 0.07046058, step = 42601 (0.387 sec)
INFO:tensorflow:global_step/sec: 267.38
INFO:tensorflow:loss = 0.0941832, step = 42701 (0.378 sec)
INFO:tensorflow:global_step/sec: 265.419
INFO:tensorflow:loss = 0.024095044, step = 42801 (0.372 sec)
INFO:tensorflow:global_step/sec: 151.92
INFO:tensorflow:loss = 0.06935177, step = 42901 (0.669 sec)
INFO:tensorflow:global_step/sec: 203.366
INFO:tensorflow:loss = 0.0517662, step = 43001 (0.482 sec)
INFO:tensorflow:global_step/sec: 166.004
INFO:tensorflow:loss = 0.02210995, step = 43101 (0.608 sec)
INFO:tensorflow:global_step/sec: 185.903
INFO:tensorflow:loss = 0.06902549, step = 43201 (0.540 sec)
INFO:tensorflow:global_step/sec: 154.076
INFO:tensorflow:loss = 0.094448306, step = 43301 (0.646 sec)
INFO:tensorflow:global_step/sec: 248.803
INFO:tensorflow:loss = 0.036873564, step = 43401 (0.398 sec)
INFO:tensorflow:global_step/sec: 286.478
INFO:tensorflow:loss = 0.064160794, step = 43501 (0.348 sec)
INFO:tensorflow:global_step/sec: 277.134
INFO:tensorflow:loss = 0.057811715, step = 43601 (0.360 sec)
INFO:tensorflow:global_step/sec: 276.219
INFO:tensorflow:loss = 0.018571634, step = 43701 (0.363 sec)
INFO:tensorflow:global_step/sec: 293.18
INFO:tensorflow:loss = 0.082472146, step = 43801 (0.340 sec)
INFO:tensorflow:global_step/sec: 287.599
INFO:tensorflow:loss = 0.07906988, step = 43901 (0.348 sec)
INFO:tensorflow:global_step/sec: 291.474
INFO:tensorflow:loss = 0.03498091, step = 44001 (0.344 sec)
INFO:tensorflow:global_step/sec: 291.475
INFO:tensorflow:loss = 0.07337621, step = 44101 (0.343 sec)
INFO:tensorflow:global_step/sec: 297.663
INFO:tensorflow:loss = 0.073616184, step = 44201 (0.338 sec)
INFO:tensorflow:global_step/sec: 302.016
INFO:tensorflow:loss = 0.02367942, step = 44301 (0.331 sec)
INFO:tensorflow:global_step/sec: 288.955
INFO:tensorflow:loss = 0.06778104, step = 44401 (0.343 sec)
INFO:tensorflow:global_step/sec: 292.578
INFO:tensorflow:loss = 0.06666125, step = 44501 (0.345 sec)
INFO:tensorflow:global_step/sec: 262.48
INFO:tensorflow:loss = 0.026955485, step = 44601 (0.381 sec)
INFO:tensorflow:global_step/sec: 273.954
INFO:tensorflow:loss = 0.06536393, step = 44701 (0.363 sec)
INFO:tensorflow:global_step/sec: 270.627
INFO:tensorflow:loss = 0.11598677, step = 44801 (0.374 sec)
INFO:tensorflow:global_step/sec: 176.839
INFO:tensorflow:loss = 0.028442333, step = 44901 (0.567 sec)
INFO:tensorflow:global_step/sec: 254.504
INFO:tensorflow:loss = 0.062104367, step = 45001 (0.386 sec)
INFO:tensorflow:global_step/sec: 263.168
INFO:tensorflow:loss = 0.05752158, step = 45101 (0.380 sec)
INFO:tensorflow:global_step/sec: 279.55
INFO:tensorflow:loss = 0.020415168, step = 45201 (0.359 sec)
INFO:tensorflow:global_step/sec: 296.649
INFO:tensorflow:loss = 0.07284053, step = 45301 (0.339 sec)
INFO:tensorflow:global_step/sec: 296.648
INFO:tensorflow:loss = 0.10609268, step = 45401 (0.337 sec)
INFO:tensorflow:global_step/sec: 289.163
INFO:tensorflow:loss = 0.03753686, step = 45501 (0.346 sec)
INFO:tensorflow:global_step/sec: 242.183
INFO:tensorflow:loss = 0.07539731, step = 45601 (0.411 sec)
INFO:tensorflow:global_step/sec: 182.723
INFO:tensorflow:loss = 0.0807307, step = 45701 (0.546 sec)
INFO:tensorflow:global_step/sec: 274.707
INFO:tensorflow:loss = 0.018085375, step = 45801 (0.365 sec)
INFO:tensorflow:global_step/sec: 286.478
INFO:tensorflow:loss = 0.08718066, step = 45901 (0.348 sec)
INFO:tensorflow:global_step/sec: 299.305
INFO:tensorflow:loss = 0.088378854, step = 46001 (0.334 sec)
INFO:tensorflow:global_step/sec: 294.039
INFO:tensorflow:loss = 0.036748014, step = 46101 (0.340 sec)
INFO:tensorflow:global_step/sec: 283.242
INFO:tensorflow:loss = 0.07226403, step = 46201 (0.355 sec)
INFO:tensorflow:global_step/sec: 273.208
INFO:tensorflow:loss = 0.105186135, step = 46301 (0.366 sec)
INFO:tensorflow:global_step/sec: 277.749
INFO:tensorflow:loss = 0.024719864, step = 46401 (0.358 sec)
INFO:tensorflow:global_step/sec: 288.123
INFO:tensorflow:loss = 0.08536232, step = 46501 (0.347 sec)
INFO:tensorflow:global_step/sec: 306.63
INFO:tensorflow:loss = 0.07138116, step = 46601 (0.328 sec)
INFO:tensorflow:global_step/sec: 271.728
INFO:tensorflow:loss = 0.024890233, step = 46701 (0.371 sec)
INFO:tensorflow:global_step/sec: 194.694
INFO:tensorflow:loss = 0.08181512, step = 46801 (0.510 sec)
INFO:tensorflow:global_step/sec: 259.761
INFO:tensorflow:loss = 0.13006693, step = 46901 (0.387 sec)
INFO:tensorflow:global_step/sec: 292.324
INFO:tensorflow:loss = 0.03466392, step = 47001 (0.340 sec)
INFO:tensorflow:global_step/sec: 292.324
INFO:tensorflow:loss = 0.076379016, step = 47101 (0.343 sec)
INFO:tensorflow:global_step/sec: 281.65
INFO:tensorflow:loss = 0.066052586, step = 47201 (0.354 sec)
INFO:tensorflow:global_step/sec: 195.451
INFO:tensorflow:loss = 0.021731038, step = 47301 (0.522 sec)
INFO:tensorflow:global_step/sec: 212.884
INFO:tensorflow:loss = 0.07308343, step = 47401 (0.461 sec)
INFO:tensorflow:global_step/sec: 294.905
INFO:tensorflow:loss = 0.13414271, step = 47501 (0.337 sec)
INFO:tensorflow:global_step/sec: 284.044
INFO:tensorflow:loss = 0.036732923, step = 47601 (0.353 sec)
INFO:tensorflow:global_step/sec: 273.955
INFO:tensorflow:loss = 0.0743381, step = 47701 (0.365 sec)
INFO:tensorflow:global_step/sec: 253.841
INFO:tensorflow:loss = 0.096731044, step = 47801 (0.395 sec)
INFO:tensorflow:global_step/sec: 182.97
INFO:tensorflow:loss = 0.020686347, step = 47901 (0.571 sec)
INFO:tensorflow:global_step/sec: 169.37
INFO:tensorflow:loss = 0.07160351, step = 48001 (0.564 sec)
INFO:tensorflow:global_step/sec: 263.861
INFO:tensorflow:loss = 0.09029151, step = 48101 (0.380 sec)
INFO:tensorflow:global_step/sec: 144.685
INFO:tensorflow:loss = 0.033549923, step = 48201 (0.709 sec)
INFO:tensorflow:global_step/sec: 177.466
INFO:tensorflow:loss = 0.06933582, step = 48301 (0.545 sec)
INFO:tensorflow:global_step/sec: 192.821
INFO:tensorflow:loss = 0.14186579, step = 48401 (0.523 sec)
INFO:tensorflow:global_step/sec: 189.539
INFO:tensorflow:loss = 0.026808614, step = 48501 (0.525 sec)
INFO:tensorflow:global_step/sec: 201.75
INFO:tensorflow:loss = 0.0859948, step = 48601 (0.497 sec)
INFO:tensorflow:global_step/sec: 196.989
INFO:tensorflow:loss = 0.08885132, step = 48701 (0.506 sec)
INFO:tensorflow:global_step/sec: 202.971
INFO:tensorflow:loss = 0.024762977, step = 48801 (0.494 sec)
INFO:tensorflow:global_step/sec: 192.821
INFO:tensorflow:loss = 0.08615236, step = 48901 (0.519 sec)
INFO:tensorflow:global_step/sec: 282.444
INFO:tensorflow:loss = 0.14492016, step = 49001 (0.353 sec)
INFO:tensorflow:global_step/sec: 304.765
INFO:tensorflow:loss = 0.039036565, step = 49101 (0.330 sec)
INFO:tensorflow:global_step/sec: 133.512
INFO:tensorflow:loss = 0.08584847, step = 49201 (0.748 sec)
INFO:tensorflow:global_step/sec: 194.317
INFO:tensorflow:loss = 0.08690578, step = 49301 (0.514 sec)
INFO:tensorflow:global_step/sec: 294.904
INFO:tensorflow:loss = 0.021082029, step = 49401 (0.342 sec)
INFO:tensorflow:global_step/sec: 294.035
INFO:tensorflow:loss = 0.07428626, step = 49501 (0.336 sec)
INFO:tensorflow:global_step/sec: 302.928
INFO:tensorflow:loss = 0.14064331, step = 49601 (0.331 sec)
INFO:tensorflow:global_step/sec: 291.475
INFO:tensorflow:loss = 0.03935333, step = 49701 (0.347 sec)
INFO:tensorflow:global_step/sec: 294.039
INFO:tensorflow:loss = 0.09069357, step = 49801 (0.336 sec)
INFO:tensorflow:global_step/sec: 130.727
INFO:tensorflow:loss = 0.1264919, step = 49901 (0.776 sec)
INFO:tensorflow:global_step/sec: 186.37
INFO:tensorflow:loss = 0.023504142, step = 50001 (0.526 sec)
INFO:tensorflow:global_step/sec: 200.528
INFO:tensorflow:loss = 0.096213445, step = 50101 (0.499 sec)
INFO:tensorflow:global_step/sec: 201.752
INFO:tensorflow:loss = 0.11533563, step = 50201 (0.497 sec)
INFO:tensorflow:global_step/sec: 202.559
INFO:tensorflow:loss = 0.032153483, step = 50301 (0.493 sec)
INFO:tensorflow:global_step/sec: 205.468
INFO:tensorflow:loss = 0.09044957, step = 50401 (0.488 sec)
INFO:tensorflow:global_step/sec: 284.851
INFO:tensorflow:loss = 0.20495865, step = 50501 (0.352 sec)
INFO:tensorflow:global_step/sec: 292.326
INFO:tensorflow:loss = 0.028214257, step = 50601 (0.340 sec)
INFO:tensorflow:global_step/sec: 301.103
INFO:tensorflow:loss = 0.08116261, step = 50701 (0.331 sec)
INFO:tensorflow:global_step/sec: 307.568
INFO:tensorflow:loss = 0.10606042, step = 50801 (0.329 sec)
INFO:tensorflow:global_step/sec: 295.773
INFO:tensorflow:loss = 0.024945365, step = 50901 (0.335 sec)
INFO:tensorflow:Saving checkpoints for 51000 into ongoing/ATOS7model.ckpt.
INFO:tensorflow:Loss for final step: 0.07768929.
Out[27]:
<tensorflow.python.estimator.canned.linear.LinearClassifier at 0x189995d8208>

Ocena modelu

In [28]:
model.evaluate(input_fn=get_input_fn(df_test, 
                                      num_epochs=1,
                                      n_batch = 128,
                                      shuffle=False),
                                      steps=1000)
INFO:tensorflow:Starting evaluation at 2020-03-03-10:49:24
INFO:tensorflow:Restoring parameters from ongoing/ATOS7model.ckpt-51000
INFO:tensorflow:Evaluation [100/1000]
INFO:tensorflow:Finished evaluation at 2020-03-03-10:49:25
INFO:tensorflow:Saving dict for global step 51000: accuracy = 1.0, accuracy_baseline = 0.50481504, auc = 1.0, auc_precision_recall = 1.0, average_loss = 0.0007520476, global_step = 51000, label/mean = 0.50481504, loss = 0.09591383, prediction/mean = 0.5047147
Out[28]:
{'accuracy': 1.0,
 'accuracy_baseline': 0.50481504,
 'auc': 1.0,
 'auc_precision_recall': 1.0,
 'average_loss': 0.0007520476,
 'label/mean': 0.50481504,
 'loss': 0.09591383,
 'prediction/mean': 0.5047147,
 'global_step': 51000}
In [29]:
y = model.predict(    
         input_fn=get_input_fn(df_test,                          
         num_epochs=1,                          
         n_batch = 256,                          
         shuffle=False))
In [30]:
y
Out[30]:
<generator object Estimator.predict at 0x000001899EFF25C8>

Pobranie modelu z grafu i klasyfikacja na danych testowych

In [31]:
import time
start_time = time.time() ## pomiar czasu: start pomiaru czasu
print(time.ctime())
Tue Mar  3 11:49:25 2020
In [ ]:
with tf.Session() as sess:
    saver = tf.train.import_meta_graph('ongoing/ATOS7')
    print("Model found.")

    saver.restore(sess, tf.train.latest_checkpoint('./'))
    print("Model restored compl.")

    #z = tf.placeholder(tf.float32, shape= (None,38555))

    y_pred= y_pred.as_matrix()
    
    output =sess.run(y,feed_dict={Xtest_OVSA: y_pred})

    #output =sess.run(z,feed_dict={Xtest_OVSA: y_pred})
    #print(output)
In [ ]:

print(’Pomiar czasu wykonania tego zadania’) print(time.time() start_time) ## koniec pomiaru czasu

Part_6 Stroke_Prediction_Pytorch