Example #1
0
void test_Iterate_UserBased_Predictor()
{
	clock_t begin,end;
	string train_data = "E:/data/resys/corpus/1/train/base";
	string test_data = "E:/data/resys/corpus/1/test/u1.test";
	FileDataReader train_reader = FileDataReader(train_data);
	FileDataReader test_reader = FileDataReader(test_data);
	int n_user, n_item, n_rating;
	n_user = 943;
	n_item = 1682;
	n_rating = 80000;
	UserModel trainModel(n_user,n_item,n_rating);
	UserModel testModel(n_user,n_item,20000);
	UserModel resultModel(n_user,n_item,20000);
	if(train_reader.readData(trainModel) && test_reader.readData(testModel)) {
		int min_com = 0;
		double min_sim = 0.00001;
		int shrink_parameter = 25;
		Similarity_Shrinking shrinker(min_com,min_sim,shrink_parameter);
		Pearson_Similarity similarity(shrinker);
		UserBased_Predictor predictor = UserBased_Predictor(30,0);
		begin = clock();
		predictor.train(trainModel);
		end = clock();
		cout << "Train time: " << (end - begin) * 1.0 / CLOCKS_PER_SEC << endl;
		begin = end;
		predictor.predictAll(trainModel,testModel,resultModel);
		end = clock();
		cout << "Predict time: " << (end - begin) * 1.0 / CLOCKS_PER_SEC << endl;
		double rmse = evl_rmse(testModel, resultModel);
		cout << "RMSE: " << rmse << endl;
	}
}
Example #2
0
int main(int argc,char** argv){
    if(3!=argc){
        cerr<<argv[0]<<" confPath confFile"<<endl;
        return -1;
    }

    string confPath = string(argv[1]);
    string confFile = string(argv[2]);

    trainModel(confPath,confFile);

    return 0;
}
Example #3
0
void printUserSim()
{
	clock_t begin,end;
	string train_data = "E:/data/resys/corpus/1/train/base";
	FileDataReader train_reader = FileDataReader(train_data);
	int n_user, n_item, n_rating;
	n_user = 943;
	n_item = 1682;
	n_rating = 80000;
	UserModel trainModel(n_user,n_item,n_rating);
	if(train_reader.readData(trainModel)) {
		int min_com = 0;
		double min_sim = 0.0;
		int shrink_parameter = 30;
		int k_neighbors = 30;
		Similarity_Shrinking shrinker(min_com,min_sim,shrink_parameter);
		Pearson_Similarity similarity(shrinker);
		NeighborCollection *neighbor_cls;
		neighbor_cls = new NeighborCollection[trainModel.n_user+trainModel.user_0];
		similarity.similarity(trainModel, neighbor_cls);

		string sim_file = "E:/data/resys/corpus/1/user_sims.txt";
		ofstream fout(sim_file.c_str());
		vector<Neighbor>::iterator nb_it;
		for(int u = trainModel.user_0; u < trainModel.user_0 + trainModel.n_user; u++) {
			double sum = 0;
			int count = 0;
			for(nb_it = neighbor_cls[u].begin(); nb_it != neighbor_cls[u].end(); nb_it++) {
				int n = nb_it->neighbor;
				double sim = nb_it->similarity;
				sum += sim;
				count ++;
				if(count >= k_neighbors)
					break;
			}
			count = 0;
			if(sum > 0) {
				for(nb_it = neighbor_cls[u].begin(); nb_it != neighbor_cls[u].end(); nb_it++) {
					int n = nb_it->neighbor;
					double sim = nb_it->similarity;
					sim = sim/sum;
					fout << u << " " << n << " " << sim << endl;
					count ++;
					if(count >= k_neighbors)
						break;
				}
			}
		}
		fout.close();
	}
}
Example #4
0
void ActGraph::trainModel(const std::string featureFile)
{
    // features
    cv::Mat stat;        // mean_x, mean_y, var_x, var_y
    cv::Mat shapeXY;     // track shapes
    cv::Mat mbhX;        // MBHx
    cv::Mat mbhY;        // MBHy

    // import features from file
    readFeatures(featureFile, stat, shapeXY, mbhX, mbhY);

    // GMM-clustering
    trainModel(stat, shapeXY, mbhX, mbhY);
}
Example #5
0
void test_ItemBased_Predictor()
{
	clock_t begin,end;
	string train_data = "E:/data/resys/corpus/1/train/base";
	string test_data = "E:/data/resys/corpus/1/test/u1.test";
	FileDataReader train_reader = FileDataReader(train_data);
	FileDataReader test_reader = FileDataReader(test_data);
	int n_user, n_item, n_rating;
	n_user = 943;
	n_item = 1682;
	n_rating = 80000;
	ItemModel trainModel(n_user,n_item,n_rating);
	ItemModel testModel(n_user,n_item,20000);
	ItemModel resultModel(n_user,n_item,20000);

	double *userRanks = new double[n_user+1];
	string filepath = "E:/data/resys/corpus/1/user_com_ranks.txt";
	train_reader.readUserComRank(userRanks,filepath);

	if(train_reader.readData(trainModel) && test_reader.readData(testModel)) {
		int min_com = 0;
		double min_sim = 0.0;
		int shrink_parameter = 30;
		Similarity_Shrinking shrinker(min_com,min_sim,shrink_parameter);
		Pearson_Similarity similarity(shrinker);
		ItemBased_Predictor predictor = ItemBased_Predictor(similarity,30,0);
		begin = clock();
		//predictor.train(trainModel);
		predictor.train(trainModel,userRanks);
		end = clock();
		cout << "Train time: " << (end - begin) * 1.0 / CLOCKS_PER_SEC << endl;
		begin = end;
		predictor.predictAll(trainModel,testModel,resultModel);
		end = clock();
		cout << "Predict time: " << (end - begin) * 1.0 / CLOCKS_PER_SEC << endl;
		double rmse = evl_rmse(testModel, resultModel);
		cout << "RMSE: " << rmse << endl;
		double mae = evl_mae(testModel, resultModel);
		cout << "MAE: " << mae << endl;
	}
}
void trainingMode(){
	/* *********************      Interface part      **********************/
	/* ********************* **** ************** **** **********************/
	cout << endl << endl;

	cout << " ================== Training mode  =============" << endl;
	cout << " == Enter number of training examples: "; 	cin >> numTrainingExamples;
	cout << " == Enter number of LBFGS iterations: " ; 	cin >> numLbfgsIterations;
	cout << " == Enter lambda: "                     ; 	cin >> lambda;

	/* Checking if input is valid */
	if (numTrainingExamples > 40000) numTrainingExamples = 40000;
	if (numTrainingExamples <= 0) numTrainingExamples = 150;
	if (numLbfgsIterations < 1) numLbfgsIterations = 5;

	/* Reading model */
	//NNModel* model = readModel();
	ModelLoader l;
	NNModel* model = l.loadModel("modelL56.nn");

	trainModel(model);
}
Example #7
0
void ProbabilityModel::train()
{

	//zliczanie ilości rekordów o danej klasie
	float rowCount = 0.0f;
	for (Data::iterator rowIt = trainSet->begin(); rowIt != trainSet->end() ; rowIt++)
	{
		DataRow &row = *rowIt;
		if (labelProbs.find(row.getLabel()) == labelProbs.end())
			labelProbs[row.getLabel()] = 1.0f;
		else
			labelProbs[row.getLabel()] += 1.0f;
		rowCount += 1.0f;
	}

	for (StrFloatMap::iterator it=labelProbs.begin() ; it != labelProbs.end(); it++)
	{
		(*it).second = (*it).second / rowCount;
		//std::cout << (*it).second << "\n";
	}

	trainModel();
}
Example #8
0
bool KMeans::train_(MatrixFloat &data){
	
	trained = false;
	
	if( numClusters == 0 ){
        errorLog << "train_(MatrixFloat &data) - Failed to train model. NumClusters is zero!" << std::endl;
		return false;
	}
    
    if( data.getNumRows() == 0 || data.getNumCols() == 0 ){
        errorLog << "train_(MatrixFloat &data) - The number of rows or columns in the data is zero!" << std::endl;
		return false;
	}
    
	numTrainingSamples = data.getNumRows();
	numInputDimensions = data.getNumCols();

	clusters.resize(numClusters,numInputDimensions);
	assign.resize(numTrainingSamples);
	count.resize(numClusters);

	//Randomly pick k data points as the starting clusters
	Random random;
	Vector< UINT > randIndexs(numTrainingSamples);
	for(UINT i=0; i<numTrainingSamples; i++) randIndexs[i] = i;
    std::random_shuffle(randIndexs.begin(), randIndexs.end());

    //Copy the clusters
	for(UINT k=0; k<numClusters; k++){
		for(UINT j=0; j<numInputDimensions; j++){
            clusters[k][j] = data[ randIndexs[k] ][j];
		}
	}

	return trainModel( data );
}