from sklearn.neighbors import KNeighborsClassifier import numpy as np import util def knnClassify(trainData, trainLabel, testData): knnClf = KNeighborsClassifier() knnClf.fit(trainData, np.ravel(trainLabel)) testLabel = knnClf.predict(testData) util.saveResult(testLabel, './result/sklearn_knn_result.csv') return testLabel if __name__ == "__main__": trainData, trainLabel = util.loadTrainData() testData = util.loadTestData() knnClassify(trainData, trainLabel, testData) print 'knnClassify is finished!'
from sklearn.ensemble import RandomForestClassifier # Visualisation import matplotlib.pyplot as plt # plot the data import seaborn as sns # data visualisation sns.set(color_codes=True) #% matplotlib inline import util # In[54]: x_raw, y_raw = util.loadTrainData() x_test = util.loadTestData() # In[55]: table = { "Class_1": 1, "Class_2": 2, "Class_3": 3, "Class_4": 4, "Class_5": 5, "Class_6": 6, "Class_7": 7, "Class_8": 8, "Class_9": 9 } y_temp = []
userNDCGs = [] for user in users: recommendations = rec.makeRecomendations(user, k) recItems = recommendations['items'].tolist() actualItems = data[data['visitorid'] == user].itemid.tolist() hits = set(recItems) & set(actualItems) positions = [] for hit in hits: positions.append(recItems.index(hit)) userNDCG = 0 for pos in positions: userNDCG += 1/(log(pos+2)) userNDCGs.append(userNDCG) return statistics.mean(userNDCGs) # "Main" if __name__ == "__main__": recomender = Recomender('model-450.meta') #TODO: insert model path data = loadTestData() while True: k = input("Please enter k value (or quit to exit): ") if str(k).lower() == "quit": break try: k = int(k) except: print("Please enter an integer") continue print('Top-k Hit Ratio:', hitRatio(recomender, data[0], int(k))) print('nDCG:', nDCG(recomender, data[0], int(k)))
def evaluate_lenet5(learning_rate=0.1, n_epoches=200, nkerns=[20, 50], batch_size=500): # load data from dataset logging.info('... loading data') trainData, trainLabel = util.load_total_data() testData = util.loadTestData() trainData = util.upToInt(trainData) train_set_x = theano.shared(np.asarray(trainData, dtype = theano.config.floatX), borrow = True) train_set_y = theano.shared(np.asarray(trainLabel, dtype = theano.config.floatX), borrow = True) test_set_x = theano.shared(np.asarray(testData, dtype = theano.config.floatX), borrow = True) train_set_y = T.cast(train_set_y, 'int32') n_train_batches = train_set_x.get_value(borrow=True).shape[0] n_valid_batches = train_set_x.get_value(borrow=True).shape[0] n_test_batches = test_set_x.get_value(borrow=True).shape[0] n_train_batches /= batch_size n_valid_batches /= batch_size n_test_batches /= batch_size rng = np.random.RandomState(23455) # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch # start-snippet-1 x = T.matrix('x') y = T.ivector('y') logging.info('... building the model') layer0_input = x.reshape((batch_size, 1, 28, 28)) # Construct the first convolutional pooling layer: # filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24) # maxpooling reduces this further to (24/2, 24/2) = (12, 12) # 4D output tensor is thus of shape (batch_size, nkerns[0], 12, 12) layer0 = LeNetConvPoolLayer( rng, input=layer0_input, image_shape=(batch_size, 1, 28, 28), filter_shape=(nkerns[0], 1, 5, 5), poolsize=(2, 2) ) # Construct the second convolutional pooling layer # filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8) # maxpooling reduces this further to (8/2, 8/2) = (4, 4) # 4D output tensor is thus of shape (batch_size, nkerns[1], 4, 4) layer1 = LeNetConvPoolLayer( rng, input=layer0.output, image_shape=(batch_size, nkerns[0], 12, 12), filter_shape=(nkerns[1], nkerns[0], 5, 5), poolsize=(2, 2) ) # the HiddenLayer being fully-connected, it operates on 2D matrices of # shape (batch_size, num_pixels) (i.e matrix of rasterized images). # This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4), # or (500, 50 * 4 * 4) = (500, 800) with the default values. layer2_input = layer1.output.flatten(2) # construct a fully-connected sigmoidal layer layer2 = HiddenLayer( rng, input=layer2_input, n_in=nkerns[1] * 4 * 4, n_out=500, activation=T.tanh ) # classify the values of the fully-connected sigmoidal layer layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10) # the cost we minimize during training is the NLL of the model cost = layer3.negative_log_likelihood(y) # create a function to compute the mistakes that are made by the model validate_model = theano.function( [index], layer3.errors(y), givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size], } ) # create a list of all model parameters to be fit by gradient descent params = layer3.params + layer2.params + layer1.params + layer0.params # create a list of gradients for all model parameters grads = T.grad(cost, params) # train_model is a function that updates the model parameters by # SGD Since this model has many parameters, it would be tedious to # manually create an update rule for each model parameter. We thus # create the updates list by automatically looping over all # (params[i], grads[i]) pairs. updates = [ (param_i, param_i - learning_rate * grad_i) for param_i, grad_i in zip(params, grads) ] train_model = theano.function( [index], cost, updates=updates, givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size] } ) validation_model = theano.function( [index], layer3.errors(y), givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size] } ) logging.info('... training') patience = 10000 patience_increase = 2 improvement_threshold = 0.995 validation_frequency = min(n_train_batches, patience / 2) best_validation_loss = np.inf best_iter = 0 test_score = 0. start_time = time.clock() epoch = 0 done_looping = False while (epoch < n_epoches) and (not done_looping): epoch = epoch + 1 for minibatch_index in xrange(n_train_batches): iter = (epoch - 1) * n_train_batches + minibatch_index if iter % 100 == 0: logging.info('training @ iter = %d' % (iter)) cost_ij = train_model(minibatch_index) if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set validation_losses = [validation_model(i) for i in xrange(n_valid_batches)] this_validation_loss = np.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation error %f %%' % (epoch, minibatch_index + 1, n_train_batches, this_validation_loss * 100.)) if (this_validation_loss * 100.) < 0.001: done_looping = True break end_time = time.clock() logging.info('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' % ((end_time - start_time) / 60.)) # make a prediction and save file # make a prediction predict_model = theano.function( inputs=[index], outputs= layer3.predict(), givens={ x: test_set_x[index * batch_size: (index + 1) * batch_size] } ) # save the result file testLabel = np.array([]) for test_index in range(n_test_batches): tempLabel = predict_model(test_index) testLabel = np.hstack((testLabel, tempLabel)) util.saveResult(testLabel, './result/cnn_result.csv')