@데코레이터이름을 함수 위에 붙여서 사용 class MyEST:
count = 3700
#정적메소드는 self파라메타를 사용안함, 클래스 자신접근 인자 기술시 에러
@staticmethod
def isSquare(w, h):
area = w * h
return area
@classmethod
def printCount(self):
print(f'갯수는 = {self.count}')
# 데코레이션 사용시
hap = MyEST.isSquare(5, 5)
print(f'hap는 = {hap}')
MyEST.printCount()
@Staticmethod는 클래스 변수에 접근 하지 않는 독립적인 함수
import time
def check_time(function):
def measure(*mlist, **mtuple):
start_time = time.time()
result = function(*mlist, **mtuple)
end_time = time.time()
print(f"시간측정: {function.__name__}함수에서 {round( end_time - start_time ,5)}")
return result
return measure
@check_time
def myTotal(n):
total = 0
for k in range(1, n+1):
total = total + k
return total
myTotal(1000000)
print()
import numpy as np
eng = np.array( [ 7, 5, 3 ,9, 1, 2, 4, 6 ] )
print(eng)
print()
# numpy에서의 where절 -> 타 언어의 삼항 연산과 비슷함 조건 ? 참: 거짓
print(np.where(eng > 5, 1, 0))
print()
# ret
ret = np.where(eng >= 5, 1, 0)
print(ret)
print()
data = np.arange(1,11)
print(np.where(data < 5 , data, 5*data))
eng = np.array( [ 7, 5, 3 ,9, 1, 2, 4, 6 ] )
grade = np.where(eng <= 4, np.where(eng <= 7, '중급', '상급'),'초급')
print(grade)
import numpy as np
import time
score = np.array(
[ [1,2,3] ,
[4,5,6],
[7,8,9],
[10,11,12]
] )
print(score)
np.save('./data/myarray', score)
print('./data/myarray.npy 저장성공')
print()
time.sleep(1)
print(np.load('./data/myarray.npy'))
print('./data/myarray.npy 열기성공')
import numpy as np
import time
print('x = np.linspace(1, 5, 10)')
x = np.linspace(1, 5, 10)
print(x)
print()
print('y = np.linspace(1, 5, 30)')
y = np.linspace(1, 5, 30)
print(y)
print()
time.sleep(1)
print('z = np.linspace(1, 5)')
z = np.linspace(1, 5)
print(z)
print()
실행결과
import numpy as np
print('unique적용')
a = np.array( [9,5,4,3,1,2,3,4,3,2,4,1,2,3,7,3,1,2,1,2,5,1,7,1,2,2,1,3] )
ret = np.unique(a) #[1 2 3 4 5 7 9]
print(ret)
print()
print('앞뒤 zero 숫자처리')
b = np.array( [0,0,0,3,0,2,3,4,0,2,4,1,2,3,7,3,1,2,0,0,5,1,7,1,0,0,0,0] )
print(np.trim_zeros(b))
print(np.trim_zeros(b, trim = 'f'))
print(np.trim_zeros(b,trim= 'b'))
print(np.trim_zeros(b,trim= 'bf'))
print()
vstack은 수직(행 기준)으로 배열 연결hstack은 수평(열 기준)으로 배열 연결 c = np.array(([1,2,3], [7,8,9]))
print(c)
print()
print('변형\n', np.transpose(c))
print()
# c.T로도 변형 가능함
print('변형\n', c.T)
a = np.array( [ [1,2], [3,4] ] )
b = np.array( [ [5,6], [7,8] ] )
print('a\n', a)
print()
print('b\n', b)
print()
print('a 변형\n',np.transpose(a))
print()
print('b 변형\n',b.T)
[a_00, a_01] [b_00, b_01] [a_10, a_11] X [b_10, b_11] 계산 과정 [a_00 * b_00 + a_01*b_10][a_00 * b_01 + a_01*b_11] [a_10 * b_00 + a_11*b_10][a_10 * b_01 + a_11*b_11]
import numpy as np
kor = np.array([[3,2], [5,4]]) # 2*2행렬
ret = np.dot(kor,kor)
print(ret)