0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

下手なりにpandasで偏差値を出してみた

Last updated at Posted at 2019-06-24

いきなり誰?

新入社員のIT屋(27)
社内データの分析に回された

QiitaとGitの垢を持っていないと転職時に面接でハブられるとの文言から慌てて始めた。
SIer4次請から転職したら人生が変わった話

GithubやQiitaの利用
転職活動当時、この辺のアカウントを持っていないせいで面接で門前払いを食らったことがありました(笑)

ていうか転職するまで知らなかったんで終わってましたがね……はい、お恥ずかしい。

書いたコード

仕事でデータの偏差値を出したほうが良くなったので書いていたが
Pandasには標準偏差までは出してくれるが、偏差値行列まで計算はしてくれないそうなので
ちなみにPandasで偏差値を出す @osk_kamui にすでに書かれてるが
どうせ書くなら一般化してみようと。

PandasUserExtra.py
import pandas as pd
import numpy as np

#############################


def ss(input,axis=0,skipna=True):
    row_mean    = input.mean(axis=axis,skipna=skipna)
    row_std     = input.std (axis=axis,skipna=skipna)
    output      = pd.DataFrame(np.zeros((len(input),len(input.columns))))

    if axis==0 or axis=="index" :
        for i in range(len(input)):
            for j in range(len(input.columns)):
                num     = input.iloc[i,j]
                result  = ((num - row_mean[j])*10/row_std[j]) +50
                output.iloc[i,j] = result
    else:
        for i in range(len(input)):
            for j in range(len(input.columns)):
                num     = input.iloc[i,j]
                result  = ((num - row_mean[i])*10/row_std[i]) +50
                output.iloc[i,j] = result

    return output
Main.py
import numpy as np
import pandas as pd
import PandasUserExtra as PUE

#############################

matrix = pd.DataFrame(np.array([[1,2,3,4,5],[6,7,8,9,10],[1,2,3,4,5]],dtype=np.float64))

ss = NPE.ss(matrix,axis=0)

print(matrix)
print(ss)

ss = NPE.ss(matrix,axis=1)

print(matrix)
print(ss)
     0    1    2    3     4
0  1.0  2.0  3.0  4.0   5.0
1  6.0  7.0  8.0  9.0  10.0
2  1.0  2.0  3.0  4.0   5.0
           0          1          2          3          4
0  44.226497  44.226497  44.226497  44.226497  44.226497
1  61.547005  61.547005  61.547005  61.547005  61.547005
2  44.226497  44.226497  44.226497  44.226497  44.226497
     0    1    2    3     4
0  1.0  2.0  3.0  4.0   5.0
1  6.0  7.0  8.0  9.0  10.0
2  1.0  2.0  3.0  4.0   5.0
           0          1     2          3          4
0  37.350889  43.675445  50.0  56.324555  62.649111
1  37.350889  43.675445  50.0  56.324555  62.649111
2  37.350889  43.675445  50.0  56.324555  62.649111

所感

一応、動く。
だが、for文2重ループしているのがかなり美しくない。
そもそもnumpy/pandasはpythonの鈍重な処理を高速化するように作られているので
それらの関数を使いこなす事が求められる。
(が、そういう入門書がないのでレファレンスを丸暗記するしかないというゴリ押し。あるいはオライリー本?)
もしくはlambdaを使ってもっと記述を簡略化できれば良いのでが、mapかfilterで表面的に使うだけで
lambdaが内部的に何をやっているのか完全に理解出来ないと厳しいなぁと。

会社内にPython出来る方が殆ど居ないので先輩方ご教授お願いします。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?