Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
1,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. from __future__ import division
  2. import csv
  3. import numpy as np
  4. import urllib
  5. import math
  6. import random
  7.  
  8. X = []
  9. y = []
  10. url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
  11.  
  12. numClasses=3
  13. pArray = []
  14. classes = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']
  15.  
  16.  
  17. def loadData():
  18.     global X
  19.     global y
  20.     # download the file
  21.     raw_data = urllib.urlopen(url)
  22.  
  23.     # load the CSV file into features (X) and class (y)
  24.     for row in csv.reader(raw_data):
  25.         if row:
  26.             y.append(row[-1])
  27.             X.append(row[:-1])
  28.  
  29.     X = np.asarray(X)
  30.     y = np.asarray(y)
  31.     X = X.astype(np.float)
  32.     [m, n] = X.shape
  33.  
  34.  
  35.     class1 = X[0:50]
  36.     class2 = X[51:100]
  37.     class3 = X[101:150]
  38.  
  39.     # divide training set in the 3 classes
  40.     classes = [class1, class2, class3]
  41.  
  42.     #calculate the probability
  43.     calculateMeanandVar(classes, n)
  44.  
  45.  
  46.  
  47. def calculateMeanandVar(classes, n):
  48.     for i in range(0,3):
  49.         pArray.append([])
  50.         for x in range(0,n):
  51.             mean = np.mean(classes[i][:,x])
  52.             var = np.var(classes[i][:,x])
  53.             pArray[i].append([mean, var])
  54.     classify()
  55.  
  56. def calculateProbability(mean, stdev, x):
  57.     exponent = math.exp(-(math.pow(x-mean,2)/(2*stdev)))
  58.     return (1.0 / (math.sqrt((2.0*math.pi) * stdev))) * exponent
  59.  
  60. def classify():
  61.     my_randoms = random.sample(xrange(150), 100)
  62.     corr=0
  63.     for q in my_randoms:
  64.         x1 = X[q,:]
  65.         results=[]
  66.  
  67.         for i in range(numClasses):
  68.  
  69.             results.append([])
  70.  
  71.             # calculate the probabilities of the features of q being class i
  72.             pA = 0.3333
  73.             p0 = calculateProbability(pArray[i][0][0], pArray[i][0][1], x1[0])
  74.             p1 = calculateProbability(pArray[i][1][0], pArray[i][1][1], x1[1])
  75.             p2 = calculateProbability(pArray[i][2][0], pArray[i][2][1], x1[2])
  76.             p3 = calculateProbability(pArray[i][3][0], pArray[i][3][1], x1[3])
  77.             p = p0 * p1 * p2 * p3 * pA
  78.             results[i].append(p)
  79.  
  80.         # get the max value and index (which class row X[q,:] has the highest
  81.         # probability of belonging to)
  82.         max_value = max(results)
  83.         max_index = results.index(max_value)
  84.  
  85.         if (classes[max_index] == y[max_index]):
  86.             corr +=1
  87.  
  88.  
  89.     print('\nPercentage of rightly classified rows: {0}%').format(corr)
  90.  
  91.  
  92. loadData()
  93. # loadTest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement