Advertisement
gfro_za

Untitled

Apr 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. """
  2. This code has been amended from the Udacity Coding Resource page for the Machine Learnig 1 project.
  3. """
  4.  
  5. # Import libraries
  6. # numpy is used for numerical function
  7. # pylab is used for plotting
  8. # sklearn is the sci-kit library
  9. from numpy import *
  10. import pylab as pl
  11. from sklearn.utils import shuffle
  12. from sklearn.metrics import mean_squared_error
  13. from sklearn import datasets
  14. from sklearn.tree import DecisionTreeRegressor
  15. from sklearn.neighbors import KNeighborsRegressor
  16. from pybrain.structure import FeedForwardNetwork
  17. from pybrain.tools.shortcuts import buildNetwork
  18. from pybrain.datasets import SupervisedDataSet
  19. from pybrain.supervised.trainers import BackpropTrainer
  20. from sklearn.svm import SVR
  21.  
  22. def DecisionTreeF(x, iDepth, X_trainData, y_trainData, X_testData, y_testData):
  23.     # Setup a Decision Tree Regressor so that it learns a tree with depth 5
  24.     regressor = DecisionTreeRegressor(max_depth=iDepth)
  25.     regressor.fit(X_trainData, y_trainData)
  26.     # Use the model to predict the output of a particular sample
  27.     y = regressor.predict(x)
  28.     print "Prediction for Decision Trees = " + str(y)
  29.  
  30.     # Find the MSE on the training set
  31.     train_err = mean_squared_error(y_train, regressor.predict(X_trainData))
  32.     print "Training Error = " + str(train_err)
  33.  
  34.     # Find the MSE on the testing set
  35.     test_err = mean_squared_error(y_test, regressor.predict(X_testData))
  36.     print "Testing Error = " + str(test_err)
  37.  
  38. def kNN(x, iNeigbours, X_trainData, y_trainData, X_testData, y_testData):
  39.     # Setup a Decision Tree Regressor so that it learns a tree with depth 5
  40.     regressor = KNeighborsRegressor(n_neighbors=iNeigbours)
  41.     regressor.fit(X_trainData, y_trainData)
  42.     # Use the model to predict the output of a particular sample
  43.     y = regressor.predict(x)
  44.     print "Prediction for k-NN = " + str(y)
  45.  
  46.     # Find the MSE on the training set
  47.     train_err = mean_squared_error(y_train, regressor.predict(X_trainData))
  48.     print "Training Error = " + str(train_err)
  49.  
  50.     # Find the MSE on the testing set
  51.     test_err = mean_squared_error(y_test, regressor.predict(X_testData))
  52.     print "Testing Error = " + str(test_err)
  53.  
  54.  
  55. def Boosting(x, iDegree, X_trainData, y_trainData, X_testData, y_testData):
  56.     # Setup a Decision Tree Regressor so that it learns a tree with depth 5
  57.     regressor = SVR(kernel='rbf', degree=iDegree)
  58.     regressor.fit(X_trainData, y_trainData)
  59.     # Use the model to predict the output of a particular sample
  60.     y = regressor.predict(x)
  61.     print "Prediction for Boosting = " + str(y)
  62.  
  63.     # Find the MSE on the training set
  64.     train_err = mean_squared_error(y_train, regressor.predict(X_trainData))
  65.     print "Training Error = " + str(train_err)
  66.  
  67.     # Find the MSE on the testing set
  68.     test_err = mean_squared_error(y_test, regressor.predict(X_testData))
  69.     print "Testing Error = " + str(test_err)
  70.  
  71.  
  72. def NN(x, iInputFeatures, iOutputs, X_trainData, y_trainData, X_testData, y_testData):
  73.     # List all the different networks we want to test again
  74.     # All networks have 13 input nodes and 1 output nodes
  75.     # All networks are fully connected
  76.  
  77.     net = buildNetwork(13,9,6,3,1)
  78.     net_arr = 1 #can ignore left over from prev file
  79.  
  80.     # The dataset will have 13 features and 1 target label
  81.     ds = SupervisedDataSet(13, 1)
  82.  
  83.     train_err = 0
  84.     test_err = 0
  85.  
  86.     # We will train each NN for 50 epochs
  87.     max_epochs = 50
  88.  
  89.     # Convert the boston dataset into SupervisedDataset
  90.     for j in range(1, len(X_trainData)):
  91.         ds.addSample(X_trainData[j], y_trainData[j])
  92.  
  93.  
  94.     # Setup a trainer that will use backpropogation for training
  95.     trainer = BackpropTrainer(net, ds)
  96.  
  97.     # Run backprop for max_epochs number of times
  98.     for k in range(1, max_epochs):
  99.         train_err = trainer.train()
  100.  
  101.     # Find the labels for test set
  102.     y = zeros(len(X_testData))
  103.  
  104.     for j in range(0, len(X_testData)):
  105.         y[j] = net.activate(X_testData[j])
  106.  
  107.     # Calculate MSE for all samples in the test set
  108.     test_err = mean_squared_error(y, y_testData)
  109.  
  110.     y2 = net.activate(x)
  111.     print "Prediction for Neural Networks = " + str(y2)    
  112.    
  113.     # Find the MSE on the training set
  114.     print "Training Error = " + str(train_err)
  115.  
  116.     # Find the MSE on the testing set
  117.     print "Testing Error = " + str(test_err)
  118.  
  119. # Load the boston dataset
  120. boston = datasets.load_boston()
  121. # Shuffle it and seperate it into training and testing set
  122. # We need to shuffle it so that when we split it, we sample from the dataset uniformly
  123. X, y = shuffle(boston.data, boston.target)
  124. # Training and testing set is divided in the ration 7:3
  125. offset = int(0.7*len(X))
  126. X_train, y_train = X[:offset], y[:offset]
  127. X_test, y_test = X[offset:], y[offset:]
  128.  
  129. x = [11.95, 0.00, 18.100, 0, 0.6590, 5.6090, 90.00, 1.385, 24, 680.0, 20.20, 332.09, 12.13]
  130. print "Predictions for data set:" + str(x)
  131. DecisionTreeF(x,8,X_train,y_train,X_test,y_test)
  132. kNN(x,5,X_train,y_train,X_test,y_test)
  133. Boosting(x,40,X_train,y_train,X_test,y_test)
  134. NN(x,13,1,X_train,y_train,X_test,y_test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement