-
statsmodels-js - MLR 구현ML 2023. 1. 8. 09:32
- 참고
https://github.com/egusahiroaki/statsmodels-js
- code
import React from 'react'; import './App.css'; function App() { const Stats = require("statsmodels-js"); //예시 data const x = [ [10, 20, 30], [20, 42, 63], [4, 8, 16], ]; const y = [30, 50, 70]; const xTest = [ [10, 20, 30], [1, 2, 3], ]; // 모델학습 const model = new Stats.MultipleLinearRegression(x, y).fit(); // 결과 let modelSummary = model.summary(); console.log(modelSummary); // 회귀식 출력을 위한 컬럼명 출력 // let columns = modelSummary.keys(); // console.log(columns) // 회귀식 만들기 let str_fml = 'y = '; for(let prop in modelSummary){ if (prop == 'intercept'){ str_fml += `${modelSummary[prop]}` } else if (prop == 'r2Score'){ str_fml += '' } else{ str_fml += `${Math.round(modelSummary[prop],2)}*${prop} + ` } } console.log(str_fml) // 예측 let result = model.predict(xTest); function makeRound(x){ return Math.round(x*100 + Number.EPSILON) / 100 } // let result_round = result.map(x => Math.round(x)); // let result_round = result.map(x => Math.round(x*100 + Number.EPSILON) /100 ); let result_round = result.map(x => makeRound(x)); let r2 = modelSummary.r2Score; console.log(result_round); return ( <div className="App"> <header className="App-header"> <h3>test statsmodels-js</h3> <p>pred_y_values: {result_round.join(', ')}</p> <p>r2: {r2}</p> <p>formula: {str_fml}</p> </header> </div> ); } export default App;
'ML' 카테고리의 다른 글
windows 가상머신(Ubuntu) CUDA 설정방법 (0) 2023.06.09 sklearn 회귀모델 r2-score 측정 (1) 2023.01.08