Python
-
[NumPy] Problem setRoad to Data Analyst/Python 2022. 6. 13. 12:32
22. Normalize a 5x5 random matrix (★☆☆) a = np.random.random((5,5)) b = (a - np.mean(a)) / (np.std(a)) print(a) print(b) Normalize (표준화) : X'= (X-μ) / σ - mean값과 standard deviation값은 np.mean(해당 변수) np.std(해당 변수)에 저장 가능하다. # 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆) print(np.dot(np.ones((5,3)), np.ones((3,2)))) - array끼리의 곱을 하고 싶을 때 np.dot() 사용하기 # 25. Given a 1D array..
-
[NumPy] Fancy Indexing 팬시 인덱싱Road to Data Analyst/Python 2022. 6. 13. 09:42
import numpy as np a1 = np.array([1,2,3,4,5]) print(a1) print(a1[0], a1[2]) ind = [0,2] print(a1[ind]) #한번에 묶어서 ind = np.array([[0,1], [2,0]]) print(a1[ind]) #1차원 배역이지만, 2차원 인덱스에 영향을 받아 2차원으로 결과 출력 1. print(a1[0], a1[2])와 같이 특정 위치를 지정하여 값을 받을 수 있다. 2. 또는 ind = [0, 2]와 같이 한번에 묶어서 값을 받을 수 있다. 3. 위에서도 주석으로 남겨 두었듯이 인덱스가 2차원 배열처럼 값을 알고자 하는 배열과 다른 차원일 때 : 인덱스의 영향을 받아 인덱스 배열 차원으로 변경된다. (여기서는 인덱스 배열 : 2..
-
[Project Euler] Q7. 10001st primeRoad to Data Analyst/Python 2022. 6. 13. 09:18
Q7 : By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? 구해야 할것 - prime number들을 구별해주는 부분 - prime number를 찾으면 count하여 10001번째까지 세주는 조건 코드 i = 0 n = 3 count = 1 check = 0 while True: for i in range(2, n): if n % i == 0: check = 1 break if check == 0: count += 1 if count == 10001: print(n) break n += 1 check = 0 (1)..
-
[Project Euler] Q6. Sum square differenceRoad to Data Analyst/Python 2022. 6. 8. 08:53
Q6 : Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. 구해야 할 것 - 반복문을 통한 각 숫자의 (1~100) 제곱의 합, 1~100을 더한 값의 제곱 코드 add = 0 sq = 0 for n in range(1,101): add += n**2 sq += n print(sq**2 - add)
-
[Project Euler] Q5. Smallest multipleRoad to Data Analyst/Python 2022. 6. 6. 09:37
Q5 : 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 처음 아이디어 - 조건문을 통한 1~20 나머지 (%)가 0인 숫자 찾기 (1~20에서 소수만 조건문에 포함시키면 될 것 같다.) - 반복문을 통한 숫자 계속 증가 (20부터), 조건문에 맞는 값 나오면 탈출 그 다음 아이디어 - 파이썬에 있는 math함수를 활용하여, 1~20사이에 있는 모든 수의 최소공배수를 구하면 될 것 같다는 생..
-
[Project Euler] Q4 : Largest palindrome productRoad to Data Analyst/Python 2022. 6. 4. 08:51
Q4 : A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. 문제 : 세자리 숫자를 곱하여 나오는 대칭수 중 최댓값을 찾아야 함. (1) 최소 다섯자리 (100 * 100 = 10,000) 부터 여섯자리 (999 * 999 = 998,001) 중에서 대칭수를 찾고 (2) 그 중 최댓값을 구해야 함 (1) 최소 다섯자리 (100 * 100 = 10,000) 부터 여섯자리 (999 * 999 ..
-
[NumPy] 배열 값 삽입/수정/삭제/복사Road to Data Analyst/Python 2022. 5. 31. 00:19
배열 값 삽입 : insert() 1. 코드 import numpy as np a2 = np.array([[1,2,3], [4,5,6], [7,8,9]]) print(a2) b2 = np.insert(a2, 1, 10, axis=0) print(b2) c2 = np.insert(a2, 1, 10, axis=1) print(c2) 2. 결과 - axis 값을 입력하지 않을 경우, axis = 0으로 인식, 1차원 배열로 출력한다 - 특정 row/column/depth 전체에 값을 삽입하는 것으로 생각하면 된다. 배열 값 수정 - fancy indexing을 활용하자 - 2차원 배열인 a2를 'a2[0]=1'와 같이 작성할 경우 0은 0번째 row의 값을 모두 1로 변경한다. - 원본에는 영향 X 배열 값 ..
-
[NumPy] 불리언 인덱싱(Boolean Indexing)Road to Data Analyst/Python 2022. 5. 27. 12:10
불리언 인덱싱 : True인 값만 조회하는 것 1. 코드 import numpy as np a = np.array([[1,2,3], [4,5,6], [7,8,9]]) bi = np.random.randint(0,2, (3,3), dtype=bool) print(a) print(bi) print(a[bi]) 2. 결과 (1) 우선 numpy를 불러온다. (2) 그리고 적당한 size의 array를 만든다. (위의 경우 (3,3)) (3) True, False 형태, (3,3) 사이즈의 배열을 하나 더 만든다. (4) print(a[bi]) : 배열 각 요소에 True인 부분 값만 출력한다. * 활용 : 특정 조건에 맞추어 결과를 필터링할 때 활용된다. 출처 : 이수안 컴퓨터 연구소