๐Ÿš€ Languege/Python 13

[aws python] ๋ฃจํŠธ๋ณผ๋ฅจ ๋Š˜๋ฆฌ๊ธฐ!

#!/bin/bash # Specify the desired volume size in GiB as a command-line argument. If not specified, default to 20 GiB. SIZE=${1:-20} # Get the ID of the environment host Amazon EC2 instance. INSTANCEID=$(curl http://169.254.169.254/latest/meta-data//instance-id) # Get the ID of the Amazon EBS volume associated with the instance. VOLUMEID=$(aws ec2 describe-instances \ --instance-id $INSTANCEID \ ..

Python์—์„œ Hostname ํ™•์ธํ•˜๊ธฐ

์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. import socket print(socket.gethostname()) (base) [root@ip-172-31-14-180 basicPubSub]# python3 Python 3.8.8 (default, Apr 13 2021, 19:58:26) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> print(socket.gethostname()) ip-172-31-14-180.ap-northeast-2.compute.internal

python - numpy(13) ํ†ต๊ณ„

import numpy as np print('*'*100) print('1์ฐจ์› ๋ฐฐ์—ด') print('-'*20) x = np.array([1, 2, 3, 4]) print(x) print('-'*20) #ํ•ฉ๊ณ„ print('ํ•ฉ๊ณ„:', np.sum(x)) print('ํ•ฉ๊ณ„:', x.sum()) #์ตœ์†Œ๊ฐ’, ์ตœ๋Œ€๊ฐ’ print('์ตœ์†Œ๊ฐ’:', x.min(), '์ตœ๋Œ€๊ฐ’:', x.max()) # ์ตœ์†Œ๊ฐ’, ์ตœ๋Œ€๊ฐ’ print('์ตœ์†Œ๊ฐ’์˜ ์œ„์น˜:', x.argmin(),'์ตœ๋Œ€๊ฐ’์˜ ์œ„์น˜:', x.argmax()) # ์ตœ์†Œ๊ฐ’์˜ ์œ„์น˜, ์ตœ๋Œ€๊ฐ’์˜ ์œ„์น˜ #๋ฐฑํ„ฐ๊ฐ’์˜ ํ‰๊ท  print('๋ฐฑํ„ฐ๊ฐ’์˜ ํ‰๊ท :', x.mean()) #๋ฐฑํ„ฐ์˜ ์ค‘์•™๊ฐ’ print('๋ฐฑํ„ฐ์˜ ์ค‘์•™๊ฐ’:', np.median(x)) # ๊ฐ€์˜จ๋ฐ ๋‘๊ฐœ๊ฐ’ ๋”ํ•ด์„œ ํ‰๊ท  print(..

python - numpy(12) ๋ฐฑํ„ฐํ™” ์—ฐ์‚ฐ

๋ฐฑํ„ฐํ™” ์—ฐ์‚ฐ ์†๋„ ์ธก์ •! import numpy as np x = np.arange(1, 10001) y = np.arange(10001,20001) z = np.zeros_like(x) for i in range(10000): z[i] = x[1]+y[i] print(z) z=x+y print(z) # ================================== # ์†๋„ ์ธก์ • def runTime1(): import timeit start = timeit.default_timer() for i in range(10000): z[i] = x[1] + y[i] end = timeit.default_timer() print('sum vector:', (end-start)) def runTime2(): im..

python - numpy(11) 2์ฐจ์› ๊ทธ๋ฆฌ๋“œ ํฌ์ธํŠธ

๊ทธ๋ฆฌ๋“œ๋ž€? 2์ฐจ์› ์˜์—ญ์— ๋Œ€ํ•œ (x,y)์˜ ์ขŒํ‘œ๊ฐ’ ์Œ. metplotlib ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ 2์ฐจ์› ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ฑฐ๋‚˜ ํ‘œ๋ฅผ ๊ทธ๋ฆฌ๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ metplotlib ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•œ๋‹ค. import numpy as np import matplotlib.pyplot as plt x = np.arange(3) y = np.arange(5) X, Y = np.meshgrid(x, y) print(X) print(Y) plt.title('Grid Points') plt.scatter(X, Y, linewidths=1) # ์‚ฐ์ ๋„ plt.show() [์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ] ๋ญ”๊ฐ€๊ฐ€ ํŒ์—…๋œ๋‹ค..ใ…‹ [[0 1 2] [0 1 2] [0 1 2] [0 1 2] [0 1 2]] [[0 0 0] [1 1..

python - numpy(8) ๋ฐฐ์—ด ์ƒ์„ฑ ๋ช…๋ น

zeros, ones, empty(๊ถŒ์žฅ), arange, linspace ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! import numpy as np # zeros() : ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’์ด 0์ธ ๋ฐฐ์—ด ์ƒ์„ฑ, ์ธ์ˆ˜๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ z_ar = np.zeros(5) print('zeros array 1d \n', z_ar, z_ar.dtype) # ๋‹ค์ฐจ์› ๋ฐฐ์—ด: ์ธ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ ์‚ฌ์šฉ z_2dar = np.zeros((2, 3)) print('zeros array 2d \n', z_2dar, z_2dar.dtype) # ์ž๋ฃŒํ˜• ์ž…๋ ฅ ์ง€์ • dt_z_ar = np.zeros((5, 2), dtype='i') print('zeros array dt \n', dt_z_ar, dt_z_a..

python -numpy(7) ๋ฐฐ์—ด์˜ ์ž๋ฃŒํ˜•

๋ฐฐ์—ด ndarrayํด๋ž˜์Šค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ™์€ ์ž๋ฃŒํ˜•์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! array๋ช…๋ น์œผ๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ ์‹œ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด dtype์ธ์ˆ˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. import numpy as np # ---------------------------------------------------------- # ๋ฐฐ์—ด์— ํƒ€์ž… ํ™•์ธ x = np.array([1, 2, 3]) print(x.dtype) x = np.array([1.2, 2.2, 3.1]) print(x.dtype) x = np.array(['A', 'B', 'C']) print(x.dtype) # Unicode x = np.array([True, False]) print(x.dtype) # ------------------------------------------..

python - numpy(5) ๋ฐฐ์—ด์˜ ์ธ๋ฑ์‹ฑ

import numpy as np # ๋ฐฐ์—ด ์ธ๋ฑ์‹ฑ # ์ธ๋ฑ์‹ฑ์€? ํŠน์ • ์œ„์น˜์˜ ๊ฐ’์„ ํ™•์ธํ•˜๋Š”๊ฒƒ.. a = np.array([0, 1, 2, 3, 4]) print(a[2]) print(a[-1]) a = np.array([[1, 2, 3],[4, 5, 6]]) print(a) print(a[1, 0]) # 1๋ฒˆ ํ–‰์˜ 0๋ฒˆ ์—ด print(a[1][0]) # 1๋ฒˆ ํ–‰์˜ 0๋ฒˆ ์—ด print(a[-1, -1]) # ๋งˆ์ง€๋ง‰ ํ–‰, ๋งˆ์ง€๋ง‰ ์—ด # ๋ฐฐ์—ด ์Šฌ๋ผ์ด์‹ฑ # ๋ฐฐ์—ด ๊ฐ์ฒด๋กœ ๊ตฌํ˜„ํ•œ ๋‹ค์ฐจ์› ๋ฐฐ์—ด์˜ ์›์†Œ์ค‘ ๋ณต์ˆ˜๊ฐœ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ # Slicing ๊ณผ comma๋ฅผ ์‚ฌ์šฉ b = np.array([[0, 1, 2, 3], [4, 5, 6, 7]]) print(b) print(b[0, :]) # 0๋ฒˆ์งธ ํ–‰ ์ „์ฒด print(b[..

python - numpy(4) ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ ํ™•์ธ

import numpy as np # 1์ฐจ์› ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ a = np.array([1, 2, 3]) print(a.ndim) # 1 print(a.shape) # (3,) # 2์ฐจ์› ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ c = np.array([[1, 2, 3], [4, 5, 6]]) print(c.ndim) # 2 print(c.shape) # (2, 3) # 3์ฐจ์› ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ d = np.array([ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[11, 12, 13, 14], [15, 16, 17, 18], [19, 20, 21, 22]] ]) print(d.ndim) # 3 print(d.shape) # (2, 3, 4) [์‹คํ–‰ ๊ฒฐ๊ณผ] 1 (3,) 2 (2, 3) 3 (2,..

728x90