๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Study๐Ÿ“š/AI&SW

๋จธ์‹ ๋Ÿฌ๋‹์˜ ์ฒซ๊ฑธ์Œ: NumPy์™€ Matplotlib๋กœ ์„ ํ˜•ํšŒ๊ท€ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”ํ•˜๊ณ  ๋ถ„์„ํ•˜๊ธฐ

์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ(Linear Regression)์˜ ๊ธฐ์ดˆ์ ์ธ ํŒŒ์ด์ฌ ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๋ฉฐ NumPy์™€ Matplotlib์˜ ํ™œ์šฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด๋„๋ก ํ•˜์ž.

 

์ฝ”๋“œ ํ’€๋ฒ„์ „
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('./deeplearning.mplstyle')

# x_train is the input variable (size in 1000 square feet)
# y_train is the target (price in 1000s of dollars)
x_train = np.array([1.0, 2.0])
y_train = np.array([300.0, 500.0])
print(f"x_train = {x_train}")
print(f"y_train = {y_train}")

# m is the number of training examples
print(f"x_train.shape: {x_train.shape}")
m = x_train.shape[0]
print(f"Number of training examples is: {m}")

# m is the number of training examples
m = len(x_train)
print(f"Number of training examples is: {m}")

i = 1 # Change this to 1 to see (x^1, y^1)

x_i = x_train[i]
y_i = y_train[i]
print(f"(x^({i}), y^({i})) = ({x_i}, {y_i})")

# Plot the data points
plt.scatter(x_train, y_train, marker='x', c='r')
# Set the title
plt.title("Housing Prices")
# Set the y-axis label
plt.ylabel('Price (in 1000s of dollars)')
# Set the x-axis label
plt.xlabel('Size (1000 sqft)')
plt.show()

 

์ฝ”๋“œ ๋œฏ์–ด๋ณด๊ธฐ

 

import numpy as np

 

  • NumPy ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

NumPy๋Š” ์ˆซ์ž๋กœ ๋œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋‹ค๋ฃฐ ๋•Œ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๋ชจ์•„ ๋‘” ์ˆ˜ํ•™ ๊ณ„์‚ฐ&๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์šฉ ๋„๊ตฌ ์ƒ์ž๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. NumPy๋ฅผ ์ž„ํฌํŠธํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๋‹ค์ฐจ์› ๋ฐฐ์—ด(e.g. ํ‘œ), ์ˆ˜๋งŽ์€ ์ˆซ์ž์— ๊ฐ™์€ ์—ฐ์‚ฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ, ์ œ๊ณฑ๊ทผ, ์‚ผ๊ฐํ•จ์ˆ˜, ๋กœ๊ทธ ๊ณ„์‚ฐ, ๋‚œ์ˆ˜ ์ƒ์„ฑ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

import numpy as np์—์„œ as np๋Š” ๋„˜ํŒŒ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ถ€๋ฅด๊ธฐ ์œ„ํ•ด ์ถ•์•ฝํ˜• ์ด๋ฆ„์„ np๋ผ๊ณ  ์ •ํ•ด์ค€๋‹ค๋Š” ๋œป์ด๋‹ค. NumPy ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์† ๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ์ผ์ผ์ด NumPy๋ฅผ ํƒ€์ดํ•‘ํ•˜๊ธฐ ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์— np๋ผ๊ณ ๋งŒ ์“ฐ๋ฉด ๋„˜ํŒŒ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํŒŒ์ด์ฌ ์‚ฌ์šฉ์ž๋“ค์ด NumPy๋ฅผ np๋กœ ๋ถ€๋ฅด๋Š” ๊ฑธ ๊ด€๋ก€์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด np๋ฅผ ๋ด๋„ NumPy์ž„์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • ์ด ์ฝ”๋“œ์—์„œ NumPy ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ์ด์œ 

NumPy๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด ์ „์ฒด์— ๋Œ€ํ•ด ํ•œ๊บผ๋ฒˆ์— ์—ฐ์‚ฐ์„ ๋จน์ผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ NumPy๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค. ๋จธ์‹ ๋Ÿฌ๋‹์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ์–‘์ด ๋งŽ์œผ๋ฏ€๋กœ NumPy๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ๊ณ„์‚ฐ์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ for ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง๊ด€์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์–ด ํŽธํ•˜๋‹ค.

 

์ด ๋จธ์‹ ๋Ÿฌ๋‹ ์ฝ”๋“œ์—์„œ๋Š” x ๋ฐ์ดํ„ฐ์…‹๊ณผ y ๋ฐ์ดํ„ฐ์…‹์„ ๋ชจ๋‘ ํŠธ๋ ˆ์ด๋‹์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ ๋‹ค์ฐจ์› ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ผ๋ฆฌ ์—ฐ์‚ฐ๋„ ํ•  ์˜ˆ์ •์ด๋‹ค. NumPy๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฆฌ์ŠคํŠธ๋กœ ์‚ฌ์šฉํ•ด๋„ 1์ฐจ์› ๋ฐฐ์—ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค์ฐจ์› ๋ฐฐ์—ด๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์ŠคํŠธ๋กœ๋„ ์ „์ฒด ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค(len()). ๊ทธ๋Ÿฌ๋‚˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์–ด ๋†“๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ผ ๋ฟ, ๋ฐฐ์—ด ๊ฐ„์˜ ์ˆ˜ํ•™์  ์—ฐ์‚ฐ์€ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•  ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ผ๋ฆฌ ๊ณ„์‚ฐ์„ ํ•  ๋•Œ๋งˆ๋‹ค ์ˆ˜๋™์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์•ผ ํ•œ๋‹ค.

๋˜ํ•œ ๋„˜ํŒŒ์ด๋Š” ๋‹ค์ฐจ์› ๋ฐฐ์—ด์˜ ํ–‰/์—ด ์ •๋ณด ๋“ฑ ์ข€ ๋” ๋‹ค์–‘ํ•œ ์ •๋ณด๋“ค์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ์ˆœํžˆ ์š”์†Œ๋“ค์˜ ๋ชจ์Œ์ด๋ผ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋‚˜ ์ฐจ์›์€ ์•Œ๊ธฐ ์–ด๋ ต์ง€๋งŒ, NumPy๋Š” .shape๊ฐ™์€ ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ํ–‰์—ด ์ •๋ณด๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.


import matplotlib.pyplot as plt

 

  • pyplot ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

Matplotlib๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ pyplot ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋‹จ๊ณ„๋‹ค. Pyplot์€ Matplotlib์˜ ์ผ๋ถ€๋‹ค.

Mat๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ํˆด์ธ MATLAB(๋งคํŠธ๋žฉ)์—์„œ ๋”ฐ์˜จ ๊ฒƒ์ด๊ณ , Plot์€ ๊ทธ๋ž˜ํ”„(ํ”Œ๋กฏ)์„ ๊ทธ๋ฆฐ๋‹ค๋Š” ๋œป์ด๋‹ค. lib๋Š” Library์˜ ์•ฝ์ž๋‹ค. ๋”ฐ๋ผ์„œ Matplotlib๋Š” ๋งคํŠธ๋žฉ ๋А๋‚Œ์˜ ๊ทธ๋ž˜ํ”„๊ทธ๋ฆฌ๊ธฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๋Š” ๋œป์ด๋‹ค.

pyplot์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ๊ทธ๋ ค์ฃผ๋Š” ๋„๊ตฌ๋“ค์„ ์ œ๊ณตํ•˜๋ฉฐ, ํŒŒ์ด์ฌ์šฉ ํ”Œ๋กฏ ๋„๊ตฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. Matplotlib์˜ Pyplot์€ ํŒŒ์ด์ฌ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๋ชจ๋“ˆ์ด๋‹ค. 'as plt'๋ผ๊ณ  ํ•œ ๊ฒƒ์€ 'as np'์ฒ˜๋Ÿผ ํƒ€์ดํ•‘์ด ๊ท€์ฐฎ์•„์„œ ์•ฝ์นญ์„ ์ง€์ •ํ•ด ์ค€ ๊ฒƒ์ด๋‹ค. ์ด์ œ plt๋ผ๊ณ  ์“ฐ๋ฉด pyplot์„ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ด๋‚˜ ๋‹ค๋ฆ„์ด ์—†๋‹ค.

 

  • pyplot ์† ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋“ค
๊ตฌ๋ถ„ ํ•จ์ˆ˜๋ช… ํ•จ์ˆ˜ ์„ค๋ช… ์‚ฌ์šฉ ์˜ˆ์‹œ
๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” .plot() ์„ ํ˜• ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ plt.plot([1, 2, 3], [4, 5, 6])
.scatter() ์‚ฐ์ ๋„(์  ๊ทธ๋ž˜ํ”„) ์ƒ์„ฑ plt.scatter([1, 2, 3], [4, 5, 6])
.bar() ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ plt.bar(['A', 'B', 'C'], [10, 15, 7])
.hist() ํžˆ์Šคํ† ๊ทธ๋žจ ์ƒ์„ฑ
# ๊ตฌ๊ฐ„๋ณ„ ๋ถ„ํฌ๋ฅผ ๋ณด๋Š” ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„
plt.hist(data, bins=10)
# bins๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๊ตฌ๊ฐ„์ž„
.pie() ์›ํ˜• ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ plt.pie([30, 50, 20], labels=['A', 'B', 'C'])
.imshow() 2D ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ํ‘œ์‹œ
# ํžˆํŠธ๋งต ๋“ฑ
plt.imshow(2D ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด ์ด๋ฆ„, cmap='gray')
# cmap์€ ๋ฐ์ดํ„ฐ๊ฐ’์„ ์ƒ‰์ƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ๋•Œ์˜ ์ปฌ๋Ÿฌ๋งต. ์ƒ‰์ƒ ํŒ”๋ ˆํŠธ
.boxplot() ๋ฐ•์Šค ํ”Œ๋กฏ ์ƒ์„ฑ plt.boxplot(data)
๊ทธ๋ž˜ํ”„ ๊พธ๋ฏธ๊ธฐ .title() ๊ทธ๋ž˜ํ”„ ์ œ๋ชฉ ์„ค์ • plt.title("๊ทธ๋ž˜ํ”„ ์ด๋ฆ„")
.xlabel() x์ถ• ๋ ˆ์ด๋ธ” ์„ค์ • plt.xlabel("x์ถ• ๋ ˆ์ด๋ธ” ์ด๋ฆ„")
.ylabel() y์ถ• ๋ ˆ์ด๋ธ” ์„ค์ • plt.ylabel("y์ถ• ๋ ˆ์ด๋ธ” ์ด๋ฆ„")
.xlim() x์ถ• ๋ฒ”์œ„ ์„ค์ • plt.xlim(0, 10)
.ylim() y์ถ• ๋ฒ”์œ„ ์„ค์ • plt.ylim(0, 100)
.legend() ๋ฒ”๋ก€ ํ‘œ์‹œ plt.legend(['Line 1', 'Line 2'], loc='best')
# ์ฒซ๋ฒˆ์งธ ๋ณ€์ˆ˜๋Š” ๋ฒ”๋ก€ ์ด๋ฆ„ ์ง€์ •.
# ๋‘๋ฒˆ์งธ ๋ณ€์ˆ˜๋Š” ๋ฒ”๋ก€ ์œ„์น˜ ์ง€์ •. 'best(์•Œ์•„์„œ ์˜ˆ์œ ๊ณณ์—)', upper right', 'lower left'๋“ฑ ์ž…๋ ฅ ๊ฐ€๋Šฅ
.grid() ๊ฒฉ์ž์„  ์ถ”๊ฐ€ plt.grid(True)
๋ฐ์ดํ„ฐ ์Šคํƒ€์ผ๋ง .style.use() ๊ทธ๋ž˜ํ”„ ์Šคํƒ€์ผ ์„ค์ • plt.style.use('seaborn-darkgrid')
# 'seaborn-darkgrid', 'seaborn-notebook', 'seaborn-white', 'ggplot', 'fivethirtyeight', 'dark_background', 'classic', 'bmh', 'Solarize_Light2' ๋“ฑ...
# ์ง์ ‘ ์Šคํƒ€์ผ์„ ๋งŒ๋“ค์–ด .mplstyle ํ™•์žฅ์ž ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Œ
# ์Šคํƒ€์ผ ์ดˆ๊ธฐํ™”: plt.style.use('default')
.color() ๊ทธ๋ž˜ํ”„ ์ƒ‰์ƒ ์„ค์ • plt.plot(x, y, color='red')
# 'blue''red''green''yellow''cyan''magenta''black''white' ๊ฐ€๋Šฅ
# RGB ์ƒ‰์ƒ ์ง€์ • ๊ฐ€๋Šฅ color=(0.50.20.8)
# ํ—ฅ์‚ฌ ์ฝ”๋“œ ๊ฐ€๋Šฅ color='#FF5733'
.linestyle() ์„  ์Šคํƒ€์ผ ๋ณ€๊ฒฝ plt.plot(x, y, linestyle='-')  # ์‹ค์„  โ”€โ”€โ”€
plt.plot(x, y, linestyle='--')  # ์ ์„  - - - - - -
plt.plot(x, y, linestyle='-.')  # ์ ์„ -์‹ค์„  ํ˜ผํ•ฉ โ”€โ€งโ”€โ€ง
plt.plot(x, y, linestyle=':')  # ์ž‘์€ ์ ์„  .......
.marker() ๋ฐ์ดํ„ฐ ์  ๋งˆ์ปค ์„ค์ • plt.plot(x, y, marker='o')
๋‹ค์ค‘ ๊ทธ๋ž˜ํ”„ .subplot() ์—ฌ๋Ÿฌ ๊ทธ๋ž˜ํ”„๋ฅผ ํ•œ ํ™”๋ฉด์— ๋ฐฐ์น˜ # (ํ–‰, ์—ด, ๊ทธ๋ž˜ํ”„ ๋ฒˆํ˜ธ)
plt.subplot(2, 1, 1)  
# ์œ„์•„๋ž˜๋กœ ๋‘๊ฐœ ๋ฐฐ์น˜ํ•œ ๊ฑฐ ์ค‘์— 1๋ฒˆ์งธ ๊ทธ๋ž˜ํ”„๋ผ๋Š” ๋œป

plt.plot(x1, y1)

plt.subplot(2, 1, 2)
# ์œ„์•„๋ž˜๋กœ ๋‘๊ฐœ ๋ฐฐ์น˜ํ•œ ๊ฑฐ ์ค‘์— 2๋ฒˆ์งธ ๊ทธ๋ž˜ํ”„๋ผ๋Š” ๋œป
plt.plot(x2, y2)
.figure() ์ƒˆ๋กœ์šด ๊ทธ๋ฆผ ์ฐฝ ์ƒ์„ฑ plt.figure(figsize=(8, 6))
# ์ฐฝ ํฌ๊ธฐ ์ง€์ •
# ๊ฐ€๋กœ 8์ธ์น˜, ์„ธ๋กœ 6์ธ์น˜
๋ฐ์ดํ„ฐ ํ‘œ์‹œ ์ €์žฅ .show() ๊ทธ๋ž˜ํ”„ ํ‘œ์‹œ plt.show()
.savefig() ๊ทธ๋ž˜ํ”„๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅ plt.savefig("graph.png") # PNG ํ˜•์‹์œผ๋กœ ์ €์žฅ
plt.savefig("graph.jpg", dpi=300)  # JPG ํ˜•์‹์œผ๋กœ ์ €์žฅ (ํ•ด์ƒ๋„ 300 DPI)
plt.savefig("graph.gif")  # GIF ํ˜•์‹์œผ๋กœ ์ €์žฅ
plt.savefig("graph.pdf")  # PDF ํ˜•์‹์œผ๋กœ ์ €์žฅ
plt.savefig("graph.svg")  # SVG ํ˜•์‹์œผ๋กœ ์ €์žฅ
ํ…์ŠคํŠธ ์ถ”๊ฐ€ .annotate() ๋ฐ์ดํ„ฐ ์ ์— ํ™”์‚ดํ‘œ ์ฃผ์„ ์ถ”๊ฐ€ # plt.annotate(text, xy, xytext, arrowprops)
# text: ์ฃผ์„์— ํ‘œ์‹œํ•  ํ…์ŠคํŠธ
# xy: ํ™”์‚ดํ‘œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ฌ ์ ์˜ x,y์ขŒํ‘œ
# xytext: ์ฃผ์„ ํ…์ŠคํŠธ๊ฐ€ ํ‘œ์‹œ๋  x,y ์œ„์น˜
# arrowprops: ํ™”์‚ดํ‘œ ์Šคํƒ€์ผ ์ •์˜. shrink๋Š” ํ™”์‚ดํ‘œ ๊ธธ์ด

plt.annotate('Important point', xy=(2, 3), xytext=(3, 4),
             arrowprops=dict(facecolor='black', shrink=0.05))
.text() ๊ทธ๋ž˜ํ”„์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ plt.text(2, 3, 'Label')
# ๊ทธ๋ž˜ํ”„์˜ ์ขŒํ‘œ (2, 3)์— Label์ด๋ผ๋Š” ๊ธ€์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒ ๋‹ค๋Š” ๋œป
2์ฐจ์› ๋ฐฐ์—ด์˜ 2ํ–‰ 3์—ด ๋ฐ์ดํ„ฐ์— ๊ธ€์ž๋ฅผ ์“ฐ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ ์•„๋‹˜

 


plt.style.use('./deeplearning.mplstyle')

์œ„ ํ‘œ์— ๋”ฐ๋ฅด๋ฉด ์ด ์ฝ”๋“œ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ์Šคํƒ€์ผ ํŒŒ์ผ(.mplstyle)์„ ๋ถˆ๋Ÿฌ์™€ ํ‘œ๋ฅผ ๊พธ๋ฏธ๋Š” ์ฝ”๋“œ๋‹ค. mpstyle์€ Matplot Style์˜ ์•ฝ์ž๋‹ค.


x_train = np.array([1.0, 2.0])
y_train = np.array([300.0, 500.0])

x_train์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ , ์—ฌ๊ธฐ์— NumPy์˜ .array()๋ฅผ ์‚ฌ์šฉํ•ด 1์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ–ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋˜ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ y_train์„ ๋งŒ๋“ค์–ด ์—ฌ๊ธฐ์—๋„ 1์ฐจ์› ๋ฐฐ์—ด์„ ๋„ฃ์—ˆ๋‹ค.

๋ฐฐ์—ด x_train[0] x_train[1]
x_train 1.0 2.0

 

๋ฐฐ์—ด y_train[0] y_train[1]
y_train 300.0 500.0

 


print(f"x_train = {x_train}")
print(f"y_train = {y_train}")

 

  • ํŒŒ์ด์ฌ์—์„œ print() ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•

print()์˜ ๊ด„ํ˜ธ ์•ˆ์—๋Š” ๋ฌธ์ž์—ด ํ˜•์‹๋งŒ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค. ๋ฌธ์ž์—ด๊ณผ ๋ณ€์ˆ˜๋ฅผ ๊ฐ™์ด ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด f"..." ํ˜•์‹์˜ f-string์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค. f"..."์•ˆ์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๋„ ์ตœ์ข…์ ์œผ๋กœ๋Š” ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

์ค‘๊ด„ํ˜ธ{}๋Š” f-string ์•ˆ์—์„œ ๋ณ€์ˆ˜๋‚˜ ํ‘œํ˜„์‹์„ ์‚ฝ์ž…ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณ€์ˆ˜์ธ y_train์˜ ๊ฐ’์ด ์ค‘๊ด„ํ˜ธ {}์•ˆ์— ์‚ฝ์ž…๋˜์–ด ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

  • f-string์˜ ํ™œ์šฉ ์˜ˆ์‹œ
x = 5
y = 10
print(f"The sum of {x} and {y} is {x + y}")  # The sum of 5 and 10 is 15