์ ํ ํ๊ท ๋ชจ๋ธ(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.5, 0.2, 0.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
'Study๐ > AI&SW' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Jupyter Notebook(์ฃผํผํฐ ๋ ธํธ๋ถ)์ ํน์ง๊ณผ ์ฌ์ฉ๋ฒ (0) | 2025.01.19 |
---|