SHARE
TWEET

Untitled

a guest Oct 20th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import csv
  2. import math
  3.  
  4.  
  5. def mean(numbers):
  6.     return sum(numbers) / float(len(numbers))
  7.  
  8.  
  9. def stdev(numbers):
  10.     avg = mean(numbers)
  11.     variance = sum([pow(x - avg, 2) for x in numbers]) / float(len(numbers) - 1)
  12.     return math.sqrt(variance)
  13.  
  14.  
  15. def summarize(dataset):
  16.     summaries = [(mean(attribute), stdev(attribute))
  17.                  for attribute in zip(*dataset)]
  18.     del summaries[-1]
  19.     return summaries
  20.  
  21.  
  22. def calcProb(summary, item):
  23.     prob = 1
  24.     for i in range(len(summary)):
  25.         x = item[i]
  26.         mean, stdev = summary[i]
  27.         exponent = math.exp(-math.pow(x - mean, 2) / (2 * math.pow(stdev, 2)))
  28.         final = exponent / (math.sqrt(2 * math.pi) * stdev)
  29.         prob *= final
  30.     return prob
  31.  
  32.  
  33. with open('ds3.csv') as csvFile:
  34.     data = [line for line in csv.reader(csvFile)]
  35. for i in range(len(data)):
  36.     data[i] = [float(x) for x in data[i]]
  37. split = int(0.90 * len(data))
  38.  
  39. train = data[:split]
  40. test = data[split:]
  41. print("{} input rows is split into {} training and {} testing datasets".format(
  42.     len(data), len(train), len(test)))
  43. print("\nThe values assumed for the concept learning attributes are\n")
  44. print(
  45.     "OUTLOOK=> Sunny=1 Overcast=2 Rain=3\nTEMPERATURE=> Hot=1 Mild=2 Cool=3\nHUMIDITY=> High=1 Normal=2\nWIND=> Weak=1 Strong=2")
  46. print("TARGET CONCEPT:PLAY TENNIS=> Yes=10 No=5")
  47. print("\nThe Training set are:")
  48. for x in train:
  49.     print(x)
  50. print("\nThe Test data set are:")
  51. for x in test:
  52.     print(x)
  53. yes = []
  54. no = []
  55. for i in range(len(train)):
  56.     if data[i][-1] == 5.0:
  57.         no.append(data[i])
  58.     else:
  59.         yes.append(data[i])
  60. yes = summarize(yes)
  61. no = summarize(no)
  62. predictions = []
  63. for item in test:
  64.     yesProb = calcProb(yes, item)
  65.     noProb = calcProb(no, item)
  66.     predictions.append(10.0 if (yesProb > noProb) else 5.0)
  67. correct = 0
  68. for i in range(len(test)):
  69.     if (test[i][-1] == predictions[i]):
  70.         correct += 1
  71. print("\nActual values are:")
  72. for i in range(len(test)):
  73.     print(test[i][-1], end=" ")
  74. print("\nPredicted values are:")
  75. for i in range(len(predictions)):
  76.     print(predictions[i], end=" ")
  77. print("\nAccuracy is {}%".format(float(correct / len(test) * 100)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top