Spin glass model of learning by selection