๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ฝ”๋”ฉ๊ณผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํŒŒ๋„(16) - ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ขŒ์•™~์ ์œผ๋กœ ๋จธ์‹ ๋Ÿฌ๋‹!

๋ฐ˜์‘ํ˜•

๐Ÿณ 'ํŒŒ๋„'๋Š” ํฌ๋ ˆ์ด๊ฐ€ ๋„์ „ํ•˜๊ณ  ์žˆ๋Š” ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ ๋„์ „๊ธฐ์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค.

์ง€๋‚œ ๊ฒŒ์‹œ๊ธ€์—์„œ ์—ฐ์žฌ๋˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. : https://itadventure.tistory.com/557

 

ํŒŒ๋„!(15) - ๋ผ์˜ํšŒ๊ท€์™€ 4์ฐจ๋ฐฉ์ •์‹๊นŒ์ง€

๐Ÿฟ 'ํŒŒ๋„'๋Š” ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ ๋„์ „๊ธฐ์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค. ์ง€๋‚œ ๊ฒŒ์‹œ๊ธ€์—์„œ ์—ฐ์žฌ๋˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. : https://itadventure.tistory.com/555 ํŒŒ๋„!(14) - ๋ฆฟ์ง€ ๋ฆฌ๊ทธ๋ ˆ์…˜์œผ๋กœ ์ •ํ™•๋„๊ฐ€ ๋†’์•„์ง„๋‹ค๊ตฌ? โ€ป 'ํŒŒ๋„'๋Š” ํŒŒ์ด์Šคํฌ

itadventure.tistory.com

ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ๋Š” ํŒŒ์ด์ฌ๋ณด๋‹ค ์†๋„๊ฐ€ ๋Š๋ฆฝ๋‹ˆ๋‹ค. ๋กœ๋”ฉ์†๋„๋„ ๋งค์šฐ ๋”๋””๊ณ ์š”.
ํ•˜์ง€๋งŒ ์ „ํ˜€ ์•„๋ฌด๊ฒƒ๋„ ์„ค์น˜๋˜์ง€ ์•Š์€ ์ปดํ“จํ„ฐ์—์„œ ์›น๋ธŒ๋ผ์šฐ์ €๋งŒ ์žˆ์œผ๋ฉด ์ž‘๋™์ด ๋˜์ง€์š”.

์ฒ˜์Œ์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ€์ƒ๋จธ์‹ ์ด ํŒŒ์ด์ฌ์„ ํ‰๋‚ด๋ฅผ ๋‚ด๋Š” ์ค„๋กœ ์ƒ๊ฐ ํ–ˆ์—ˆ๋Š”๋ฐ์š”.
๊ทธ๊ฒŒ ์•„๋‹ˆ๋”๋ผ๊ตฌ์š”. ์š”์ƒˆ ์‹ ๊ธฐ์ˆ ๋กœ ๋ถ„๋ฅ˜๋  ์ˆ˜ ์žˆ๋Š” ์›น์–ด์…ˆ๋ธ”๋ฆฌ๋ผ๋Š” ๊ธฐ์ˆ ์ด ์‹ค์ œ ํŒŒ์ด์ฌ์„ ์ž‘๋™์‹œํ‚ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ทธ๊ฒƒ๋„ ๋‚ด ์ปดํ“จํ„ฐ์—์„œ ๋ง์ด์ง€์š”. ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์€ ์›น์„œ๋ฒ„๊ฐ€ ์•„๋‹ˆ๋ผ ๋‚ด ์ปดํ“จํ„ฐ์ด์ง€์š”.

์›น์„œ๋ฒ„๋Š” ๋‹ค๋ฅธ ๊ณณ์— ์žˆ์ง€๋งŒ ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ๋กœ ๋จธ์‹ ๋Ÿฌ๋‹์„ ์ž‘๋™์‹œ์ผœ ๋ณด๋ฉด ๋‚ด ์ปดํ“จํ„ฐ์˜ CPU ์‚ฌ์šฉ์œจ์ด ์—„์ฒญ ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค.

 

ํŒŒ์ด์ฌ์€ ์ฃฝ์ง€ ์•Š๋Š”๋‹ค. ๋‹ค๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ๋–  ์žˆ์„ ๋ฟ์ด๋‹ค.


๊ฑฐ์˜ 1.5G ๊ฐ€๋Ÿ‰์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ์œ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”. ์›นํŽ˜์ด์ง€๊ฐ€ ๋ชจ๋‘ ๋œฌ ์ƒํƒœ์—์„œ๋„ ๊ณ„์† ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ์š”? ๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ ์›น๋ธŒ๋ผ์šฐ์ € ๋‚ด ํŒŒ์ด์ฌ์ด ํ˜„์žฌ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ง€์š”.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ ๋–  ์žˆ๋Š” ํŒŒ์ด์ฌ ์—”์ง„์€ ๋ช…๋ น์„ ๋ฐ›์œผ๋ฉด ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋„๋ก ๋Œ€๊ธฐํ•˜๊ณ  ์žˆ์œผ๋ฉฐ
์›นํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ ๊ณ ์นจํ•˜์ง€ ์•Š๋”๋ผ๋„ ํŒŒ์ด์ฌ ์—”์ง„์œผ๋กœ ๋ช…๋ น๋งŒ ์ „๋‹ฌํ•˜๋ฉด ๋ญ”๊ฐ€ ์ƒˆ๋กœ์šด ์ž‘๋™์„ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.
๋†€๋ž์ง€ ์•Š๋‚˜์š”? :)

์˜ค๋Š˜์€ ๊ทธ๊ฑธ ์ด์šฉํ•ด์„œ ๋ฆฟ์ง€๋ฆฌ๊ทธ๋ ˆ์…˜๊ณผ ๋ผ์˜๋ฆฌ๊ทธ๋ ˆ์…˜, ๊ทธ๋ฆฌ๊ณ  ์•ŒํŒŒ๊ฐ’์„ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒ์ƒ์ž๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๊ฐ€ ์–ด๋Š์ •๋„ ์ ์ค‘ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์šฐ์„  ํ†ต์พŒ(?)ํ•˜๊ฒŒ ์†Œ์Šค์™€ ๊ฒฐ๊ณผ๋ฌผ์„ ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์— ๊ณต๊ฐœํ•˜๊ณ  ์„ค๋ช…์€ ๋‹ค์Œ ๊ฒŒ์‹œ๊ธ€์—์„œ ์ง„ํ–‰ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค :)

 

์†Œ์Šค๋ฅผ ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค!

 

์ด๋ฒˆ ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค๋Š” ์•„๋ž˜์™€ ๊ฐ™์œผ๋ฉฐ ํฌ๋ ˆ์ด์˜ ํ™ˆํŽ˜์ด์ง€์—์„œ๋„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
http://dreamplan7.cafe24.com/pyscript/py11-1.html

<html> 
    <head>
      <title>๋‹คํ•ญํšŒ๊ท€ + ๋ผ์˜ ๋ฆฌ๊ทธ๋ ˆ์…˜</title>
      <link rel="stylesheet" 
        href="https://pyscript.net/alpha/pyscript.css" /> 
      <script defer 
        src="https://pyscript.net/alpha/pyscript.js"></script> 
<py-env>
  - pandas
  - matplotlib
  - seaborn
  - scikit-learn
  - paths:
    - ./NANUMMYEONGJO.TTF
    - ./NANUMMYEONGJOBOLD.TTF
    - ./common.py
</py-env>
    </head>
  <body> 
    <link rel="stylesheet" href="pytable.css"/>

    ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ํƒ :
    <select id="algorithm">
      <option value="">์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ ํƒํ•˜์„ธ์š”</option>
      <option value="๋ฆฟ์ง€">๋ฆฟ์ง€ ํšŒ๊ท€ (Ridge Regression)</option>
      <option value="๋ผ์˜">๋ผ์˜ ํšŒ๊ท€ (Lasso Regression)</option>
    </select><br/>
    ์•ŒํŒŒ๊ฐ’ ์„ ํƒ : 
    <select id="alpha_value">
       <option value="0.001">0.001</option>
       <option value="0.01">0.01</option>
       <option value="0.1">0.1</option>
       <option value="1">1</option>
       <option value="10">10</option>
       <option value="100">100</option>
    </select><br/>
    ํ›ˆ๋ จ์ ์ˆ˜ : <span id="train_score" style=''></span><br/>
    ํ…Œ์ŠคํŠธ์ ์ˆ˜ : <span id="test_score" style=''></span><br/>
    
    <div id="graph"></div>
<py-script>
import pandas as pd
from pyodide.http import open_url
from common import *
import numpy as np

from datetime import datetime

# ๊ฒฝ๊ณ  ๋ฌธ๊ตฌ ์ œ๊ฑฐ
import warnings
warnings.filterwarnings( 'ignore' )

# ํŒ๋‹ค์Šค์—์„œ csv ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ์ฝ์–ด์˜ด
๋งค์ถœ๋ฐ์ดํ„ฐ = pd.read_csv(open_url(
  "http://dreamplan7.cafe24.com/pyscript/csv/avocado.csv"
))      

# 3๊ฐœ ํ•„๋“œ๋งŒ ์ถ”๋ ค์„œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ๋‹ค์‹œ ๋งŒ๋“ฌ
๋งค์ถœ๋ฐ์ดํ„ฐ = ๋งค์ถœ๋ฐ์ดํ„ฐ[[
  'Date', 
  'Total Volume',
  'AveragePrice'
]]   

# ์˜๋ฌธ ์ œ๋ชฉ์„ ํ•œ๊ธ€๋กœ ๋ณ€๊ฒฝ
๋งค์ถœ๋ฐ์ดํ„ฐ.columns = [
  '๋‚ ์งœ', 
  '๋งค์ถœ๋Ÿ‰',
  'ํ‰๊ท ๊ฐ€๊ฒฉ'
]

์ฃผ๊ฐ„๋งค์ถœ_๋งค์ถœ๋Ÿ‰=๋งค์ถœ๋ฐ์ดํ„ฐ.fillna(0) \
  .groupby('๋‚ ์งœ', as_index=False)[['๋งค์ถœ๋Ÿ‰']].sum() \
  .sort_values(by='๋‚ ์งœ', ascending=True)
  
์ฃผ๊ฐ„๋งค์ถœ_ํ‰๊ท ๊ฐ€=๋งค์ถœ๋ฐ์ดํ„ฐ.fillna(0) \
  .groupby('๋‚ ์งœ', as_index=False)[['ํ‰๊ท ๊ฐ€๊ฒฉ']].mean() \
  .sort_values(by='๋‚ ์งœ', ascending=True)

์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ=pd.merge(์ฃผ๊ฐ„๋งค์ถœ_๋งค์ถœ๋Ÿ‰, ์ฃผ๊ฐ„๋งค์ถœ_ํ‰๊ท ๊ฐ€, on='๋‚ ์งœ')

# ๋‚ ์งœ(์‹œ๊ฐ„๊ฐ’) ์ถ”๊ฐ€
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ.insert(1, '๋‚ ์งœ(์‹œ๊ฐ„๊ฐ’)',
  '',
  True)
  
for i in ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋‚ ์งœ'].index:
  ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋‚ ์งœ(์‹œ๊ฐ„๊ฐ’)'].loc[i]=time.mktime(
    datetime.strptime(
      ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋‚ ์งœ'].loc[i], 
      '%Y-%m-%d'
    ).timetuple()
  )

# 10000์œผ๋กœ ๋‚˜๋ˆˆ ๋งค์ถœ๋Ÿ‰ ํ•„๋“œ ์ถ”๊ฐ€
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ.insert(3, '๋งค์ถœ๋Ÿ‰(๋งŒ)', 
  ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋งค์ถœ๋Ÿ‰']/10000, 
  True)

# ํ›ˆ๋ จํ•™์Šต์šฉ์œผ๋กœ ๋‚ ์งœ๋ฅผ ์—ฐ๋„, ์›”, ์ผ๋กœ ๋‚˜๋ˆˆ๋‹ค
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ.insert(4, '์—ฐ๋„', '', True)
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ.insert(5, '์›”', '', True)
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ.insert(6, '์ผ', '', True)
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ.insert(7, '์ฃผ', '', True)

for i in ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋‚ ์งœ'].index:
  ์ž„์‹œ=str(์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋‚ ์งœ'].loc[i]).split('-')
  ์—ฐ๋„=int(์ž„์‹œ[0])
  ์›”=int(์ž„์‹œ[1])
  ์ผ=int(์ž„์‹œ[2])
  ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['์—ฐ๋„'].loc[i]=์—ฐ๋„
  ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['์›”'].loc[i]=์›”
  ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['์ผ'].loc[i]=์ผ
  ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['์ฃผ'].loc[i]=str(
    datetime(์—ฐ๋„, ์›”, ์ผ).isocalendar()[1]
  )

createElementDiv(
  document, 
  Element, 
  'output2'
).write(์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ)

์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐํ›ˆ๋ จ_๋„˜ํŒŒ์ด = ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ[['๋‚ ์งœ(์‹œ๊ฐ„๊ฐ’)', '์—ฐ๋„', '์›”', '์ผ', 'ํ‰๊ท ๊ฐ€๊ฒฉ']].to_numpy()
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ๋ชฉํ‘œ_๋„˜ํŒŒ์ด = ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋งค์ถœ๋Ÿ‰(๋งŒ)'].to_numpy()
์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ๋‚ ์งœ_๋„˜ํŒŒ์ด = ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ['๋‚ ์งœ'].to_numpy()

from sklearn.model_selection import train_test_split

ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ, ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ, ํ…Œ์ŠคํŠธ๋ชฉํ‘œ = \
  train_test_split(
    ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐํ›ˆ๋ จ_๋„˜ํŒŒ์ด, 
    ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ๋ชฉํ‘œ_๋„˜ํŒŒ์ด,
    random_state=100,
    shuffle=False)

from sklearn.preprocessing import PolynomialFeatures
ํด๋ฆฌ = PolynomialFeatures(degree=4, include_bias=False) # ์ ˆํŽธ ์†์„ฑ์€ ์ œ๊ฑฐ
ํด๋ฆฌ.fit(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ) # ํŠน์„ฑ์„ ๋‹คํ•ญ์œผ๋กœ ์ž๋™์œผ๋กœ ๋ถˆ๋ฆผ
#print(ํด๋ฆฌ.get_feature_names_out())
ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต = ํด๋ฆฌ.transform(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ) # ํ•™์Šต์— ์ถ”๊ฐ€๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋งž๊ฒŒ ๋‹คํ•ญ ๋ณ€ํ™˜
ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต = ํด๋ฆฌ.transform(ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ) # ํ…Œ์ŠคํŠธ ์„ธํŠธ๋„ ๋‹คํ•ญ ๋ณ€ํ™˜, fitํ–ˆ๋˜ ํ›ˆ๋ จ poly ๋ฅผ ์‚ฌ์šฉ.

#=====================================
from sklearn.preprocessing import StandardScaler
์Šค์ผ€์ผ๋Ÿฌ = StandardScaler()
์Šค์ผ€์ผ๋Ÿฌ.fit(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)
ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต = ์Šค์ผ€์ผ๋Ÿฌ.transform(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)
ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต = ์Šค์ผ€์ผ๋Ÿฌ.transform(ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)

from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso

#=====================================

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import matplotlib as mat

# ๊ธฐ์ค€ ํฐํŠธ ๋ณ€๊ฒฝ : legend ์˜ ํ•œ๊ธ€์„ ์ ์šฉํ•˜๋ ค๋ฉด ํ•„์ˆ˜
fm.fontManager.addfont('./NANUMMYEONGJO.TTF') #  ํฐํŠธ๋ช… : NanumMyeongjo
mat.rc('font', family='NanumMyeongjo')

###################
# ํฐํŠธ ๋ชฉ๋ก ์ถœ๋ ฅ ( ํฐํŠธ ์ถ”๊ฐ€ ํ›„ ์ •ํ™•ํ•œ ์ด๋ฆ„์„ ํ™•์ธํ•˜๋ ค๋ฉด ํ•„์š”
#font_list = [font.name for font in fm.fontManager.ttflist]
#for f in font_list:
#  print(f)      
###################        

# ๊ฐœ๋ณ„ ํฐํŠธ ์ ์šฉ
NanumMyengjo = fm.FontProperties(
  fname='./NANUMMYEONGJO.TTF'
)
NanumMyengjoBold = fm.FontProperties(
  fname='./NANUMMYEONGJOBOLD.TTF'
)

# ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ํ†ต์‹ 
from js import document
from pyodide import create_proxy

def ๊ทธ๋ž˜ํ”„ํ•จ์ˆ˜(event):

  ์•Œ๊ณ ๋ฆฌ์ฆ˜ = document.getElementById("algorithm").value
  ์•ŒํŒŒ๊ฐ’ = float(document.getElementById("alpha_value").value)
    
  if ์•Œ๊ณ ๋ฆฌ์ฆ˜=='๋ฆฟ์ง€':
    #=====================================
    # ๋ฆฟ์ง€๋ชจ๋ธ
    ๋ฆฟ์ง€๋ชจ๋ธ = Ridge(alpha=์•ŒํŒŒ๊ฐ’)
    ๋ฆฟ์ง€๋ชจ๋ธ.fit(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต, ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ)

    ํ›ˆ๋ จ์ ์ˆ˜=๋ฆฟ์ง€๋ชจ๋ธ.score(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต, ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ)
    ํ…Œ์ŠคํŠธ์ ์ˆ˜=๋ฆฟ์ง€๋ชจ๋ธ.score(ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต, ํ…Œ์ŠคํŠธ๋ชฉํ‘œ)
    document.getElementById("train_score").innerHTML=ํ›ˆ๋ จ์ ์ˆ˜
    document.getElementById("test_score").innerHTML=ํ…Œ์ŠคํŠธ์ ์ˆ˜

    ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ์˜ˆ์ธก = ๋ฆฟ์ง€๋ชจ๋ธ.predict(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)
    ํ…Œ์ŠคํŠธ๋ชฉํ‘œ์˜ˆ์ธก = ๋ฆฟ์ง€๋ชจ๋ธ.predict(ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)
  else:
    #=====================================
    # ๋ผ์˜๋ชจ๋ธ
    ๋ผ์˜๋ชจ๋ธ = Lasso(alpha=์•ŒํŒŒ๊ฐ’)
    ๋ผ์˜๋ชจ๋ธ.fit(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต, ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ)
    
    ํ›ˆ๋ จ์ ์ˆ˜=๋ผ์˜๋ชจ๋ธ.score(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต, ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ)
    ํ…Œ์ŠคํŠธ์ ์ˆ˜=๋ผ์˜๋ชจ๋ธ.score(ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต, ํ…Œ์ŠคํŠธ๋ชฉํ‘œ)
    document.getElementById("train_score").innerHTML=ํ›ˆ๋ จ์ ์ˆ˜
    document.getElementById("test_score").innerHTML=ํ…Œ์ŠคํŠธ์ ์ˆ˜

    ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ์˜ˆ์ธก = ๋ผ์˜๋ชจ๋ธ.predict(ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)
    ํ…Œ์ŠคํŠธ๋ชฉํ‘œ์˜ˆ์ธก = ๋ผ์˜๋ชจ๋ธ.predict(ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ_๊ฐ€๊ณต)

  # ๊ทธ๋ž˜ํ”„
  fig = plt.figure(
    figsize=(15, 7)
  )
  # ์ฃผ์œ„ ์ด์ƒํ•œ ์—ฌ๋ฐฑ ์—†์• ๊ธฐ
  fig.tight_layout() 
  
  plt.title('์ฃผ๊ฐ„ ์•„๋ณด์นด๋„ ๋งค์ถœ๋Ÿ‰(' + ์•Œ๊ณ ๋ฆฌ์ฆ˜ + ')',
    fontproperties=NanumMyengjoBold, 
    fontsize=32
  );

  # ์›๋ณธ
  plt.plot(        
    ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ[:,0],
    ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ,
    #marker='o',
    color='gray',
    label='์›๋ณธ'
  )
  plt.plot(        
    ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ[:, 0],
    ํ…Œ์ŠคํŠธ๋ชฉํ‘œ,
    #marker='o',
    color='gray'
  )

  # ํ›ˆ๋ จ
  plt.plot(        
    ํ›ˆ๋ จ์šฉ๋ฐ์ดํ„ฐ[:,0],
    ํ›ˆ๋ จ์šฉ๋ชฉํ‘œ์˜ˆ์ธก,
    marker='d',
    color='blue',
    label='ํ›ˆ๋ จํŒจํ„ด'
  )
  
  # ์˜ˆ์ธก
  plt.plot(        
    ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ[:, 0],
    ํ…Œ์ŠคํŠธ๋ชฉํ‘œ์˜ˆ์ธก,
    marker='*',
    color='green',
    label='์˜ˆ์ธกํŒจํ„ด'
  )

  plt.legend(
    shadow=True
  )

  plt.xticks(
    ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐํ›ˆ๋ จ_๋„˜ํŒŒ์ด[:, 0],
    ์ฃผ๊ฐ„๋งค์ถœ๋ฐ์ดํ„ฐ๋‚ ์งœ_๋„˜ํŒŒ์ด, 
    rotation=90, fontsize=8)

  plt.xlabel('๋‚ ์งœ', fontsize=16)
  plt.ylabel('๋งค์ถœ๋Ÿ‰(๋‹จ์œ„:๋งŒ)', fontsize=12)

  ax = plt.gca()
  # ์ถ•๋งŒ ๊ทธ๋ฆฌ๋“œ
  ax.xaxis.grid(True)

  # ๋ฐฐ๊ฒฝ์ƒ‰, ๋งˆ์ง„ ์กฐ์ •
  ax.set_facecolor('#e8e7d2')
  ax.margins(x=0.01, y=0.02)
  pyscript.write("graph",fig)

e1 = document.getElementById("algorithm")
e1.addEventListener("change", create_proxy(๊ทธ๋ž˜ํ”„ํ•จ์ˆ˜))  

e2 = document.getElementById("alpha_value")
e2.addEventListener("change", create_proxy(๊ทธ๋ž˜ํ”„ํ•จ์ˆ˜))  

</py-script> 
  </body> 
</html>


ํ‰์†Œ์ฒ˜๋Ÿผ ์ƒ๋‹นํ•œ ๋กœ๋”ฉ์‹œ๊ฐ„์ด ์†Œ์š”๋œ ๋‹ค์Œ์— ๋ญ”๊ฐ€ ์ข€ ํ™”๋ฉด์ด ๋‹ค๋ฅด๊ฒŒ ๋ณด์ด์‹คํ…๋ฐ์š”.
๋ฐ”๋กœ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‚˜์˜ค๋Š” ๋Œ€์‹  ๋ญ”๊ฐ€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์„ ํƒ์ƒ์ž๊ฐ€ 2๊ฐœ๊ฐ€ ๋ณด์ด์‹ค ๊ฒ๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ์„ ํƒ์ƒ์ž๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ข…๋ฅ˜๋ฅผ ์„ ํƒํ•˜๋Š” ์ƒ์ž์ธ๋ฐ์š”.
๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•˜๋ฉด 2๊ฐœ์˜ ์„ ํƒํ•ญ๋ชฉ์ด ๋ณด์ด์‹ค ๊ฒ๋‹ˆ๋‹ค.

 

๋ผ์˜ ํšŒ๊ท€๋ฅผ ์„ ํƒํ•ด๋ณผ๊นŒ์š”?
์•— ๊ทธ๋Ÿฌ์ž 1.5์ดˆ ์žˆ๋‹ค ๊ทธ๋ž˜ํ”„๊ฐ€ ๋–ด์Šต๋‹ˆ๋‹ค ( ๋…์ž ์—ฌ๋Ÿฌ๋ถ„์˜ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์— ๋”ฐ๋ผ ์‹œ๊ฐ„์€ ์ฐจ์ด๊ฐ€ ๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค )
ํ›ˆ๋ จ์ ์ˆ˜์™€ ํ…Œ์ŠคํŠธ ์ ์ˆ˜๋„ ํ•จ๊ป˜ ํ‘œ์‹œ๋˜์—ˆ๊ตฐ์š”. ์ ์ค‘์œจ์€ ๊ทธ๋ฆฌ ๋†’์ง€ ์•Š์€๋ฐ์š”. ๊ทธ๊ฒƒ์€ ์•ŒํŒŒ๊ฐ’์ด ๋‚ฎ์•„์„œ์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด์ œ ์•ŒํŒŒ๊ฐ’๋„ ์„ ํƒ์ƒ์ž๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์•ŒํŒŒ๊ฐ’ ์„ ํƒ์ƒ์ž๋ฅผ ํด๋ฆญํ•˜๋ฉด 0.001 ๋ถ€ํ„ฐ 100๊นŒ์ง€ ์ค‘ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

ํ•˜๋‚˜์”ฉ ๊ณจ๋ผ์„œ ์‚ดํŽด๋ณด๋ฉด ์ ์  ์ ์ค‘๋„์™€ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ณ€ํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฐ˜์‘์†๋„๋Š” 1์ดˆ~ 1.5์ดˆ ์†Œ์š”๋˜๋Š”๋ฐ ์ €๋ณด๋‹ค ์ปดํ“จํ„ฐ๊ฐ€ ์ข‹์€ ๋ถ„์€ ๋” ๋น ๋ฅธ ์‘๋‹ต ์†๋„๋ฅผ ๋ณด์ด์‹ค ๊ฒ๋‹ˆ๋‹ค.
์•ŒํŒŒ๊ฐ’ ๊ต์ •์ด๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ํƒ์„ ๋‹จ์ˆœํžˆ ์„ ํƒํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ๋จธ์‹ ๋Ÿฌ๋‹ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด
๋ฐ”๋กœ ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์žฅ์ ์ž…๋‹ˆ๋‹ค.

๋งˆ๋ฌด~๋ฆฌ

 

์ด๋ ‡๊ฒŒ ์›น ๋ธŒ๋ผ์šฐ์ € ๋‚ด์— ๋–  ์žˆ๋Š” ํŒŒ์ด์ฌ๊ณผ ์†Œํ†ตํ•˜๋Š” ๊ธฐ์ˆ ์„ ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” 'ํ”„๋ก์‹œ'๋ผ๊ณ  ํ•˜๋Š”๋ฐ์š”.
๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” ์ž‘๋™์›๋ฆฌ๋ฅผ ์„ค๋ช…๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)


์•„๋ฌด์ชผ๋ก ํ•„์š”ํ•˜์‹  ๋ถ„๊ป˜ ๋„์›€์ด ๋˜์…จ์„์ง€์š” :)
๋ฐฉ๋ฌธํ•ด์ฃผ์‹œ๋Š” ๋ชจ๋“  ๋ถ„๋“ค๊ป˜ ๋Š˜ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

์œ ์ตํ•˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ  ํ•œ๋ฐฉ, ๋Œ“๊ธ€์€ ๊ตฟ์žก!
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~


๋‹ค์Œ๊ฒŒ์‹œ๊ธ€ : https://itadventure.tistory.com/559

 

ํŒŒ๋„(17) - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ, ์†์žก๋‹ค!

๐Ÿฌ 'ํŒŒ๋„'๋Š” ํฌ๋ ˆ์ด์˜ ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ ๋„์ „๊ธฐ์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค. ์ง€๋‚œ ๊ฒŒ์‹œ๊ธ€์—์„œ ์—ฐ์žฌ๋˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. https://itadventure.tistory.com/558 ํŒŒ๋„(16) - ํŒŒ์ด์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ขŒ์•™~์ ์œผ๋กœ ๋จธ์‹ ๋Ÿฌ๋‹! ๐Ÿณ 'ํŒŒ๋„'๋Š”

itadventure.tistory.com

 

๋ฐ˜์‘ํ˜•