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/05.%20Numpy%EC%9D%98%20%ED%99%9C%EC%9A%A9/Numpy%EC%9D%98%20%ED%99%9C%EC%9A%A9.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" : " mLSxrQLtL1lk" ,
31
+ "colab_type" : " text"
32
+ },
33
+ "source" : [
34
+ " ## Numpy의 활용\n " ,
35
+ " [강의 노트](https://github.com/ndb796/Python-Data-Analysis-and-Image-Processing-Tutorial/blob/master/05.%20Numpy%EC%9D%98%20%ED%99%9C%EC%9A%A9/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%ED%99%9C%EC%9A%A9.pdf)"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type" : " markdown" ,
40
+ "metadata" : {
41
+ "id" : " 4QwfuGHNL7qv" ,
42
+ "colab_type" : " text"
43
+ },
44
+ "source" : [
45
+ " Python의 Numpy는 **저장 및 불러오기**가 가능합니다."
46
+ ]
47
+ },
48
+ {
49
+ "cell_type" : " code" ,
50
+ "metadata" : {
51
+ "id" : " cJ9HX7GtL6tT" ,
52
+ "colab_type" : " code" ,
53
+ "colab" : {
54
+ "base_uri" : " https://localhost:8080/" ,
55
+ "height" : 34
56
+ },
57
+ "outputId" : " 4bb17042-2e41-469c-ae45-1cda452a45a4"
58
+ },
59
+ "source" : [
60
+ " import numpy as np\n " ,
61
+ " \n " ,
62
+ " # 단일 객체 저장 및 불러오기\n " ,
63
+ " array = np.arange(0, 10)\n " ,
64
+ " np.save('saved.npy', array)\n " ,
65
+ " \n " ,
66
+ " result = np.load('saved.npy')\n " ,
67
+ " print(result)"
68
+ ],
69
+ "execution_count" : 1 ,
70
+ "outputs" : [
71
+ {
72
+ "output_type" : " stream" ,
73
+ "text" : [
74
+ " [0 1 2 3 4 5 6 7 8 9]\n "
75
+ ],
76
+ "name" : " stdout"
77
+ }
78
+ ]
79
+ },
80
+ {
81
+ "cell_type" : " markdown" ,
82
+ "metadata" : {
83
+ "id" : " 1Bq-T643MGXP" ,
84
+ "colab_type" : " text"
85
+ },
86
+ "source" : [
87
+ " 여러 개의 Numpy의 객체를 하나의 파일에 저장하고 불러올 수 있습니다."
88
+ ]
89
+ },
90
+ {
91
+ "cell_type" : " code" ,
92
+ "metadata" : {
93
+ "id" : " nmqFl9ykMC5J" ,
94
+ "colab_type" : " code" ,
95
+ "colab" : {
96
+ "base_uri" : " https://localhost:8080/" ,
97
+ "height" : 52
98
+ },
99
+ "outputId" : " d2a60f6d-85ce-4fcf-8495-80cdaf5cb419"
100
+ },
101
+ "source" : [
102
+ " import numpy as np\n " ,
103
+ " \n " ,
104
+ " # 복수 객체 저장 및 불러오기\n " ,
105
+ " array1 = np.arange(0, 10)\n " ,
106
+ " array2 = np.arange(10, 20)\n " ,
107
+ " np.savez('saved.npz', array1=array1, array2=array2)\n " ,
108
+ " \n " ,
109
+ " data = np.load('saved.npz')\n " ,
110
+ " result1 = data['array1']\n " ,
111
+ " result2 = data['array2']\n " ,
112
+ " print(result1)\n " ,
113
+ " print(result2)"
114
+ ],
115
+ "execution_count" : 3 ,
116
+ "outputs" : [
117
+ {
118
+ "output_type" : " stream" ,
119
+ "text" : [
120
+ " [0 1 2 3 4 5 6 7 8 9]\n " ,
121
+ " [10 11 12 13 14 15 16 17 18 19]\n "
122
+ ],
123
+ "name" : " stdout"
124
+ }
125
+ ]
126
+ },
127
+ {
128
+ "cell_type" : " markdown" ,
129
+ "metadata" : {
130
+ "id" : " 7v2Gr9_xMaCg" ,
131
+ "colab_type" : " text"
132
+ },
133
+ "source" : [
134
+ " Numpy의 원소들은 특정한 기준에 따라서 **정렬**할 수 있습니다."
135
+ ]
136
+ },
137
+ {
138
+ "cell_type" : " code" ,
139
+ "metadata" : {
140
+ "id" : " nJ2GKQqMMPhA" ,
141
+ "colab_type" : " code" ,
142
+ "colab" : {
143
+ "base_uri" : " https://localhost:8080/" ,
144
+ "height" : 87
145
+ },
146
+ "outputId" : " 5ae93570-2cc5-42de-a45d-f0f93147e69d"
147
+ },
148
+ "source" : [
149
+ " import numpy as np\n " ,
150
+ " \n " ,
151
+ " # Numpy 원소 오름차순 정렬\n " ,
152
+ " array = np.array([5, 9, 10, 3, 1])\n " ,
153
+ " array.sort()\n " ,
154
+ " print(array)\n " ,
155
+ " \n " ,
156
+ " # Numpy 원소 내림차순 정렬\n " ,
157
+ " array = np.array([5, 9, 10, 3, 1])\n " ,
158
+ " array.sort()\n " ,
159
+ " print(array[::-1])\n " ,
160
+ " \n " ,
161
+ " # 각 열을 기준으로 정렬\n " ,
162
+ " array = np.array([[5, 9, 10, 3, 1], [8, 3, 4, 2, 5]])\n " ,
163
+ " array.sort(axis=0)\n " ,
164
+ " print(array)"
165
+ ],
166
+ "execution_count" : 4 ,
167
+ "outputs" : [
168
+ {
169
+ "output_type" : " stream" ,
170
+ "text" : [
171
+ " [ 1 3 5 9 10]\n " ,
172
+ " [10 9 5 3 1]\n " ,
173
+ " [[ 5 3 4 2 1]\n " ,
174
+ " [ 8 9 10 3 5]]\n "
175
+ ],
176
+ "name" : " stdout"
177
+ }
178
+ ]
179
+ },
180
+ {
181
+ "cell_type" : " markdown" ,
182
+ "metadata" : {
183
+ "id" : " XAk0Jg_yMo44" ,
184
+ "colab_type" : " text"
185
+ },
186
+ "source" : [
187
+ " Numpy 관련 자주 사용되는 함수는 다음과 같습니다."
188
+ ]
189
+ },
190
+ {
191
+ "cell_type" : " code" ,
192
+ "metadata" : {
193
+ "id" : " 6_bsO1aEMg8k" ,
194
+ "colab_type" : " code" ,
195
+ "colab" : {
196
+ "base_uri" : " https://localhost:8080/" ,
197
+ "height" : 105
198
+ },
199
+ "outputId" : " 46fc3e62-1478-40ce-acbe-a33f4f0b763c"
200
+ },
201
+ "source" : [
202
+ " import numpy as np\n " ,
203
+ " \n " ,
204
+ " # 균일한 간격으로 데이터 생성\n " ,
205
+ " array = np.linspace(0, 10, 5)\n " ,
206
+ " print(array)\n " ,
207
+ " \n " ,
208
+ " # 난수의 재연(실행마다 결과 동일)\n " ,
209
+ " np.random.seed(7)\n " ,
210
+ " print(np.random.randint(0, 10, (2, 3)))\n " ,
211
+ " \n " ,
212
+ " # Numpy 배열 객체 복사\n " ,
213
+ " array1 = np.arange(0, 10)\n " ,
214
+ " array2 = array1.copy()\n " ,
215
+ " print(array2)\n " ,
216
+ " \n " ,
217
+ " # 중복된 원소 제거\n " ,
218
+ " array = np.array([1, 1, 2, 3, 3, 3, 1])\n " ,
219
+ " print(np.unique(array))"
220
+ ],
221
+ "execution_count" : 6 ,
222
+ "outputs" : [
223
+ {
224
+ "output_type" : " stream" ,
225
+ "text" : [
226
+ " [ 0. 2.5 5. 7.5 10. ]\n " ,
227
+ " [[4 9 6]\n " ,
228
+ " [3 3 7]]\n " ,
229
+ " [0 1 2 3 4 5 6 7 8 9]\n " ,
230
+ " [1 2 3]\n "
231
+ ],
232
+ "name" : " stdout"
233
+ }
234
+ ]
235
+ }
236
+ ]
237
+ }
0 commit comments