├── ANN.py ├── DataOperation.py ├── Evaluation.py ├── HTRU_2.csv └── main.py /ANN.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | 4 | def sigmoid(x): 5 | active = [] 6 | for i in x: 7 | active.append(1.0 / (1.0 + np.exp(-i))) 8 | active = np.array(active) 9 | return active 10 | 11 | #f'(x) 12 | def sigmoid_derivate(f): 13 | return f * (1 - f) 14 | 15 | def BPTrain(feature, label, Ni, Nh, No,n): 16 | Wi = np.random.random((Ni, Nh)) 17 | Wo = np.random.random((Nh, No)) 18 | LearningRate = 0.001 19 | h = 0.01 20 | 21 | iter = 1 22 | while(iter > 0): 23 | for i in range(0, len(feature)): 24 | #forward 25 | nethValue = np.dot(feature[i], Wi) 26 | nethActive = sigmoid(nethValue) 27 | netoValue = np.dot(nethActive, Wo) 28 | netoActive = sigmoid(netoValue) 29 | 30 | #back 31 | #sum of squared errors: 1/2(t-y)^2 32 | loss_derivate_se = label[i] - netoActive 33 | #cross entropy 34 | loss_derivate_ce = np.log(netoActive / (1 - netoActive)) 35 | #print(loss_derivate_ce) 36 | outputDelta = loss_derivate_ce * sigmoid_derivate(netoActive) 37 | hiddenDelta = np.dot(Wo, outputDelta) * sigmoid_derivate(nethActive) 38 | for j in range(0, No): 39 | Wo[:,j] += LearningRate * (outputDelta[j] * nethActive + h/n * Wo[:,j]) 40 | for k in range(0, Nh): 41 | Wi[:,k] += LearningRate * (hiddenDelta[k] * feature[i] + h/n * Wi[:,k]) 42 | 43 | iter -= 1 44 | return Wi, Wo 45 | 46 | def predict(testFeature, testLabel, Wi, Wo): 47 | hiddenValue = np.dot(testFeature, Wi) 48 | hiddenActive = sigmoid(hiddenValue) 49 | outputValue = np.dot(hiddenActive, Wo) 50 | outputActive = sigmoid(outputValue) 51 | prediction = [] 52 | for i in range(0, len(testFeature)): 53 | if outputActive[i] > 0.5: 54 | prediction.append(1) 55 | else: 56 | prediction.append(0) 57 | return prediction -------------------------------------------------------------------------------- /DataOperation.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import random 3 | import numpy as np 4 | 5 | #read data from .csv 6 | def readData(): 7 | rf = open('HTRU_2.csv','r') 8 | dataReader = csv.reader(rf) 9 | posDataList = [] 10 | negDataList = [] 11 | featureIndex = 8 12 | featureMin = [999,999,999,999,999,999,999,999] 13 | featureMax = [0,0,0,0,0,0,0,0] 14 | for rList in dataReader: 15 | for i in range(featureIndex): 16 | if float(rList[i]) > float(featureMax[i]): 17 | featureMax[i] = float(rList[i]) 18 | if float(rList[i]) < float(featureMin[i]): 19 | featureMin[i] = float(rList[i]) 20 | if rList[featureIndex] == '0': 21 | negDataList.append(rList) 22 | else: 23 | posDataList.append(rList) 24 | rf.close() 25 | # 17,898 total examples. 26 | # 1,639 positive examples. 27 | # 16,259 negative examples. 28 | 29 | train_features = [] 30 | train_labels = [] 31 | test_features = [] 32 | test_labels = [] 33 | 34 | negDataList = random.sample(negDataList, len(posDataList)) 35 | #0.1,0.5 92% 36 | posTestList = random.sample(posDataList, int(round(len(posDataList) * 0.2))) 37 | negTestList = random.sample(negDataList, int(round(len(negDataList) * 0.2))) 38 | 39 | #devide postive examples into test and train 40 | for pTest in posTestList: 41 | fList = [] 42 | for i in range(featureIndex): 43 | fList.append((float(pTest[i])-featureMin[i])/(featureMax[i]-featureMin[i])) 44 | test_labels.append(1) 45 | fList.append(1) 46 | test_features.append(fList) 47 | 48 | posDataList.remove(pTest) 49 | 50 | for pTrain in posDataList: 51 | fList = [] 52 | for i in range(featureIndex): 53 | fList.append((float(pTrain[i])-featureMin[i])/(featureMax[i]-featureMin[i])) 54 | train_labels.append(1) 55 | fList.append(1) 56 | train_features.append(fList) 57 | 58 | 59 | # devide negtive examples into test and train 60 | for nTest in negTestList: 61 | fList = [] 62 | for i in range(featureIndex): 63 | fList.append((float(nTest[i])-featureMin[i])/(featureMax[i]-featureMin[i])) 64 | test_labels.append(0) 65 | fList.append(1) 66 | test_features.append(fList) 67 | 68 | negDataList.remove(nTest) 69 | 70 | for nTrain in negDataList: 71 | fList = [] 72 | for i in range(featureIndex): 73 | fList.append((float(nTrain[i])-featureMin[i])/(featureMax[i]-featureMin[i])) 74 | train_labels.append(0) 75 | fList.append(1) 76 | train_features.append(fList) 77 | 78 | train_labels = np.array(train_labels) 79 | train_features = np.array(train_features) 80 | test_labels = np.array(test_labels) 81 | test_features = np.array(test_features) 82 | return train_labels, train_features, test_labels, test_features 83 | 84 | -------------------------------------------------------------------------------- /Evaluation.py: -------------------------------------------------------------------------------- 1 | #evaluate 2 | #TP: positive examples predicted to be positive 3 | #FP: negtive examples predicted to be positive 4 | #TN: negtive examples predicte to be negtive 5 | #FN: positive examples predicted to be negtive 6 | 7 | def Evaluate(prediction, test_labels): 8 | TP = FP = FN = TN = 0 9 | for i in range(len(test_labels)): 10 | if test_labels[i] == prediction[i] and test_labels[i] == 1: 11 | TP += 1 12 | elif test_labels[i] == prediction[i] and test_labels[i] == 0: 13 | TN += 1 14 | elif test_labels[i] != prediction[i] and test_labels[i] == 1: 15 | FN += 1 16 | elif test_labels[i] != prediction[i] and test_labels[i] == 0: 17 | FP += 1 18 | 19 | print TP 20 | print TN 21 | print FP 22 | print FN 23 | P = 1.0 * TP / (TP + FP) 24 | print("P", P) 25 | R = 1.0 * TP / (TP + FN) 26 | print("R", R) 27 | F1 = 2 * P * R / (P + R) 28 | print("F1",F1) 29 | return F1 30 | -------------------------------------------------------------------------------- /HTRU_2.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shellystar/artificial-neural-networks/7f7a58489c443fca6e8bb77949cfea95cffd7291/HTRU_2.csv -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import DataOperation 4 | import ANN 5 | import Evaluation 6 | 7 | train_labels, train_features, test_labels, test_features = DataOperation.readData() 8 | Wi, Wo = ANN.BPTrain(train_features, train_labels, 9, 4, 1,len(train_labels)) 9 | #print(Wi) 10 | #print(Wo) 11 | prediction = ANN.predict(test_features, test_labels, Wi, Wo) 12 | coun1 = 0 13 | coun2 = 0 14 | #print(prediction) 15 | for l in prediction: 16 | if l == 1: 17 | coun1 += 1 18 | else: 19 | coun2 += 1 20 | print("count positive",coun1) 21 | print("count negtive",coun2) 22 | F1 = Evaluation.Evaluate(prediction, test_labels) --------------------------------------------------------------------------------