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