-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.cpp
54 lines (43 loc) · 1.17 KB
/
knn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// knn.cpp
// MovieGross
//
//
// Copyright © 2016 ArsenKevinMD. All rights reserved.
//
#include <stdio.h>
#include <stdio.h>
#include "knn.h"
#include <math.h>
#include <iostream>
using namespace std;
using namespace MovieData;
//K-Nearest Neighbor Algorithm
namespace KnearestAlgo {
//constructor
Knn::Knn(vector<Movie>&train){
trainingSet = train;
}
//method to predict approximate movie gross
float Knn::predict(Movie& aMovie){
//make a pair of distances and gross
vector<pair<float, float>> gross;
//go through and store each eucledian distances with its Movie
for(Movie m: trainingSet){
pair<float, float>p;
p.first = m - aMovie;
p.second = m[GROSS];
gross.push_back(p);
}
//sort the vector
sort(gross.begin(),gross.end());
//count accumulate the gross of neighbors
float total = 0;
for(int i =0; i < 43; ++i){
total += gross[i].second;
}
//conduct simple inflation calculations
float avg = total/43;
return avg;
}
}