v-crn Code Log

主に備忘録

Python

pandas.DataFrame.agg() における SpecificationError: nested renamer is not supported

Code df.agg({'feature': ['mean', 'count', 'std', 'median', 'skew']}) Error Message SpecificationError: nested renamer is not supported Solution df.feature.agg(['mean', 'count', 'std', 'median', 'skew']) Ref. python - Solution for Specifica…

Google Colaboratory を使うときはパッケージのバージョンに注意

Google Colaboratory とローカル環境で同じPythonコードを走らせているのに挙動が異なる場合がある。その原因はパッケージのバージョンの違いかもよ、というお話。 たとえば、 imblearn.under_sampling.RandomUnderSampler を使った次のコードをローカルのPy…

Google Colaboratory でカスタムモジュールをインポートする

手順 1. Google Drive を Mount from google.colab import drive drive.mount('/content/drive') 2. sys.path.append() でパスを通してインポート 例として次のようなファイル構成になっているとする。 My Drive Colab Notebooks ML workspace my_module __i…

TensorFlow 2.0.0へのアップデートで出くわしたエラー:AttributeError: module 'tensorflow' has no attribute 'placeholder'

前提 TensorFlow 2.0.0 エラー内容 TensorFlow 1.4.0で書かれたコード g = tf.Graph() with g.as_default(): x = tf.placeholder(dtype=tf.float32, shape=(None), name='x') w = tf.Variable(2.0, name='weight') b = tf.Variable(0.7, name='bias') z = w*…

VS CodeのPythonエクステンションを使ったJupterNotebookは通常のJupyterNotebookより遅い

要点 VS Codeの拡張機能『Python』を導入すると、VS Code上で通常のJupyterNotebookと同様にIPythonファイルを編集し、実行することができるようになります。 しかし、いざそれを使ってみると普段より明らかに処理に時間がかかっているように思えたので、今…

IPython上でGIFを表示する

前提 ルートディレクトリに表示したいGIFファイルがある場合を想定します。 !ls sample_data sin_curve.gif ちなみに上の「sin_curve.gif」を生成するコードはこんな感じ。 %% !pip install animatplot %% """ Introduction to animatplot https://animatplo…

Google Colaboratoryのスクラッチコードセル

Google Colaboratoryにおいて!ls -aやdf -hなどのちょっとした確認のためのコマンドを実行したいとき、スクラッチコードセルが便利。 以下のショートカットキーでスクラッチコードセルが用意される。 Mac Cmd + alt + N Windows Ctrl + alt + N

ValueError: Integers to negative integer powers are not allowed.[Python][Numpy]

エラー内容 以下のように、負の数を指数とした累乗の計算でエラーが出た。 for num in np.arange(-5, 5): print(10**num) """ ValueError: Integers to negative integer powers are not allowed. """ 「負の整数による整数のべき乗は許されない」とのこと。…