728x90
728x90
from datetime import date
from dateutil.relativedelta import relativedelta

today = date.today()

print(today)

sub_date = today - relativedelta(years=2)

print(sub_date)

 

728x90
728x90

텐서플로우 인덱스 참조

- 텐서의 인덱스 참조도 넘파이와 유사

 

1차원 인덱싱

- 1차원 텐서 만들기 

>>> t = tf.constant([1,2,3,])
>>> t

<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>

- 인덱싱

>>> t[:2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>

 

2차원 인덱싱

- 2차원 텐서만들기

>>> t = tf.constant([[1,2,3],[4,5,6]])
>>> t

<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>

- 2차원 인덱싱

>>> t[:1]

<tf.Tensor: shape=(1, 3), dtype=int32, numpy=array([[1, 2, 3]], dtype=int32)>

>>> t[:,2]

<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 6], dtype=int32)>

>>> t[:2,:2]

<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [4, 5]], dtype=int32)>

 

3차원 인덱싱

- 3차원 텐서만들기

>>> t = tf.constant([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
>>> t

<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]], dtype=int32)>

- 3차원 인덱싱

>>> t[:2,1:,:2]

<tf.Tensor: shape=(2, 1, 2), dtype=int32, numpy=
array([[[ 4,  5]],

       [[10, 11]]], dtype=int32)>
728x90

'Development Study > tensorflow' 카테고리의 다른 글

[tensorflow] 텐서플로우 - 텐서 만들기 #1  (0) 2021.03.10
728x90

tf.constant()

 

- tf.constant() 함수로 텐서를 만들 수 있음

tf.constant(value, dtype=None, shape=None, name='Const')

 

이것저것 해보기

 

import tensorflow as tf

>>> tf.constant([1,2,3,])

<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>

- dtype 지정

>>> tf.constant([1,2,3],dtype=float)

<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>

- shape 지정

- value의 shape 에 맞춰서 지정

>>> tf.constant([1,2,3],dtype=float,shape=(3,1))

<tf.Tensor: shape=(3, 1), dtype=float32, numpy=
array([[1.],
       [2.],
       [3.]], dtype=float32)>

 

 

728x90
728x90

볼린저 밴드 Bollinger Bands

- 이동평균선 ( 볼린저 밴드에서는 단순 이동 평균선)을 기준으로 일정한 표준편차 번위 안에 드는 밴드를 설정한 그래프

- 시장의 높고 낮음 및 과매수 또는 과매도 여부를 나타냅니다.

TA-LIB을 이용한  볼린저 밴드 구하기

TA-LIB에서 볼린저 밴드를 구하는 함수 

- talib.BBANDS()

upperband, middleband, lowerband = BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
http://mrjbq7.github.io/ta-lib/func_groups/overlap_studies.html

- talib의 BBANDS() 함수는 볼린저 밴드의 상한선 / 중간선 / 하한선에 대한 값을 반환합니다.

- 간단한 예제를 통해 볼린저 밴드값을 구해보고 차트를 그려보겠습니다.

 

예제

- 사용한 데이터는 종목코드 - 000020 입니다. 

- 최근 기간중 주말과 휴장일을 제외한 100일간의 데이터입니다.

- 간단한 봉 차트를 그려보겠습니다.

- talib의 BBANDS() 함수를 사용해 기간은 20일로 지정하고, 볼린저 밴드 값을 구합니다.

df['BBAND_UPPER'],df['BBAND_MIDDLE'],df['BBAND_LOWER'] = ta.BBANDS(df['close'],20,2)

- 대신증권 HTS 에서 그려진 볼린저 밴드와 비교해 보면 똑같이 그려졌네요.

 

728x90
728x90

넘파이 (Numpy) Numpy.c_

- Translates slice objects to concatenation along the second axis.

- 2개의 배열을 붙여 2차원 배열로 만들기

 

>>> np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])

 

https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.c_.html?highlight=numpy%20c_#numpy.c_
728x90
728x90
728x90

+ Recent posts