機械学習のために、python3.7 jupyterlab上で小数点入りの数字や文字列の入ったデータフレームの中から1列のみを文字列から適当な整数に変換したいです。
以下のように表全体についてordinalencoderを使うと小数点を含むデータや変わってほしくない文字列まで変換されてしまいます。
import sklearn.preprocessing enc = sklearn.preprocessing.OrdinalEncoder() enc.fit(df) df_enc = enc.transform(df) print(df_enc)
例えば、dfのうち、aという1列目のみを数値に変換したい場合どうしたらよいですか。
以下のコードを実行するとReshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.というエラーが出てしまいました。
import sklearn.preprocessing enc = sklearn.preprocessing.OrdinalEncoder() enc.fit(df['a']) df_enc = enc.transform(df['a'])
さらに、reshapeを試そうと思い、以下のコードを実行すると'Series' object has no attribute 'reshape'というエラーが出てきました。
import sklearn.preprocessing enc = sklearn.preprocessing.OrdinalEncoder() df['a'] = df['a'].reshape(1, -1) enc.fit(df['a']) df_enc = enc.transform(df['a'])
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。