... copied from sklearn ...