-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (86 loc) · 2.85 KB
/
Copy pathmain.cpp
File metadata and controls
105 lines (86 loc) · 2.85 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/***************************************************
*
* file: main.cpp
*
* Copyright (C) Angela Burova and Egor Smirnov 2017
*
****************************************************
*/
#include <fstream>
#include <vector>
#include <cstdlib>
#include <iostream>
#include "matrix_operations.h"
#include "implicit_als_train.h"
#include "prediction.h"
using FPType = float;
TablePtr<FPType> readCsv(const char* const name);
int main()
{
TablePtr<FPType> data = readCsv("als_train.csv");
als::Train<FPType> algorithm;
algorithm.parameter.nFactors = 3;
algorithm.parameter.nIteration = 5;
try
{
algorithm.compute(data);
}
catch (std::exception exc)
{
printf("exception: %s\n", exc.what());
exit(-1);
}
auto model = algorithm.getModel();
auto itemsFactors = model->getItemsFactors();
auto usersFactors = model->getUsersFactors();
printTable(itemsFactors, "ItemsFactors");
printTable(usersFactors, "UsersFactors");
printTable(utils::matrixMultiplication(utils::getTransposeMatrix(usersFactors), itemsFactors), "Result");
als::Prediction<FPType> predict(model,data);
std::cout<<"Rating user #3 and item #3= "<<predict.getRating(3,3)<<std::endl;
std::pair<FPType,FPType> result=predict.getBestItem(4);
std::cout<<"Best item for user #4 = "<<result.first<<" with rating= "<<result.second<<std::endl;
result=predict.getBestItem();
std::cout<<"Best item for all users = "<<result.first<<" with rating= "<<result.second<<std::endl;
std::cout<<"Max item = "<<predict.maxItem()<<std::endl;
std::cout<<"Min item = "<<predict.minItem()<<std::endl;
std::cout<<"Avg= "<<predict.avg()<<std::endl;
return 0;
}
TablePtr<FPType> readCsv(const char* const name)
{
std::ifstream fin(name);
if (!fin)
{
printf("file was not found: %s\n", name);
exit(-1);
}
fin.seekg(0, fin.end);
size_t length = fin.tellg();
fin.seekg(0, fin.beg);
char* buff = (char*)malloc(length * sizeof(char));
fin.read(buff, length);
size_t nCols = 1;
for (size_t i = 0; i < length; ++i)
{
if (buff[i] == ',') ++nCols;
if (buff[i] == '\n' || buff[i] == '\r') break;
}
for (size_t i = 0; i < length; ++i)
if (buff[i] == '\n' || buff[i] == ',' || buff[i] == '\r')
buff[i] = '\0';
std::vector<FPType> values;
for (size_t i = 0; i < length; ++i)
{
values.push_back(std::atof(buff + i));
while (i < length && buff[i] != '\0') ++i;
while (i < length && buff[i] == '\0') ++i;
}
free(buff);
const size_t nRows = values.size() / nCols;
TablePtr<FPType> data(new Table<FPType>(nRows, nCols));
for (size_t i = 0; i < nRows; ++i)
for (size_t j = 0; j < nCols; ++j)
(*data)[i*nCols + j] = values[i*nCols + j];
return data;
}