분석 Python(344)
-
Python Group 별로 Bar Graph 그릴 때,
그룹별로 시각화할 일이 있어서 찾아보는데, R과 같이 제공해주는 것도 있지만, 잘 생각해서 해야 하는 것도 있다 목적은 train과 test 별로 각각의 Category 비율 파악을 하는 것이 목적 f, ax = plt.subplots(3,4 , figsize = (20,20)) axx = ax.flatten() f2, ax2 = plt.subplots(3,4 , figsize = (20,20)) axx3 = ax2.flatten() for axx2 , axx4 , j in zip(axx, axx3, catcols) : #output = data.iloc[index_info[0]].reset_index(drop=True).iloc[idx][j].value_counts() tr = data.iloc[ind..
2019.06.09 -
[ Python ] 메모리 누수 해결에 도움되는 패키지 소개
도움이 되셨다면, 광고 한번만 눌러주세요. 블로그 관리에 큰 힘이 됩니다 ^^ 최근에 UI를 만들어야 하는 일을 하고 있었는데, 같은 것을 실행하는데 누적되는 문제가 발생하였다. gc.collect()를 해도 소용이 없어서, 여러 가지를 찾다가 다음과 같은 것이 나와서 공유한다. 실제 테스트를 하니, 같은 작업을 반복할 때는 그만큼 데이터가 계속해서 쌓이지는 않았다. 아직 문제는 할당된 메모리가 다시 돌아오지 않는 다는 점....... (어디로 간 거냐....) ## 190605 기준 최신 버전은 5.2입니다. # installing jemalloc wget https://github.com/jemalloc/jemalloc/releases/download/5.2.0/jemalloc-5.2.0.tar.bz..
2019.06.05 -
[Tensorflow] Cyclical Learning Rate
먼가 지금 학습을 시키는 것이 있는데, 먼가 추가적으로 데이터를 넣으면서 시키는 중이다. 그런데 현재 구조는 learning deacy 방식으로 사용하다 보니, 점점 학습률은 떨어져서 새로운 데이터에 대해서 그만큼 새로운 최적의 값을 못찾는 기분이라서, learning rate를 어떻게 해봐야겠다고 생각을 하고 다음과 같이 수정을 해보려고 한다! 그래서 알아본게 Cyclical로 주기를 만드는 방식이다. 이러면 잘 되겠지 하고 기대를 하면서!!!!!!!!!! cyclic_learning_rate( global_step, ## Epoch 의미 learning_rate=0.01, max_lr=0.1, ## 최대 learning rate step_size=20., gamma=0.99994, ## 폭? 작게 할..
2019.06.05 -
[변수 생성] AutoEncoder로 파생변수 만들기 -2 (모델링 파트) Catboost
https://data-newbie.tistory.com/163 AutoEncoder로 파생변수 만들기 데이터 분석을 하다보면, 새로운 파생변수를 만들어야 할 때가 있다. 개인적으로 나도 그러한 부분에 관심이 있어서 여래개로 포스팅을 했는데, 한번 보시면 도움이 될 것 같다. https://data-newbie.tistory.com/.. data-newbie.tistory.com ## 모델링 파트 이전에 AutoEncoder에서 얻은 Code값을 이용해서 모델링을 해봤습니다. 모델은 tree-based model 중에서 그나마 Parameter에 크게 의존하지 않는다는 catboost로 하였습니다. 궁금하신분은 https://data-newbie.tistory.com/131?category=750846 ..
2019.06.02 -
[변수 생성] AutoEncoder로 파생변수 만들기
데이터 분석을 하다보면, 새로운 파생변수를 만들어야 할 때가 있다. 개인적으로 나도 그러한 부분에 관심이 있어서 여래개로 포스팅을 했는데, 한번 보시면 도움이 될 것 같다. https://data-newbie.tistory.com/148?category=749566 https://data-newbie.tistory.com/93?category=749566 https://data-newbie.tistory.com/93?category=749566 그렇다면 결국 도메인이 중요한 것이고 즉 협엽이 잘 될 때 Feature Engineering도 빛을 발하게 되는데, 사실 현실은 그렇지 못하다. 만약 자기 데이터를 가지고 할 경우, 그러한 부분에 대해서 충분히 고려하고, 자주 데이터를 만질 수도 있지만, 만약 ..
2019.06.02 -
[ Python ] Python에서도 R처럼 data.table을 사용할 수 있어요.
Python에서 대부분의 데이터를 읽을 때 주로 Pandas를 사용하지만, 작은 데이터를 읽을 때는 상관이 없다. 하지만 최근에 34GB짜리를 읽어보려고 했는데, 시간을 많이 잡아먹어서 스트레스를 너무 많이 받았다. 머 Spark나 이런 걸 사용하면 된다고 하지만, 전처리를 좀 하고 싶었던 게 있었는데, 아직 내가 Spark 같은 것을 잘 사용하지 못해서 결국 Pandas로 하였다. data.table은 알려진 바로는 단일 머신으로 했을 때 가장 빠르게 데이터 핸들링을 할 수 있는 도구다. 이 글에서는 100GB까지 된다고 하니, 잘 사용할 줄 알아야겠다. ## 리눅스 버전 # If you have Python 3.5 pip install https://s3.amazonaws.com/h2o-release..
2019.06.02 -
tensorflow mask 씌우기
오늘은 결과물에서 특정 부분에 대해서 mask를 씌울 일이 있어서, 인터넷에 찾아봐도 잘 나오지 않아서 stackoverflow에 물어보니 답이 나와서 정리해서 코드를 보여주려고 한다. mask를 씌우는 이유는 머랄까 maxpooling처럼 실제로 쓰는 부분에 대해서만 Gradient를 주고 싶어서 하려고 한다. 물론 아예안하고 할 수도 있는데 왜 굳이 mask를 씌우냐고 할 수 있지만, 실제 자료가 [10000 , 10]이런 거여서 여러 가지 shape 바꿔주고 해서 [10000, 3, 4, 1]로 바꾸려고 한다. 그러다 보니 2개 정도 마스크를 씌워야 했다. G = generator(Z) ## [ None , 3, 4, 1] masking = tf.sequence_mask([4, 4, 2], maxl..
2019.05.30 -
tensorflow 폴더 생성 및 지우기
if tf.gfile.Exists(model_dir): tf.gfile.DeleteRecursively(model_dir) tf.gfile.MakeDirs(model_dir) tf.gfile.MakeDirs(model_dir + "/save_model") tf.gfile.MakeDirs(vis_dir) tf.gfile.MakeDirs(board_dir) else : tf.gfile.MakeDirs(model_dir) tf.gfile.MakeDirs(model_dir + "/save_model") tf.gfile.MakeDirs(vis_dir) tf.gfile.MakeDirs(board_dir)
2019.05.28 -
GAN minibatch discrimination code
NUM_KERNELS = 5 def minibatch(input, num_kernels=NUM_KERNELS, kernel_dim=3, name = None ): output_dim = num_kernels*kernel_dim w = tf.get_variable("Weight_minibatch_" + name , [input.get_shape()[1], output_dim ], initializer=tf.random_normal_initializer(stddev=0.2)) b = tf.get_variable("Bias_minibatch_" + name , [output_dim],initializer=tf.constant_initializer(0.0)) x = tf.matmul(input, w) + b a..
2019.05.28 -
numpy.unique, numpy.searchsorted
카테고리를 정수로 변환하기! pandas에는 cat.codes가 있다. 유니크 범위 : ( 0 , 카레고리수 -1 ) from itertools import combinations possible_categories = list(map(lambda x: x[0] + x[1], list(combinations('abcdefghijklmn', 2)))) categories = np.random.choice(possible_categories, size=10000) print(categories) ['al' 'kl' 'jk' ... 'jm' 'bm' 'hj'] unique_categories, new_categories = np.unique(categories, return_inverse=True) print..
2019.05.26 -
Broadcasting, numpy.newaxis
numpy를 자주 쓰다 보면 사용하면 Broadcasting! https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html Broadcasting — NumPy v1.16 Manual Broadcasting Note See this article for illustrations of broadcasting concepts. The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” acro docs.scip..
2019.05.26 -
numpy argsort
argsort를 이용해서 하나의 기준이 있을 때 다른 것도 쉽게 정렬할 수 있다! 만약 키와 나이가 쌍이 있다고 하자. ages = np.random.randint(low=30, high=60, size=10) heights = np.random.randint(low=150, high=210, size=10) print(ages) print(heights) ## ages : [56 53 52 54 42 53 39 37 50 57] ## heights : [189 182 158 204 187 168 155 169 206 153] 이것을 나이를 기준으로 height 도 같이 sort해보자! 이 방식은 zip(ages , heights) 보다 훨씬 빠르다고 합니다!! sorter = np.argsort(age..
2019.05.26