๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (11) ๋ณธ๋ฌธ
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (11)
์ง์ง์ํ์นด 2022. 11. 25. 16:56<๋ณธ ๋ธ๋ก๊ทธ๋ itadventrue ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
https://itadventure.tistory.com/553
ํ๋!(12) - ๋ฌด์ ๋ฌ๋? ๋จธ์ ๋ฌ๋! - ๋ฆฌ๋์ด ๋ฆฌ๊ทธ๋ ์ ( LinearRegression )
'ํ๋'๋ ํ์ด์คํฌ๋ฆฝํธ ๋์ ๊ธฐ์ ์ค์๋ง์ ๋๋ค. ์ง๋ ๊ฒ์๊ธ๊น์ง๋ ํ์ด์คํฌ๋ฆฝํธ์์ csv ๋ก ๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ ๊ทธ๋ํ๋ก ํํํ๋ ๊ฒ์ด ์ค์ ์ ์ธ ๋ด์ฉ์ด์์ง์. https://itadventure.tistory.com/552 ํ
itadventure.tistory.com
๐ ํ์ด์คํฌ๋ฆฝํธ์์ ๋จธ์ ๋ฌ๋์ ์ฒ๋ฆฌ
โ ๋จธ์ ๋ฌ๋์ ๊ธฐ๊ณ(๋จธ์ :Machine) ํ์ต(๋ฌ๋:Learning)
ํ์ด์ฌ์์ ์ฌ์ฉํ ์ ์๋ ์ฌ๋ฌ ์ธ๊ณต์ง๋ฅ ๊ด๋ จ ๊ธฐ์ ์ ๋ฌด๋ฃ ๊ณต๊ฐ -> ์ธ์ดํท-๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
<py-env>
- pandas
- matplotlib
- seaborn
- scikit-learn
</pv-env>
โ ๋ฆฌ๋์ด ๋ฆฌ๊ทธ๋ ์ (Linear Regression)
'์ ํํ๊ท๋ชจ๋ธ'์ ์ ์ธ
from sklearn.linear_model import LinearRegression
์ ํํ๊ท๋ชจ๋ธ = LinearRegression()
โ ๊ธฐ๊ณ ํ์ต ๋ฐฉ๋ฒ
๋จธ์ ๋ฌ๋์ด ์์ธกํ ๊ฒฐ๊ณผ๋ฅผ ์ ํจ
๋จธ์ ๋ฌ๋์ด ๋งค์ถ๋์ ์์ธกํ๋ ๊ฒ์ ๋ชฉํ
๋ฐ์ดํฐ๋ฅผ ๋๋์ด์ ์ ๊ณต
ํ๋ จ์ฉ๋ฐ์ดํฐ : ์ฐ๋, ์, ์ผ, ์ฃผ(1๋
์ค ๋ช๋ฒ์งธ ์ฃผ์ธ์ง)
ํ๋ จ์ฉ๋ชฉํ : ๋งค์ถ๋
ํ๋ จ
์ ํํ๊ท๋ชจ๋ธ.fit(ํ๋ จ์ฉ๋ฐ์ดํฐ, ํ๋ จ์ฉ๋ชฉํ)
๋ชจ๋ธ์ ์์ธก
์ ํํ๊ท ๋ชจ๋ธ์ ๋ค๋ฅธ ์ฃผ์ด์ง ๋ฐ์ดํฐ๋ฅผ ๋ฐํ์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ์์ธก
์ฐ๋, ์, ์ผ, ์ฃผ ๋จ์์ ๋ฐ์ดํฐ๋ฅผ ํ ์คํธ๋ฐ์ดํฐ๋ก ์ ๊ณตํ๋ฉด ๋งค์ถ๋ ๊ฒฐ๊ณผ๋ฅผ ์์ธก
ํ
์คํธ๋ชฉํ์์ธก = ์ ํํ๊ท๋ชจ๋ธ.predict(ํ
์คํธ๋ฐ์ดํฐ)
๊ทธ๋ํ๋ก ์๊ฐํ
๐ ๋งค์ถ๋ ์์ธก ๊ทธ๋ํ
โ ์ฝ๋ ๊ตฌํ
- index.html
<html>
<head>
<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 :
- ./common.py
</py-env>
</head>
<body>
<link rel="stylesheet" href="pytable.css"/>
<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 ๋ฅผ ๋ฐ์ดํฐ ํ๋ ์์ผ๋ก ์ฝ์ด์ด -->
SalesData = pd.read_csv(open_url(
"http://dreamplan7.cafe24.com/pyscript/csv/avocado.csv"
))
<!-- # 2๊ฐ ํ๋๋ง ์ถ๋ ค์ ๋ฐ์ดํฐ ํ๋ ์์ ๋ค์ ๋ง๋ฌ -->
SalesData = SalesData[[
'Date',
'Total Volume'
]]
SalesData.columns = [
'Day',
'Amount'
]
<!-- ์ฃผ๊ฐ ๋งค์ถ๋ ๊ทธ๋ฃน -->
WeekdaysSalesData = SalesData.fillna(0) \
.groupby('Day', as_index=False)[['Amount']] \
.sum() \
.sort_values(
by='Day',
ascending=True
)
<!-- ๋ ์ง(์๊ฐ๊ฐ) ์ถ๊ฐ -->
WeekdaysSalesData.insert(1, 'Day(timeValue)',
'',
True)
for i in WeekdaysSalesData['Day'].index:
WeekdaysSalesData['Day(timeValue)'].loc[i]=time.mktime(
datetime.strptime(
WeekdaysSalesData['Day'].loc[i],
'%Y-%m-%d'
).timetuple()
)
<!-- 10000์ผ๋ก ๋๋ ๋งค์ถ๋ ํ๋ ์ถ๊ฐ -->
WeekdaysSalesData.insert(2, 'Amount(10000)',
WeekdaysSalesData['Amount']/10000,
True)
<!-- ํ๋ จํ์ต์ฉ์ผ๋ก ๋ ์ง๋ฅผ ์ฐ๋, ์, ์ผ๋ก ๋๋๋ค -->
WeekdaysSalesData.insert(4, 'year', '', True)
WeekdaysSalesData.insert(5, 'month', '', True)
WeekdaysSalesData.insert(6, 'day', '', True)
WeekdaysSalesData.insert(7, 'week', '', True)
for i in WeekdaysSalesData['Day'].index:
temp = str(WeekdaysSalesData['Day'].loc[i]).split('-')
year = int(temp[0])
month = int(temp[1])
day = int(temp[2])
WeekdaysSalesData['year'].loc[i] = year
WeekdaysSalesData['month'].loc[i] = month
WeekdaysSalesData['day'].loc[i] = day
WeekdaysSalesData['week'].loc[i] = str(
datetime(year, month, day).isocalendar()[1]
)
createElementDiv(
document,
Element,
'output2'
).write(WeekdaysSalesData)
WeekdaysSalesDataTrain_numpy = WeekdaysSalesData[['Day(timeValue)', 'year', 'month', 'day', 'week']].to_numpy()
WeekdaysSalesDataTest_numpy = WeekdaysSalesData['Amount(10000)'].to_numpy()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = \
train_test_split(
WeekdaysSalesDataTrain_numpy,
WeekdaysSalesDataTest_numpy,
random_state=100,
shuffle=False)
<!-- ์ ํ ํ๊ท ์๊ณ ๋ฆฌ์ฆ -->
<!-- ํ๋ จ, ์ต์ ์ ๊ทธ๋ํ๋ฅผ ์ฐพ์์ค๋ค -->
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train, y_train)
y_train_predict = lr.predict(X_train)
y_test_predict = lr.predict(X_test)
import matplotlib.pyplot as plt
import matplotlib as mat
<!-- ๊ทธ๋ํ -->
fig = plt.figure(
figsize=(15, 7)
)
plt.xticks(WeekdaysSalesData['Day(timeValue)'].to_numpy(), WeekdaysSalesData[['Day']].to_numpy()[:,0], rotation=90)
plt.title('Weekdays Avocado SalesAmount')
plt.plot(
X_train[:,0],
y_train,
marker='o',
color='#c14549',
label='Original'
)
plt.plot(
X_train[:,0],
y_train_predict,
marker='d',
color='blue',
label='Train pattern'
)
plt.plot(
X_test[:, 0],
y_test,
marker='o',
color='#c14549'
)
plt.plot(
X_test[:, 0],
y_test_predict,
marker='d',
color='green',
label='Predict pattern'
)
plt.xlabel('Day')
plt.ylabel('Day(timeValue)')
plt.legend(
shadow=True
)
ax = plt.gca()
<!-- ์ถ๋ง ๊ทธ๋ฆฌ๋ -->
ax.xaxis.grid(True)
<!-- ๋ฐฐ๊ฒฝ์, ๋ง์ง ์กฐ์ -->
ax.set_facecolor('#e8e7d2')
ax.margins(x=0.01, y=0.02)
<!-- ์ฃผ์ ์ด์ํ ์ฌ๋ฐฑ ์์ ๊ธฐ -->
fig.tight_layout()
fig
</py-script>
</body>
</html>
- common.py
def createElementDiv(document, Element, name):
element = document.createElement('div')
element.id = name
document.body.append(element)
return Element(name)
๋๋ ํ๊ธ ์ํด์
์์ด๋ก ๋ฐ๊พธ๋๋ผ ํ๋ค์ด๋ฐ~~~ ใ ใ ใ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (13) (1) | 2022.11.28 |
---|---|
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (12) (0) | 2022.11.28 |
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (10) (1) | 2022.11.25 |
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (9) (1) | 2022.11.25 |
HTML์์ Python์ ์ฌ์ฉํ ์ ์๋ PyScript (8) (0) | 2022.11.25 |