as_float_array#

sklearn.utils.as_float_array(X,*,copy=True,force_all_finite='deprecated',ensure_all_finite=None)[source]#

Convert an array-like to an array of floats.

The new dtype will be np.float32 or np.float64, depending on the originaltype. The function can create a copy or modify the argument dependingon the argument copy.

Parameters:
X{array-like, sparse matrix}

The input data.

copybool, default=True

If True, a copy of X will be created. If False, a copy may still bereturned if X’s dtype is not a floating point type.

force_all_finitebool or ‘allow-nan’, default=True

Whether to raise an error on np.inf, np.nan, pd.NA in X. Thepossibilities are:

  • True: Force all values of X to be finite.

  • False: accepts np.inf, np.nan, pd.NA in X.

  • ‘allow-nan’: accepts only np.nan and pd.NA values in X. Values cannotbe infinite.

Added in version 0.20:force_all_finite accepts the string'allow-nan'.

Changed in version 0.23:Acceptspd.NA and converts it intonp.nan

Deprecated since version 1.6:force_all_finite was renamed toensure_all_finite and will be removedin 1.8.

ensure_all_finitebool or ‘allow-nan’, default=True

Whether to raise an error on np.inf, np.nan, pd.NA in X. Thepossibilities are:

  • True: Force all values of X to be finite.

  • False: accepts np.inf, np.nan, pd.NA in X.

  • ‘allow-nan’: accepts only np.nan and pd.NA values in X. Values cannotbe infinite.

Added in version 1.6:force_all_finite was renamed toensure_all_finite.

Returns:
XT{ndarray, sparse matrix}

An array of type float.

Examples

>>>fromsklearn.utilsimportas_float_array>>>importnumpyasnp>>>array=np.array([0,0,1,2,2],dtype=np.int64)>>>as_float_array(array)array([0., 0., 1., 2., 2.])
On this page

This Page