/* * Repeats first and last knot p+1 times. Removes no knots. */ std::vector<double> BSplineBasis1D::knotVectorRegular(std::vector<double> &X) const { // Copy X -> sort -> remove duplicates -> resize = a sorted vector of unique values std::vector<double> uniqueX(X); sort(uniqueX.begin(), uniqueX.end()); std::vector<double>::iterator it = unique_copy(uniqueX.begin(), uniqueX.end(), uniqueX.begin()); uniqueX.resize(distance(uniqueX.begin(),it)); std::vector<double> knots; it = uniqueX.begin(); // Repeat first knot p + 1 times (for interpolation of start point) for(unsigned int i = 0; i < degree + 1; i++) { knots.push_back(*it); } // Add unique knots for(it = uniqueX.begin()+1; it < uniqueX.end()-1; it++) { knots.push_back(*it); } // Repeat last knot p + 1 times (for interpolation of end point) it = uniqueX.end()-1; // Last element in uniqueX for(unsigned int i = 0; i < degree + 1; i++) { knots.push_back(*it); } // Number of knots in a (p+1)-regular knot vector assert(knots.size() == uniqueX.size() + 2*degree); return knots; }
/* * Creates a regular knot vector of n+p+1 equidistant knots, * where the first and last knot is repeated p+1 times, * and n is the number of unique values in x. * * This knot vector can be used for all degrees > 0. */ std::vector<double> BSplineBasis1D::knotVectorEquidistant(std::vector<double> &X) const { // Copy X -> sort -> remove duplicates -> resize = a sorted vector of unique values std::vector<double> uniqueX(X); sort(uniqueX.begin(), uniqueX.end()); std::vector<double>::iterator it = unique_copy(uniqueX.begin(), uniqueX.end(), uniqueX.begin()); uniqueX.resize(distance(uniqueX.begin(),it)); // Minimum number of interpolation points if(uniqueX.size() < degree+1) { std::ostringstream e; e << "BSplineBasis1D::knotVectorFree: Only " << uniqueX.size() << " unique interpolation points are given. A minimum of degree+1 = " << degree+1 << " unique points are required to build a B-spline basis of degree " << degree << "."; throw Exception(e.str()); } std::vector<double> knots; for(unsigned int i = 0; i < degree; i++) knots.push_back(uniqueX.front()); std::vector<double> equiKnots = linspace(uniqueX.front(), uniqueX.back(), uniqueX.size() - degree + 1); for(auto it = equiKnots.begin(); it != equiKnots.end(); ++it) knots.push_back(*it); for(unsigned int i = 0; i < degree; i++) knots.push_back(uniqueX.back()); return knots; }
void pure_numeric_algo(){ cout<<endl<<"pure_numeric_algo :"<<endl; int ia[11] = {0, 1, 2, 3, 4, 5, 6,6,6, 7, 8 }; vector<int> iv(ia,ia+11); vector<int> iv2(ia+6,ia+8); vector<int>::iterator itr; itr = adjacent_find(iv.begin(),iv.end(), equal_to<int>()); //找到相邻元素相等的第一个元素 cout<<"adjacent_find: "<<*itr<<endl; cout<<"count: "<<count(iv.begin(),iv.end(), 6)<<endl; //找到元素值等于6的个数 cout<<"count_if: "<<count_if(iv.begin(),iv.end(), bind2nd(less<int>() , 7))<<endl; //找到小于7的元素个数 itr = find(iv.begin(),iv.end(), 4); //找到元素等于4的第一个元素位置 cout<<"find: "<<*itr<<endl; itr = find_if(iv.begin(),iv.end(), bind2nd(greater<int>() , 2)); //找到元素大于2的第一个元素位置 cout<<"find_if: "<<*itr<<endl; itr = find_end(iv.begin(),iv.end(), iv2.begin(),iv2.end()); //找到iv序列中最后子序列匹配出现的位置 cout<<"find_end: "<<*(itr+3)<<endl; itr = find_first_of(iv.begin(),iv.end(), iv2.begin(),iv2.end()); //找到iv序列中最先子序列匹配出现的位置 cout<<"find_end: "<<*(itr+3)<<endl; remove(iv.begin(),iv.end(), 6); //删除元素,向前移,但是容器size不变,后面会剩余数据 cout<<"remove: "<<iv<<endl; vector<int> iv3(12,-1); remove_copy(iv.begin(),iv.end(), iv3.begin(), 6); //删除元素,将数据拷贝到新容器,后面会剩余数据 cout<<"remove_copy: "<<iv3<<endl; remove_if(iv.begin(),iv.end(), bind2nd(less<int>(), 6)); //删除小于6的元素,后面会剩余数据 cout<<"remove_if: "<<iv<<endl; remove_copy_if(iv.begin(),iv.end(), iv3.begin(), bind2nd(less<int>(), 7)); //删除小于7的元素,并拷贝到新容器 cout<<"remove_copy_if: "<<iv3<<endl; replace(iv.begin(),iv.end(), 6, 3); //将所有元素值为6的改为3 cout<<"replace: "<<iv<<endl; replace_copy(iv.begin(),iv.end(),iv3.begin(), 3, 5); //将所有元素值为3的改为5,结果保存在新容器中 cout<<"replace_copy: "<<iv3<<endl; replace_if(iv.begin(),iv.end(), bind2nd(less<int>(),5), 2); //将所有元素值小于5的改为2 cout<<"replace_if: "<<iv<<endl; replace_copy_if(iv.begin(),iv.end(),iv3.begin(), bind2nd(equal_to<int>(),8), 9); //将所有元素值为8的改为9,结果保存在新容器中 cout<<"replace_copy_if: "<<iv3<<endl; reverse(iv.begin(),iv.end()); cout<<"reverse: "<<iv<<endl; //反转 reverse_copy(iv.begin(),iv.end(),iv3.begin()); cout<<"reverse_copy: "<<iv3<<endl; //反转,结果保存在新容器 rotate(iv.begin(),iv.begin() + 4, iv.end()); cout<<"rotate: "<<iv<<endl; //互换元素 rotate_copy(iv.begin(),iv.begin() + 5,iv.end(),iv3.begin()); cout<<"rotate_copy: "<<iv3<<endl; //互换元素,结果保存在新容器 int ia2[] = {2, 8}; vector<int> iv4(ia2,ia2+2); cout<<"search: "<<*search(iv.begin(),iv.end(),iv4.begin(),iv4.end())<<endl; //查找子序列出现的第一次出现地点 swap_ranges(iv4.begin(),iv4.end(),iv.begin()); //按区域交换 cout<<"swap_ranges: "<<iv<<endl<<iv4<<endl; transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(), 2)); //所有元素减2 cout<<"transform: "<<iv<<endl; transform(iv4.begin(),iv4.end(),iv.begin(),iv4.begin(),plus<int>()); //区间对应元素相加 cout<<"transform: "<<iv4<<endl; /************************************************************************/ vector<int> iv5(ia,ia+11); vector<int> iv6(ia+4,ia+8); vector<int> iv7(15); cout<<"max_element: "<<*max_element(iv5.begin(), iv5.end())<<endl; //最大元素游标 cout<<"min_element: "<<*min_element(iv5.begin(), iv5.end())<<endl; cout<<"includes: "<<includes(iv5.begin(),iv5.end(),iv6.begin(),iv6.end())<<endl; //iv6中元素是不是都在iv5中,这两个必须排过序 merge(iv5.begin(),iv5.end(),iv6.begin(),iv6.end(),iv7.begin()); //两个排序号的容器合并 cout<<"merge: "<<iv7<<endl; partition(iv7.begin(),iv7.end(),bind2nd(equal_to<int>(), 5)); //满足条件的放在左边,不满足条件的放在右边 cout<<"partition: "<<iv7<<endl; unique(iv5.begin(),iv5.end()); //去重,重复的元素放在后面 cout<<"unique: "<<iv5<<endl; unique_copy(iv5.begin(),iv5.end(),iv7.begin()); //去重,结果保存在新容器 cout<<"unique_copy: "<<iv7<<endl; }
void sortWithUniqueElement(ifstream& infile, ofstream& outfile) { if (!infile) { cerr << "Can not open the infile!" << endl; exit(-1); } vector<string> coll; copy(istream_iterator<string>(infile), istream_iterator<string>(), back_inserter(coll)); sort(coll.begin(), coll.end()); unique_copy(coll.begin(), coll.end(), ostream_iterator<string>(outfile, "\n")); coll.clear(); }
void TestBackinserter() { std::vector<int> vec = { 0, 1, 2, 2, 1, 3, 4, 3, 4, 5, 9, 9 }; std::cout << "the org vector is: "; for (auto v : vec) { std::cout << v << " "; } std::cout << std::endl; std::list<int> li; unique_copy(vec.begin(), vec.end(), back_inserter(li)); std::cout << "the unique_copy list is: "; for (auto v : li) { std::cout << v << " "; } std::cout << std::endl; sort(vec.begin(), vec.end()); std::cout << "after sort, the vector is: "; for (auto v : vec) { std::cout << v << " "; } std::cout << std::endl; std::list<int> li2; unique_copy(vec.begin(), vec.end(), back_inserter(li2)); std::cout << "after sort, the unique_copy list is: "; for (auto v : li2) { std::cout << v << " "; } std::cout << std::endl; }
inline InputIterator unique(InputIterator first, InputIterator last, command_queue &queue = system::default_queue()) { typedef typename std::iterator_traits<InputIterator>::value_type value_type; size_t count = detail::iterator_range_size(first, last); vector<value_type> temp(count, queue.get_context()); buffer_iterator<value_type> iter = unique_copy(first, last, temp.begin(), queue); copy(temp.begin(), iter, first, queue); return first + detail::iterator_range_size(temp.begin(), iter); }
int main() { istream_iterator<int> cin_it(cin); // reads ints from cin istream_iterator<int> end_of_stream; // end iterator value // initialize vec from the standard input: vector<int> vec(cin_it, end_of_stream); sort(vec.begin(), vec.end()); // writes ints to cout using " " as the delimiter ostream_iterator<int> output(cout, " "); // write only the unique elements in vec to the standard output unique_copy(vec.begin(), vec.end(), output); cout << endl; // write a newline after the output return 0; }
/*public static*/ CoordinateSequence* CoordinateSequence::removeRepeatedPoints(const CoordinateSequence *cl) { #if PROFILE static Profile *prof= profiler->get("CoordinateSequence::removeRepeatedPoints()"); prof->start(); #endif const vector<Coordinate> *v=cl->toVector(); vector<Coordinate> *nv=new vector<Coordinate>; nv->reserve(v->size()); unique_copy(v->begin(), v->end(), back_inserter(*nv)); CoordinateSequence* ret=CoordinateArraySequenceFactory::instance()->create(nv); #if PROFILE prof->stop(); #endif return ret; }
/* * Free knot vector for degrees 1, 2, and 3 only. * The free knot vector ensure that the first and last knot is repeated p + 1 times, * and that the total number of knots is n + p + 1. * Example for degree=3 and x={a,b,c,d,e,f,g,h}: knots={a,a,a,a,c,d,e,f,h,h,h,h} */ std::vector<double> BSplineBasis1D::knotVectorFree(std::vector<double> &X) const { // Copy X -> sort -> remove duplicates -> resize = a sorted vector of unique values std::vector<double> uniqueX(X); sort(uniqueX.begin(), uniqueX.end()); std::vector<double>::iterator it = unique_copy(uniqueX.begin(), uniqueX.end(), uniqueX.begin()); uniqueX.resize(distance(uniqueX.begin(),it)); // The minimum number of samples from which a free knot vector can be created if(uniqueX.size() < degree+1) { std::ostringstream e; e << "BSplineBasis1D::knotVectorFree: Only " << uniqueX.size() << " unique interpolation points are given. A minimum of degree+1 = " << degree+1 << " unique points are required to build a B-spline basis of degree " << degree << "."; throw Exception(e.str()); } std::vector<double> knots; it = uniqueX.begin(); // Repeat first x value p + 1 times (for interpolation of start point) for(unsigned int i = 0; i < degree + 1; i++) { knots.push_back(*it); } if(degree == 1) { // No knots removed for (it = uniqueX.begin()+1; it < uniqueX.end()-1; it++) { knots.push_back(*it); } } else if(degree == 2) { // First knot removed for (it = uniqueX.begin()+2; it < uniqueX.end()-1; it++) { knots.push_back(*it); } } else if(degree == 3) { // First and last knot removed for (it = uniqueX.begin()+2; it < uniqueX.end()-2; it++) { knots.push_back(*it); } } else { throw Exception("BSplineBasis1D::knotVectorFree: A free knot vector is only supported by degrees 1, 2, and 3!"); } // Repeat last x value p + 1 times (for interpolation of end point) it = uniqueX.end()-1; // Last element in uniqueX for(unsigned int i = 0; i < degree + 1; i++) { knots.push_back(*it); } // Number of knots in a (p+1)-regular knot vector // Ensures a square matrix when calculating the control points assert(knots.size() == uniqueX.size() + degree + 1); return knots; }
double OutputInfo::getROC( vector< pair< int, float > > data ) { //uni_pred = unique(pred); //[uni_pred, idx] = sort(uni_pred, 'descend'); sort( data.begin(), data.end(), nor_utils::comparePair<2, float, float, greater<float> >() ); vector< double > uni_pred(data.size()); vector< double >::iterator it; int posNum = 0; int negNum = 0; for( size_t i = 0; i < data.size(); i++ ) { uni_pred[i] = data[i].second; if ( data[i].first == 1 ) posNum++; else negNum++; } //copy( pred.begin(), pred.end(), uni_pred.begin() ); sort( uni_pred.begin(), uni_pred.end() ); it=unique_copy (uni_pred.begin(),uni_pred.end(),uni_pred.begin()); uni_pred.resize( it - uni_pred.begin() ); reverse( uni_pred.begin(), uni_pred.end() ); uni_pred.push_back( 0.0 ); int l = uni_pred.size(); //Y = zeros(size(testY)); vector< int > Y( data.size() ); vector< pair< double, double > > M( uni_pred.size() ); double x,y; int TP = 0; int FP = 0; int j = 0; for( int i = 0; i < uni_pred.size(); i++ ) { double th = uni_pred[i]; while ( ( j < data.size() ) && ( data[j].second > th ) ) { if ( data[j].first == 1 ) TP++; if ( data[j].first == 0 ) FP++; j++; } if ( FP == 0 )x = 0; else x = ((double)FP)/((double)negNum); if ( TP == 0 )y = 0; else y = ((double)TP)/((double)posNum); M[i] = pair<double, double>(x,y); } sort( M.begin(), M.end(), nor_utils::comparePair<1, float, float, less<float> >() ); sort( M.begin(), M.end(), nor_utils::comparePair<2, float, float, less<float> >() ); double prevX = 0.0; double prevY = 0.0; double ROCscore = 0.0; cout.precision(10); for( int i = 0; i < M.size(); i++ ) { ROCscore += ((((M[i].first-prevX)*(M[i].second-prevY))/2)+(M[i].first-prevX)*prevY); prevX = M[i].first; prevY = M[i].second; //cout << ROCscore << endl; } ROCscore += (1-prevX)*prevY; return ROCscore; }
void NaiveBayes::train(const Matrix &X, const Matrix &y) { // clear last data labelDistinct.clear(); attributeUniqueNumber.clear(); labelProbability.clear(); attributeProbability.clear(); // y distinct vector<double> labels; for (Matrix::size_type i = 0; i < y.rowSize(); ++i) { labels.push_back(y(i, 0)); } sort(labels.begin(), labels.end()); unique_copy(labels.begin(), labels.end(), back_inserter(labelDistinct)); #ifdef DEBUG cout << "all labels" << endl; for (auto label : labels) cout << label << '\t'; cout << endl; cout << "distinct labels" << endl; for (auto label : labelDistinct) cout << label << '\t'; cout << endl << endl; #endif // calculate labelPro auto N = y.rowSize(); double yDistinct = static_cast<double>(y.distinctColumn()[0].size()); for (auto yEach : labelDistinct) { auto range = equal_range(labels.begin(), labels.end(), yEach); auto yCount = range.second - range.first; labelProbability[yEach] = (yCount + lambda) / (static_cast<double>(N) + yDistinct * lambda); #ifdef DEBUG cout << "y = " << yEach << " "; cout << "labelPro = " << labelProbability[yEach] << endl; #endif } // calculate distinct number of attribute in each column vector<vector<double>> distinctAttribute = X.distinctColumn(); attributeUniqueNumber.resize(X.columnSize()); transform(distinctAttribute.begin(), distinctAttribute.end(), attributeUniqueNumber.begin(), [](const vector<double> &distinctUnit) {return distinctUnit.size();}); #ifdef DEBUG for (auto &distinctNumber : attributeUniqueNumber) cout << distinctNumber << '\t'; cout << endl; #endif // attributeProbability vector<Matrix> trainVec = X.merge(y, 1).splictRow(); for (auto yEach : labelDistinct) { #ifdef DEBUG cout << "y = " << yEach << endl; #endif attributeProbability[yEach].resize(X.columnSize()); auto partitionIter = partition(trainVec.begin(), trainVec.end(), [=](const Matrix &m) {return m(0, m.columnSize() - 1) == yEach;}); auto xWithSameY = partitionIter - trainVec.begin(); vector<vector<double>> attributes(X.columnSize()); for (vector<Matrix>::iterator it = trainVec.begin(); it != partitionIter; ++it) { for (Matrix::size_type col = 0; col < it->columnSize() - 1; ++col) { attributes[col].push_back(it->operator()(0, col)); } } for (auto i = 0; i < attributes.size(); ++i) { vector<double> columnEach = attributes[i]; sort(columnEach.begin(), columnEach.end()); vector<double> columnDistinct; unique_copy(columnEach.begin(), columnEach.end(), back_inserter(columnDistinct)); for (double &attributeEach : columnDistinct) { auto range = equal_range(columnEach.begin(), columnEach.end(), attributeEach); auto xCount = range.second - range.first; attributeProbability[yEach][i][attributeEach] = (xCount + lambda) / (static_cast<double>(xWithSameY) + attributeUniqueNumber[i] * lambda); #ifdef DEBUG cout << "y = " << yEach << " " << i << "th column "; cout << " attribute = " << attributeEach ; cout << " attributePro = " << xCount / static_cast<double>(xWithSameY) << endl; #endif } } } }