We propose a learning method for efficient team formation by self-interested agents in task oriented domains. Service requests on computer networks have recently been rapidly increasing. To improve the performance of such systems, issues with effective team formation to do tasks has attracted our interest. The main feature of the proposed method is learning from two-sided viewpoints, i.e., team leaders who have the initiative to form teams or team members who work in one of the teams that are solicited. For this purpose, we introduce three parameters to agents so that they can select their roles of being a leader or a member, then an agent can anticipate which other agents should be selected as team members and which team it should join. Our experiments demonstrated that the numbers of tasks executed by successfully generated teams increased by approximately 17% compared with a conventional method.