forked from harrypotter0/breast-cancer-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute.py
114 lines (89 loc) · 4.34 KB
/
compute.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import pandas as pd
from model import InputForm
from flask import Flask, render_template, request
def compute ( a,b,c,d,e,z,g,h,i):
# Preparing the data
data_file_name = 'breast-cancer-wisconsin.data.txt'
first_line = "id,clump_thickness,unif_cell_size,unif_cell_shape,marg_adhesion,single_epith_cell_size,bare_nuclei,bland_chrom,norm_nucleoli,mitoses,class"
with open(data_file_name, "r+") as f:
content = f.read()
f.seek(0, 0)
f.write(first_line.rstrip('\r\n') + '\n' + content)
df = pd.read_csv(data_file_name)
df.replace('?', np.nan, inplace = True)
df.dropna(inplace=True)
df.drop(['id'], axis = 1, inplace = True)
df['class'].replace('2',0, inplace = True)
df['class'].replace('4',1, inplace = True)
df.to_csv("combined_data.csv", index = False)
# Data sets
CANCER_TRAINING = "cancer_training.csv"
CANCER_TEST = "cancer_test.csv"
######################################
# classifier
# Load datasets
training_set_classifier = tf.contrib.learn.datasets.base.load_csv_with_header(filename=CANCER_TRAINING,
target_dtype=np.int, features_dtype=np.int)
test_set_classifier = tf.contrib.learn.datasets.base.load_csv_with_header(filename=CANCER_TEST,
target_dtype=np.int, features_dtype=np.int)
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=9)]
classifier_classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=2,
model_dir="/tmp/iris_model")
#####################################
# Accuracy
# Load datasets.
training_set_accuracy = tf.contrib.learn.datasets.base.load_csv_with_header(filename=CANCER_TRAINING,
target_dtype=np.int,
features_dtype=np.float32,
target_column=-1)
test_set_accuracy = tf.contrib.learn.datasets.base.load_csv_with_header(filename=CANCER_TEST,
target_dtype=np.int,
features_dtype=np.float32,
target_column=-1)
# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=2)]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier_accuracy = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=2,
model_dir="/tmp/iris_model")
##########################################
#classifier
# Fitting model
classifier_classifier = classifier_classifier.fit(training_set_classifier.data,
training_set_classifier.target,
steps=2000)
k =a
l = b
m =c
n= d
o = e
p = z
q = g
r = h
s = i
def new_samples():
return np.array([[k, l, m, n, o, p, q, r, s],
], dtype=np.float32)
s = list(classifier_classifier.predict(input_fn=new_samples))
# Accuracy
# Fit model.
classifier_accuracy.fit(x=training_set_accuracy.data,
y=training_set_accuracy.target,
steps=2000)
# Evaluate accuracy.
accuracy_score = classifier_accuracy.evaluate(x=test_set_accuracy.data,
y=test_set_accuracy.target)["accuracy"]
if (s == [[1]]):
return ("MALIGNANT",accuracy_score*100)
else:
return ("BENIGN",accuracy_score*100)
if __name__ == '__main__':
print (compute(a,b,c,d,e,f,g,h,i))