1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "name" : " Numpy의 연산과 함수" ,
7
+ "version" : " 0.3.2" ,
8
+ "provenance" : [],
9
+ "include_colab_link" : true
10
+ },
11
+ "kernelspec" : {
12
+ "name" : " python3" ,
13
+ "display_name" : " Python 3"
14
+ }
15
+ },
16
+ "cells" : [
17
+ {
18
+ "cell_type" : " markdown" ,
19
+ "metadata" : {
20
+ "id" : " view-in-github" ,
21
+ "colab_type" : " text"
22
+ },
23
+ "source" : [
24
+ " <a href=\" https://colab.research.google.com/github/ndb796/Python-Data-Analysis-and-Image-Processing-Tutorial/blob/master/04.%20Numpy%EC%9D%98%20%EC%97%B0%EC%82%B0%EA%B3%BC%20%ED%95%A8%EC%88%98/Numpy%EC%9D%98%20%EC%97%B0%EC%82%B0%EA%B3%BC%20%ED%95%A8%EC%88%98.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type" : " markdown" ,
29
+ "metadata" : {
30
+ "id" : " jflbKZvWIewX" ,
31
+ "colab_type" : " text"
32
+ },
33
+ "source" : [
34
+ " ## Numpy의 기본 사용법\n " ,
35
+ " [강의 노트](https://github.com/ndb796/Python-Data-Analysis-and-Image-Processing-Tutorial/blob/master/04.%20Numpy%EC%9D%98%20%EC%97%B0%EC%82%B0%EA%B3%BC%20%ED%95%A8%EC%88%98/Python%20%EB%8D%B0%EC%9D%B4%ED%84%B0%20%EB%B6%84%EC%84%9D%EA%B3%BC%20%EC%9D%B4%EB%AF%B8%EC%A7%80%20%EC%B2%98%EB%A6%AC%20-%20Numpy%EC%9D%98%20%EC%97%B0%EC%82%B0%EA%B3%BC%20%ED%95%A8%EC%88%98.pdf)"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type" : " markdown" ,
40
+ "metadata" : {
41
+ "id" : " YMGeT2gmIqQO" ,
42
+ "colab_type" : " text"
43
+ },
44
+ "source" : [
45
+ " Python의 Numpy 라이브러리는 기본적인 상수 연산이 가능합니다."
46
+ ]
47
+ },
48
+ {
49
+ "cell_type" : " code" ,
50
+ "metadata" : {
51
+ "id" : " lB3TNlINIn33" ,
52
+ "colab_type" : " code" ,
53
+ "colab" : {
54
+ "base_uri" : " https://localhost:8080/" ,
55
+ "height" : 52
56
+ },
57
+ "outputId" : " 9293f423-94d0-4f3d-f4fa-3f1a5850de8c"
58
+ },
59
+ "source" : [
60
+ " import numpy as np\n " ,
61
+ " \n " ,
62
+ " array = np.random.randint(1, 10, size=4).reshape(2, 2)\n " ,
63
+ " result_array = array * 10\n " ,
64
+ " print(result_array)"
65
+ ],
66
+ "execution_count" : 1 ,
67
+ "outputs" : [
68
+ {
69
+ "output_type" : " stream" ,
70
+ "text" : [
71
+ " [[10 30]\n " ,
72
+ " [40 80]]\n "
73
+ ],
74
+ "name" : " stdout"
75
+ }
76
+ ]
77
+ },
78
+ {
79
+ "cell_type" : " markdown" ,
80
+ "metadata" : {
81
+ "id" : " 3gXaKkCGI5lG" ,
82
+ "colab_type" : " text"
83
+ },
84
+ "source" : [
85
+ " **서로 다른 형태의 Numpy 연산**이 가능합니다."
86
+ ]
87
+ },
88
+ {
89
+ "cell_type" : " code" ,
90
+ "metadata" : {
91
+ "id" : " CjFI4tuPI1M2" ,
92
+ "colab_type" : " code" ,
93
+ "colab" : {
94
+ "base_uri" : " https://localhost:8080/" ,
95
+ "height" : 52
96
+ },
97
+ "outputId" : " 74cb0c4d-0e91-4d28-99d7-1b080dfa69be"
98
+ },
99
+ "source" : [
100
+ " import numpy as np\n " ,
101
+ " \n " ,
102
+ " array1 = np.arange(4).reshape(2, 2)\n " ,
103
+ " array2 = np.arange(2)\n " ,
104
+ " array3 = array1 + array2\n " ,
105
+ " \n " ,
106
+ " print(array3)"
107
+ ],
108
+ "execution_count" : 2 ,
109
+ "outputs" : [
110
+ {
111
+ "output_type" : " stream" ,
112
+ "text" : [
113
+ " [[0 2]\n " ,
114
+ " [2 4]]\n "
115
+ ],
116
+ "name" : " stdout"
117
+ }
118
+ ]
119
+ },
120
+ {
121
+ "cell_type" : " code" ,
122
+ "metadata" : {
123
+ "id" : " yoaNGmROI8Fk" ,
124
+ "colab_type" : " code" ,
125
+ "colab" : {
126
+ "base_uri" : " https://localhost:8080/" ,
127
+ "height" : 87
128
+ },
129
+ "outputId" : " d88c62ad-e4ef-49aa-b40b-fbe9a9888ba6"
130
+ },
131
+ "source" : [
132
+ " import numpy as np\n " ,
133
+ " \n " ,
134
+ " array1 = np.arange(0, 8).reshape(2, 4)\n " ,
135
+ " array2 = np.arange(0, 8).reshape(2, 4)\n " ,
136
+ " array3 = np.concatenate([array1, array2], axis=0)\n " ,
137
+ " array4 = np.arange(0, 4).reshape(4, 1)\n " ,
138
+ " \n " ,
139
+ " print(array3 + array4)"
140
+ ],
141
+ "execution_count" : 3 ,
142
+ "outputs" : [
143
+ {
144
+ "output_type" : " stream" ,
145
+ "text" : [
146
+ " [[ 0 1 2 3]\n " ,
147
+ " [ 5 6 7 8]\n " ,
148
+ " [ 2 3 4 5]\n " ,
149
+ " [ 7 8 9 10]]\n "
150
+ ],
151
+ "name" : " stdout"
152
+ }
153
+ ]
154
+ },
155
+ {
156
+ "cell_type" : " markdown" ,
157
+ "metadata" : {
158
+ "id" : " HR-bcngHJbo3" ,
159
+ "colab_type" : " text"
160
+ },
161
+ "source" : [
162
+ " Numpy의 **마스킹 연산**이 가능합니다."
163
+ ]
164
+ },
165
+ {
166
+ "cell_type" : " code" ,
167
+ "metadata" : {
168
+ "id" : " 5XWPfcCEJK17" ,
169
+ "colab_type" : " code" ,
170
+ "colab" : {
171
+ "base_uri" : " https://localhost:8080/" ,
172
+ "height" : 228
173
+ },
174
+ "outputId" : " 499685e3-925b-428f-d922-63eab1ef1d82"
175
+ },
176
+ "source" : [
177
+ " import numpy as np\n " ,
178
+ " \n " ,
179
+ " # Numpy 원소의 값을 조건에 따라 바꿀 때는 다음과 같이 합니다.\n " ,
180
+ " # 반복문을 이용할 때보다 매우 빠르게 동작합니다.\n " ,
181
+ " # 대체로 이미지 처리(Image Processing)에서 자주 활용됩니다.\n " ,
182
+ " array1 = np.arange(16).reshape(4, 4)\n " ,
183
+ " print(array1)\n " ,
184
+ " \n " ,
185
+ " array2 = array1 < 10\n " ,
186
+ " print(array2)\n " ,
187
+ " \n " ,
188
+ " array1[array2] = 100\n " ,
189
+ " print(array1)"
190
+ ],
191
+ "execution_count" : 4 ,
192
+ "outputs" : [
193
+ {
194
+ "output_type" : " stream" ,
195
+ "text" : [
196
+ " [[ 0 1 2 3]\n " ,
197
+ " [ 4 5 6 7]\n " ,
198
+ " [ 8 9 10 11]\n " ,
199
+ " [12 13 14 15]]\n " ,
200
+ " [[ True True True True]\n " ,
201
+ " [ True True True True]\n " ,
202
+ " [ True True False False]\n " ,
203
+ " [False False False False]]\n " ,
204
+ " [[100 100 100 100]\n " ,
205
+ " [100 100 100 100]\n " ,
206
+ " [100 100 10 11]\n " ,
207
+ " [ 12 13 14 15]]\n "
208
+ ],
209
+ "name" : " stdout"
210
+ }
211
+ ]
212
+ },
213
+ {
214
+ "cell_type" : " markdown" ,
215
+ "metadata" : {
216
+ "id" : " NznH01UXJnpn" ,
217
+ "colab_type" : " text"
218
+ },
219
+ "source" : [
220
+ " Numpy는 **다양한 집계 함수**가 존재합니다."
221
+ ]
222
+ },
223
+ {
224
+ "cell_type" : " code" ,
225
+ "metadata" : {
226
+ "id" : " iPhu1aBhJR8O" ,
227
+ "colab_type" : " code" ,
228
+ "colab" : {
229
+ "base_uri" : " https://localhost:8080/" ,
230
+ "height" : 87
231
+ },
232
+ "outputId" : " 59d8968e-25cd-4828-c615-098b558c1f00"
233
+ },
234
+ "source" : [
235
+ " import numpy as np\n " ,
236
+ " \n " ,
237
+ " array = np.arange(16).reshape(4, 4)\n " ,
238
+ " \n " ,
239
+ " print(\" 최대값:\" , np.max(array))\n " ,
240
+ " print(\" 최소값:\" , np.min(array))\n " ,
241
+ " print(\" 합계:\" , np.sum(array))\n " ,
242
+ " print(\" 평균값:\" , np.mean(array))"
243
+ ],
244
+ "execution_count" : 5 ,
245
+ "outputs" : [
246
+ {
247
+ "output_type" : " stream" ,
248
+ "text" : [
249
+ " 최대값: 15\n " ,
250
+ " 최소값: 0\n " ,
251
+ " 합계: 120\n " ,
252
+ " 평균값: 7.5\n "
253
+ ],
254
+ "name" : " stdout"
255
+ }
256
+ ]
257
+ },
258
+ {
259
+ "cell_type" : " code" ,
260
+ "metadata" : {
261
+ "id" : " 2AQxs__jJP-L" ,
262
+ "colab_type" : " code" ,
263
+ "colab" : {
264
+ "base_uri" : " https://localhost:8080/" ,
265
+ "height" : 105
266
+ },
267
+ "outputId" : " 3446db66-a125-4f7e-c832-e1ecc181a17d"
268
+ },
269
+ "source" : [
270
+ " import numpy as np\n " ,
271
+ " \n " ,
272
+ " array = np.arange(16).reshape(4, 4)\n " ,
273
+ " \n " ,
274
+ " print(array)\n " ,
275
+ " print(\" 합계:\" , np.sum(array, axis=0))"
276
+ ],
277
+ "execution_count" : 6 ,
278
+ "outputs" : [
279
+ {
280
+ "output_type" : " stream" ,
281
+ "text" : [
282
+ " [[ 0 1 2 3]\n " ,
283
+ " [ 4 5 6 7]\n " ,
284
+ " [ 8 9 10 11]\n " ,
285
+ " [12 13 14 15]]\n " ,
286
+ " 합계: [24 28 32 36]\n "
287
+ ],
288
+ "name" : " stdout"
289
+ }
290
+ ]
291
+ }
292
+ ]
293
+ }
0 commit comments