diff --git "a/1\354\243\274\354\260\250.md" "b/1\354\243\274\354\260\250.md" new file mode 100644 index 0000000..a8300b1 --- /dev/null +++ "b/1\354\243\274\354\260\250.md" @@ -0,0 +1,412 @@ +# 01. 행렬의 필요성 +## 1) 행렬의 필요성 +- 현실 세계의 많은 문제를 쉽게 해결 가능 +- 복잡한 연립 방정식의 해를 구할 수 있음 + +## 2) 행렬의 쓰임 예시 +- 컴퓨터의 메모리 구조 표현 +- 표 형태의 데이터 표현 +- 이미지 표현 (cf. 각각의 픽셀을 행렬로 표현) + + +cf. + - 1차원 배열 : 백터 + - 2차원 배열 : 행렬 + - 3차원 배열 : 텐서 + +cf. + - 더 자세한 내용은 선형대수학 공부 + - 인공지능, 머신러닝은 행렬 필수적 +
+import numpy as np
+
+list_data = [1, 2, 3]
+array = np.array(list_data)
+
+print(array.size) #3
+print(array.dtype) #int 64
+print(array[1]) #2
+
+#### 나) Numpy 배열 초기화
+
+import numpy as np
+
+array1 = np.arange(4) #0~3배열
+array2 = np.zeros((4, 4), dtype=float) #0으로 초기화
+array3 = np.ones((3, 3), dtype=str) #1로 초기화
+array4 = np.random.randint(0, 10, (3, 3)) #0~9 랜덤 초기화
+array5 = np.random.normal(0, 1, (3, 3)) #평균 0, 표준편차 1인 표준 정규 배열
+
+print(array1) #[0 1 2 3]
+print(array2) #[[0. 0. 0. 0.]]
+print(array3) #[['1' '1' '1' '1']]
+print(array4) #[[9 3 8 8]]
+print(array5) #[[-0.684376 1.74030087 0.2387887 -0.03009152]]
+
+#### 다) Numpy 배열 형태 바꾸기 (reshape)
+
+import numpy as np
+
+array1 = np.array([1, 2, 3, 4]) #1차원 배열
+array2 = array1.reshape((2, 2)) #2차원 배열
+
+print(array1) #[1 2 3 4]
+print(array2) #[[1 2]
+ # [3 4]]
+
+#### 라) Numpy 배열 (가로축으로)합치기 (concatenate)
+
+import numpy as np
+
+array1 = np.array([1, 2, 3])
+array2 = np.array([4, 5, 6])
+array3 = np.concatenate([array1, array2])
+
+print(array3.shape) #6
+print(array3) #[1 2 3 4 5 6]
+
+#### 마) Numpy 배열 세로축으로 합치기
+
+import numpy as np
+
+array1 = np.arange(4).reshape(1, 4)
+array2 = np.arange(8).reshape(2, 4)
+array3 = np.concatenate([array1, array2], axis=0) #axis=0을 기준으로 합쳐짐.
+
+print(array3.shape) #(3, 4)
+print(array3) #[[0 1 2 3]
+ # [0 1 2 3]
+ # [4 5 6 7]]
+
+#### 바) Numpy 배열 나누기
+
+import numpy as np
+
+array1 = np.arange(8).reshape(2, 4)
+left, right = np.split(array1, [2], axis=1) #인덱스 2의 axis=1(col)을 기준으로 나눈다.
+
+print(f'{left.shape} & {right.shape}') #(2, 2) & (2, 2)
+print(f'{left[0]} & {right[0]}') #[0 1] & [2 3]
+print(f'{left[1]} & {right[1]}') #[4 5] & [6 7]
+
+
+import numpy as np
+
+array = np.random.randint(1, 10, size=4).reshape(2, 2)
+add_array = array+10
+
+print(f'{array[0]} => {add_array[0]}') #[1 5] => [11 15]
+print(f'{array[1]} {add_array[1]}') #[9 6] [19 16]
+
+#### 나) 곱하기
+- 배열(백터) + 숫자(스칼라) : 각각의 원소에 곱하기 수행
+
+import numpy as np
+
+array = np.random.randint(1, 10, size=4).reshape(2, 2)
+mul_array = array*10
+
+print(f'{array[0]} => {mul_array[0]}') #[5 2] => [50 20]
+print(f'{array[1]} {mul_array[1]}') #[2 6] [20 60]
+
+
+## 3) Numpy의 서로 다른 형태의 연산 => 브로드캐스트
+- 브로드캐스트 : 형태가 다른 배열을 연산할 수 있도록 배열의 형태를 동적으로 변환
+
+
+import numpy as np
+
+array1 = np.arange(0,4).reshape(2, 2)
+array2 = np.arange(0,2).reshape(2, 1)
+
+print(f'{array1[0]} + {array2[0]} = {array1[0]+array2[0]}') #[0 1] + [0] = [0 1]
+print(f'{array1[1]} {array2[1]} = {array1[1]+array2[1]}') #[2 3] [1] = [3 4]
+
+
+## 4) Numpy의 마스킹 연산
+- 마스킹 : 각 원소에 대하여 체크
+- 반복문을 이용할 때보다 매우 빠르게 동작
+- 이미지 처리(Image Processing) or 데이터 분석을 할 때 많이 사용
+ ex. 색상이 매우 밝은 픽셀 값만 뽑아내서 다르게 바꿀 수 있음.
+
+
+import numpy as np
+
+array = np.arange(16).reshape(4, 4)
+array1 = array < 10
+array2 = np.arange(16).reshape(4, 4)
+array2[array1]=100
+
+print(f'{array[0]}=>{array1[0]}=>{array2[0]}') #[0 1 2 3] => [ True True True True]=>[100 100 100 100]
+print(f'{array[1]} {array1[1]} {array2[1]}') #[4 5 6 7] [ True True True True] [100 100 100 100]
+print(f'{array[2]} {array1[2]} {array2[2]}') #[ 8 9 10 11] [ True True False False] [100 100 10 11]
+print(f'{array[3]} {array1[3]} {array2[3]}') #[12 13 14 15] [False False False False] [12 13 14 15]
+
+
+## 5) Numpy의 집계 함수
+
+import numpy as np
+
+array = np.arange(16).reshape(4, 4)
+
+print(f'최대값 : {np.max(array)}') #최대값 : 15
+print(f'최소값 : {np.min(array)}') #최소값 : 0
+print(f'합계 : {np.sum(array)}') #합계 : 120 #np.sum(array, axis=0):열에 대해 더함
+print(f'평균값 : {np.mean(array)}') #평균값 : 7.5
+
+
+import numpy as np
+
+array = np.arange(0,10)
+np.save('saved.npy', array)
+
+file = np.load('saved.npy')
+
+print(file) #[0 1 2 3 4 5 6 7 8 9]
+
+#### 나) 복수 객체 저장 및 불러오기 : np.savez & .npz
+
+import numpy as np
+
+array1 = np.arange(0, 10)
+array2 = np.arange(10, 20)
+np.savez('saved.npz', file1=array1, file2=array2)
+
+data = np.load('saved.npz')
+file1 = data['file1']
+file2 = data['file2']
+
+print(file1) #[0 1 2 3 4 5 6 7 8 9]
+print(file2) #[10 11 12 13 14 15 16 17 18 19]
+
+
+## 2) Numpy 원소의 정렬
+- 오름차순, 내림차순, 각 열을 기준으로 정렬
+
+import numpy as np
+
+array = np.array([5, 9, 10, 3, 1])
+array.sort()
+array1 = np.array([[5, 9, 10, 3, 1], [8, 3, 4, 2, 5]])
+array1.sort(axis=0)
+
+print(f'기본배열 : {array}') #기본배열 : [ 1 3 5 9 10]
+print(f'오름차순 : {array}') #오름차순 : [ 1 3 5 9 10]
+print(f'내림차순 : {array[::-1]}') #내림차순 : [10 9 5 3 1]
+print(f'각 열을 기준으로 정렬 : {array1[0]}') #각 열을 기준으로 정렬 : [5 3 4 2 1]
+print(f'{array1[1]}') # [8 9 10 3 5]
+
+
+## 3) 자주 사용되는 함수
+- 균일한 간격으로 데이터 생성 (np.linspace)
+- 난수 (np.random.randint)
+- Numpy 배열 객체 복사(copy)
+ cf) 'array1 = array2' 형식으로 복사를 하게되면, array2 수정 시 array1도 바뀜.
+- 중복된 원소 제거
+
+import numpy as np
+
+array = np.array([1, 2, 2, 2, 3, 3])
+균일_array = np.linspace(0, 10, 5)
+난수_array = np.random.randint(0, 10, (2, 2)) #실행마다 결과 동일하게 하려면 seed() 함수 사용.
+복사_array = 난수_array.copy()
+중복제거_array = np.unique(array)
+
+print(f'{균일_array}') #[ 0. 2.5 5. 7.5 10. ]
+print(f'{난수_array}') #[[8 8] [7 5]]
+print(f'{복사_array}') #[[8 8] [7 5]]
+print(f'{중복제거_array}') #[1 2 3]
+
+
+import matplotlib.pyplot as plt
+
+x = [1, 2, 3] #x=1일 때, y=1 ...
+y = [1, 2, 3]
+plt.plot(x, y)
+plt.title("Simple Line")
+plt.xlabel("X")
+plt.ylabel("Y")
+plt.show()
+
+
+
+## 3) 그래프 저장 (savefig)
+- 이미지 형식으로 저장 (.png)
+
+import matplotlib.pyplot as plt
+
+x = [1, 2, 3]
+y = [1, 2, 3]
+plt.plot(x, y)
+plt.title("Simple Line")
+plt.xlabel("X")
+plt.ylabel("Y")
+plt.savefig('picture.png')
+
+
+
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.linspace(0, np.pi*10, 500) #pi*10 너비에 500개의 점을 균일하게 찍기기
+fig, axes = plt.subplots(2, 1) #2개의 그래프가 들어가는 Figure 생성 (2x1)
+axes[0].plot(x, np.sin(x)) #첫 번째 그래프는 사인(sin) 그래프
+axes[1].plot(x, np.cos(x)) #두 번째 그래프는 코사인(cos) 그래프프
+fig.savefig('sin&cos.png')
+
+
+
+
+## 4) 선 그래프
+- 라인 스타일로는 '-', ':', '-.', '--' 등이 사용될 수 있음.
+- legend : 레이블에 대한 정보
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.arange(-9, 10)
+y = x**2
+plt.plot(x, y, linestyle=":", marker="*")
+plt.show()
+
+
+
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.arange(-9, 10)
+y1 = x**2
+y2 = -x
+plt.plot(x, y1, linestyle="-.", marker="*", color="red", label="y = x * x")
+plt.plot(x, y2, linestyle=":", marker="o", color="blue", label="y = -x")
+plt.xlabel("X")
+plt.ylabel("Y")
+plt.legend(
+ shadow=True,
+ borderpad=1
+)
+plt.show()
+
+
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.arange(-9, 10)
+y1 = x**2
+plt.plot(
+ x, y1,
+ linestyle=":",
+ marker="o",
+ markersize=8,
+ markerfacecolor="blue",
+ markeredgecolor="red"
+)
+plt.show()
+
+
+
+## 2) 막대 그래프 그리기 ( bar 함수 )
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.arange(-9, 10)
+plt.bar(x, x**2)
+plt.show()
+
+
+
+## 3) 누적 막대 그래프 그리기
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.random.rand(10) #아래 막대
+y = np.random.rand(10) #중간 막대
+z = np.random.rand(10) #위 막대
+data = [x, y, z]
+x_array = np.arange(10)
+for i in range(0, 3): #누적 막대의 종류가 3개
+ plt.bar(
+ x_array, #0부터 10까지의 x위치에서
+ data[i], #각 높이(10개)만큼 쌓음
+ bottom = np.sum(data[:i], axis=0) #끝난 시점으로 쌓음
+ )
+plt.show()
+
+
+
+## 4) 스캐터 그래프 그리기
+- 공간 데이터의 *분포or분산*에 적합
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+x = np.random.rand(10) #10개의 데이터
+y = np.random.rand(10)
+colors = np.random.randint(0, 100, 10)
+sizes = np.pi*1000*np.random.rand(10)
+plt.scatter(x, y, c = colors, s = sizes, alpha = 0.5)
+plt.show()
+
+
diff --git "a/2\354\243\274\354\260\250.md" "b/2\354\243\274\354\260\250.md"
new file mode 100644
index 0000000..afa2a75
--- /dev/null
+++ "b/2\354\243\274\354\260\250.md"
@@ -0,0 +1,19 @@
+# 머신러닝 도입
+## 인공지능 소개
+### 01. 인공지능이란 무엇인가 ?
+Haugeland : 마음을 가진 기계들