Advertisement
Guest User

aprendiendo Python

a guest
Jul 20th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. SECTION 1 : Load and setup data for training
  3. """
  4.  
  5. import csv
  6. import random
  7. import math
  8. import operator
  9. import numpy as np
  10. import pandas as pd
  11. from sklearn.model_selection import train_test_split
  12.  
  13. # Load dataset
  14. datatrain = pd.read_csv('iris.data')
  15.  
  16. # Change string value to numeric
  17. datatrain.set_value(datatrain['species']=='Iris-setosa',['species'],0)
  18. datatrain.set_value(datatrain['species']=='Iris-versicolor',['species'],1)
  19. datatrain.set_value(datatrain['species']=='Iris-virginica',['species'],2)
  20. datatrain = datatrain.apply(pd.to_numeric)
  21.  
  22. # Change dataframe to array
  23. datatrain_array = datatrain.as_matrix()
  24.  
  25. # Split x and y (feature and target)
  26. X_train, X_test, y_train, y_test = train_test_split(datatrain_array[:,:4],
  27.                                                     datatrain_array[:,4],
  28.                                                     test_size=0.2)
  29.  
  30. """
  31. SECTION 2 : Build and Train Model
  32.  
  33. Multilayer perceptron model, with one hidden layer.
  34. input layer : 4 neuron, represents the feature of Iris
  35. hidden layer : 10 neuron, activation using ReLU
  36. output layer : 3 neuron, represents the class of Iris, Softmax Layer
  37.  
  38. optimizer = stochastic gradient descent with no batch-size
  39. loss function = categorical cross entropy
  40. learning rate = 0.01
  41. epoch = 500
  42. """
  43.  
  44. from sklearn.neural_network import MLPClassifier
  45.  
  46. mlp = MLPClassifier(hidden_layer_sizes=(10),solver='sgd',learning_rate_init=0.01,max_iter=500)
  47.  
  48.  
  49.  
  50. #KNN
  51.  
  52.  
  53. def euclideanDistance(instance1, instance2, length):
  54.     distance = 0
  55.     for x in range(length):
  56.         distance += pow((instance1[x] - instance2[x]), 2)
  57.     return math.sqrt(distance)
  58.  
  59. def getNeighbors(trainingSet, testInstance, k):
  60.     distances = []
  61.     length = len(testInstance)-1
  62.     for x in range(len(trainingSet)):
  63.         dist = euclideanDistance(testInstance, trainingSet[x], length)
  64.         distances.append((trainingSet[x], dist))
  65.     distances.sort(key=operator.itemgetter(1))
  66.     neighbors = []
  67.     for x in range(k):
  68.         neighbors.append(distances[x][0])
  69.     return neighbors
  70.  
  71. def getResponse(neighbors):
  72.     classVotes = {}
  73.     for x in range(len(neighbors)):
  74.         response = neighbors[x][-1]
  75.         if response in classVotes:
  76.             classVotes[response] += 1
  77.         else:
  78.             classVotes[response] = 1
  79.     sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)
  80.     return sortedVotes[0][0]
  81.  
  82. def getAccuracy(testSet, predictions):
  83.     correct = 0
  84.     for x in range(len(testSet)):
  85.         if testSet[x][-1] == predictions[x]:
  86.             correct += 1
  87.     return (correct/float(len(testSet))) * 100.0
  88.    
  89. def main():
  90.     # prepare data
  91.     trainingSet=[]
  92.     testSet=[]
  93.     trainingSet = np.c_[X_train,y_train]
  94.         testSet = np.c_[X_test,y_test]
  95.    
  96.     # generate predictions
  97.     predictions=[]
  98.     k = 7
  99.     for x in range(len(testSet)):
  100.         neighbors = getNeighbors(trainingSet, testSet[x], k)
  101.         result = getResponse(neighbors)
  102.         predictions.append(result)
  103.         print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1]))
  104.     accuracy = getAccuracy(testSet, predictions)
  105.     print('Accuracy KNN: ' + repr(accuracy) + '%')
  106.  
  107.     # Train the model
  108.         mlp.fit(X_train, y_train)
  109.  
  110.         # Test the model
  111.         print ('Accuracy RNA: ' + repr(mlp.score(X_test,y_test)* 100.0)+ '%')
  112.    
  113. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement