It is not always reasonable to set a fixed weight value to an attribute (or a variable) because this cannot accurately maintain the similarity between users and leads to decrease the performance of collaborative filtering recommendation algorithms. In this paper, a piecewise weighting method is proposed with the hyper-class representation for improving the collaborative filtering recommendation. Specifically, a kernel function is first applied to map the original data into the kernel space, and to learn the weight of each attribute. And then, the hyper-class representation of the data is constructed to learn the weight of the segmented attribute value (hyper-class) for each attribute, so as to construct a piecewise weighting function. In addition, the piecewise weighting function is applied to calculate the similarity between users for a collaborative filtering recommendation. Finally, experiments are conducted to examine the collaborative filtering recommendation algorithm, and show that the proposed algorithm with piecewise weighting functions is more efficient than the compared algorithm with fixed weight values, in terms of Root Mean Square Error (RMSE), Mean Absolute Error (MAE) and Precision.