-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreastCancer.py
69 lines (63 loc) · 4.36 KB
/
breastCancer.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
import random
import pandas
import time
from randomForest import trainTestSplit, createRandomForest, randomForestPredictions, calculateAccuracy
dataFrame = pandas.read_csv("dataset_files/breast_cancer.csv")
dataFrame = dataFrame.drop("id", axis = 1)
dataFrame = dataFrame[dataFrame.columns.tolist()[1: ] + dataFrame.columns.tolist()[0: 1]]
dataFrameTrain, dataFrameTest = trainTestSplit(dataFrame, testSize = 0.25)
print("Random Forest - Breast Cancer Dataset")
print(" Maximum bootstrap size (n) is {}".format(dataFrameTrain.shape[0]))
print(" Maximum random subspace size (d) is {}".format(dataFrameTrain.shape[1] - 1))
print("\n Change n, keep other parameters")
for i in range(10, dataFrameTrain.shape[0] + 1, 50):
startTime = time.time()
randomForest = createRandomForest(dataFrameTrain, bootstrapSize = i, randomAttributes = 10, randomSplits = 50, forestSize = 30, treeMaxDepth = 3)
buildingTime = time.time() - startTime
randomForestTestResults = randomForestPredictions(dataFrameTest, randomForest)
accuracyTest = calculateAccuracy(randomForestTestResults, dataFrameTest.iloc[:, -1]) * 100
randomForestTrainResults = randomForestPredictions(dataFrameTrain, randomForest)
accuracyTrain = calculateAccuracy(randomForestTrainResults, dataFrameTrain.iloc[:, -1]) * 100
print(" n = {}, d = {}, s = {}, k = {}, maxDepth = {}:".format(i, 10, 50, 30, 3))
print(" accTest = {0:.2f}%, ".format(accuracyTest), end = "")
print("accTrain = {0:.2f}%, ".format(accuracyTrain), end = "")
print("buildTime = {0:.2f}s".format(buildingTime), end = "\n")
print("\n Change d, keep other parameters")
for i in range(10, dataFrameTrain.shape[1], 2):
startTime = time.time()
randomForest = createRandomForest(dataFrameTrain, bootstrapSize = 60, randomAttributes = i, randomSplits = 50, forestSize = 30, treeMaxDepth = 3)
buildingTime = time.time() - startTime
randomForestTestResults = randomForestPredictions(dataFrameTest, randomForest)
accuracyTest = calculateAccuracy(randomForestTestResults, dataFrameTest.iloc[:, -1]) * 100
randomForestTrainResults = randomForestPredictions(dataFrameTrain, randomForest)
accuracyTrain = calculateAccuracy(randomForestTrainResults, dataFrameTrain.iloc[:, -1]) * 100
print(" n = {}, d = {}, s = {}, k = {}, maxDepth = {}:".format(60, i, 50, 30, 3))
print(" accTest = {0:.2f}%, ".format(accuracyTest), end = "")
print("accTrain = {0:.2f}%, ".format(accuracyTrain), end = "")
print("buildTime = {0:.2f}s".format(buildingTime), end = "\n")
print("\n Change s, keep other parameters")
for i in range(10, 100 + 1, 10):
startTime = time.time()
randomForest = createRandomForest(dataFrameTrain, bootstrapSize = 60, randomAttributes = 10, randomSplits = i, forestSize = 30, treeMaxDepth = 3)
buildingTime = time.time() - startTime
randomForestTestResults = randomForestPredictions(dataFrameTest, randomForest)
accuracyTest = calculateAccuracy(randomForestTestResults, dataFrameTest.iloc[:, -1]) * 100
randomForestTrainResults = randomForestPredictions(dataFrameTrain, randomForest)
accuracyTrain = calculateAccuracy(randomForestTrainResults, dataFrameTrain.iloc[:, -1]) * 100
print(" n = {}, d = {}, s = {}, k = {}, maxDepth = {}:".format(60, 10, i, 30, 3))
print(" accTest = {0:.2f}%, ".format(accuracyTest), end = "")
print("accTrain = {0:.2f}%, ".format(accuracyTrain), end = "")
print("buildTime = {0:.2f}s".format(buildingTime), end = "\n")
print("\n Change k, keep other parameters")
for i in range(10, 100 + 1, 10):
startTime = time.time()
randomForest = createRandomForest(dataFrameTrain, bootstrapSize = 60, randomAttributes = 10, randomSplits = 50, forestSize = i, treeMaxDepth = 3)
buildingTime = time.time() - startTime
randomForestTestResults = randomForestPredictions(dataFrameTest, randomForest)
accuracyTest = calculateAccuracy(randomForestTestResults, dataFrameTest.iloc[:, -1]) * 100
randomForestTrainResults = randomForestPredictions(dataFrameTrain, randomForest)
accuracyTrain = calculateAccuracy(randomForestTrainResults, dataFrameTrain.iloc[:, -1]) * 100
print(" n = {}, d = {}, s = {}, k = {}, maxDepth = {}:".format(60, 10, 50, i, 3))
print(" accTest = {0:.2f}%, ".format(accuracyTest), end = "")
print("accTrain = {0:.2f}%, ".format(accuracyTrain), end = "")
print("buildTime = {0:.2f}s".format(buildingTime), end = "\n")