Skip to content

Instantly share code, notes, and snippets.

@bowbowbow
Created August 15, 2017 09:14
Show Gist options
  • Save bowbowbow/c29bf8958592c47dba46ebfa196ad06d to your computer and use it in GitHub Desktop.
Save bowbowbow/c29bf8958592c47dba46ebfa196ad06d to your computer and use it in GitHub Desktop.
class FindZero {
private:
ifstream fin;
ofstream fout;
clock_t start;
const string userFactorsFile = "user_factors";
const string itemFactorsFile = "item_factors";
const string zeroFile = "zero_list.txt";
map<ll, int> userMap, itemMap;
vector<vector<double> > users, items;
const int factors = 30;
const double threshold = 0.05;
public:
void run() {
clockInit("readFactor");
read(userFactorsFile, users, userMap);
read(itemFactorsFile, items, itemMap);
printDurating("readFactor");
clockInit("makeZeroFile");
makeZeroFile();
printDurating("makeZeroFile");
}
void makeZeroFile() {
fout.open(zeroFile);
for(auto &i: userMap) {
if(i.first % 5000 == 0) {
cout << "now : " << i.first << endl;
}
for(auto&j: itemMap) {
double ret = multifly(i.second, j.second);
if(ret < threshold) {
fout << i.first << " " << j.first << endl;
}
}
}
fout.close();
}
double multifly(ll userI, ll userJ) {
int userIdx = userMap[userI];
int itemIdx = itemMap[userJ];
double ret = 0.0;
for(int i=0;i<factors;i++) {
ret += users[userIdx][i]*items[itemIdx][i];
}
return ret;
}
double multifly(int userIdx, int itemIdx) {
double ret = 0.0;
for(int i=0;i<factors;i++) {
ret += users[userIdx][i]*items[itemIdx][i];
}
return ret;
}
void read(string filename, vector<vector<double> >& mat, map<ll, int>& mp) {
fin.open(filename);
while(!fin.eof()) {
ll userId;
fin >> userId;
vector<double> row;
for(int i=0;i<factors; i++) {
double tmp;
fin >> tmp;
row.push_back(tmp);
}
mat.push_back(row);
mp[userId] = (int)mat.size()-1;
}
fin.close();
}
void clockInit(string name) {
cout << "[" << name << "] :: Start" << endl;
start = clock();
}
void printDurating(string name) {
cout << "["<<name << "] :: End / duration : " << (clock() - start ) / (double) CLOCKS_PER_SEC << endl;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment