LoginSignup
0
0

学習日記#6: Kaggle startbook ch2.2

Last updated at Posted at 2024-04-29

ゴール

TitanicのコンペにSubmit

教材

Kaggleスタートブック ch2.2

やったこと

  • Kaggle上でNotebook作成
  • Inputをコンペから直接追加
  • Kaggle startbookのサンプルコード(.ipynb)をKaggle NotebookにImport

学び、気づき

  • サンプルコードをそのまま実行したらFuture Warningsでる件 #1
    data['Sex'].replace(['male', 'female'], [0, 1], inplace=True)
    • replaceメソッドでinplace引数=Trueを使うと以下メッセージが出る(ただし処理は正常に実行される)どうやら「記法は新しいやつにしてくれよな!」と言ってるらしい。
FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.


  data['Sex'].replace(['male', 'female'], [0, 1], inplace=True)
/tmp/ipykernel_34/3697913337.py:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`
  data['Sex'].replace(['male', 'female'], [0, 1], inplace=True)
  • サンプルコードをそのまま実行したらFuture Warningsでる件 #2

  • メソッドやら引数やら

    • concatメソッドのsort=Falseで並べ替えしない

    • concatメソッドのignore_index=Trueで縦結合の連番振り直し

    • 置換処理はreplaceメソッド(リスト型の多次元配列)よりmapメソッド(ディクショナリ型)のほうが後々分かりやすい気もするが...棲み分けかたは今のところ謎

    • 欠損値を補完するときのdata['Age'].fillna(np.random.randint(age_avg - age_std, age_avg + age_std), inplace=True)は便利そう

      • 平均と標準偏差の差と和の間の整数値をランダムで補完するやり方
    • 不要な列を削除する際に削除対象の列を格納する変数を定義しておくと読みやすい

      delete_columns = ['Name', 'PassengerId', 'SibSp', 'Parch', 'Ticket', 'Cabin']
      data.drop(delete_columns, axis=1, inplace=True) 
      
    • 冒頭でconcatメソッド使って結合した学習データとテストデータについてとき超量エンジニアリング実行後に分割する際に以下のlenメソッドを使った方法で簡単に分割できる

      train = data[:len(train)]
      test = data[len(train):]
      
      • 前提として学習データとテストデータが縦結合していること。

詰まった...

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(penalty='l2', solver='sag', random_state=0)
clf.fit(X_train, y_train)

↑を実行すると↓のエラーを吐いてしまうが解決方法が不明

/opt/conda/lib/python3.10/site-packages/sklearn/linear_model/_sag.py:350: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(

異常値や欠損値の処理が不十分なのか...???
とはいえサンプルコードそのまま実行すすめてSubmitまで一応完了できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0