Skip to content

Commit f2f55b6

Browse files
author
minjk-bl
committed
Add ML > DataSets
1 parent 5777932 commit f2f55b6

File tree

3 files changed

+240
-3
lines changed

3 files changed

+240
-3
lines changed

data/m_ml/mlLibrary.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,110 @@ define([
2020
* ]
2121
*/
2222
var ML_LIBRARIES = {
23+
/** Data Sets */
24+
'load_boston': {
25+
name: 'load_boston',
26+
import: 'from sklearn.datasets import load_boston',
27+
code: 'load_boston()',
28+
options: [
29+
30+
]
31+
},
32+
'load_iris': {
33+
name: 'load_iris',
34+
import: 'from sklearn.datasets import load_iris',
35+
code: 'load_iris()',
36+
options: [
37+
38+
]
39+
},
40+
'load_diabetes': {
41+
name: 'load_diabetes',
42+
import: 'from sklearn.datasets import load_diabetes',
43+
code: 'load_diabetes()',
44+
options: [
45+
46+
]
47+
},
48+
'load_digits': {
49+
name: 'load_digits',
50+
import: 'from sklearn.datasets import load_digits',
51+
code: 'load_digits(${n_class})',
52+
options: [
53+
{ name: 'n_class', component: ['input_number'], default: 10, usePair: true },
54+
]
55+
},
56+
'load_linnerud': {
57+
name: 'load_linnerud',
58+
import: 'from sklearn.datasets import load_linnerud',
59+
code: 'load_linnerud()',
60+
options: [
61+
62+
]
63+
},
64+
'load_wine': {
65+
name: 'load_wine',
66+
import: 'from sklearn.datasets import load_wine',
67+
code: 'load_wine()',
68+
options: [
69+
70+
]
71+
},
72+
'load_breast_cancer': {
73+
name: 'load_breast_cancer',
74+
import: 'from sklearn.datasets import load_breast_cancer',
75+
code: 'load_breast_cancer()',
76+
options: [
77+
78+
]
79+
},
80+
'make_classification': {
81+
name: 'make_classification',
82+
import: 'from sklearn.datasets import make_classification',
83+
code: 'make_classification(${n_samples}${n_features}${n_repeated}${n_classes}${shuffle}${random_state}${etc})',
84+
options: [
85+
{ name: 'n_samples', component: ['input_number'], default: 100, usePair: true },
86+
{ name: 'n_features', component: ['input_number'], default: 20, usePair: true },
87+
{ name: 'n_repeated', component: ['input_number'], default: 0, usePair: true },
88+
{ name: 'n_classes', component: ['input_number'], default: 2, usePair: true },
89+
{ name: 'shuffle', component: ['bool_select'], default: 'True', usePair: true },
90+
{ name: 'random_state', component: ['input_number'], placeholder: '123', usePair: true }
91+
]
92+
},
93+
'make_blobs': {
94+
name: 'make_blobs',
95+
import: 'from sklearn.datasets import make_blobs',
96+
code: 'make_blobs(${n_samples}${n_features}${shuffle}${random_state}${etc})',
97+
options: [
98+
{ name: 'n_samples', component: ['input_number'], default: 100, usePair: true },
99+
{ name: 'n_features', component: ['input_number'], default: 20, usePair: true },
100+
{ name: 'shuffle', component: ['bool_select'], default: 'True', usePair: true },
101+
{ name: 'random_state', component: ['input_number'], placeholder: '123', usePair: true }
102+
]
103+
},
104+
'make_circles': {
105+
name: 'make_circles',
106+
import: 'from sklearn.datasets import make_circles',
107+
code: 'make_circles(${n_samples}${shuffle}${noise}${random_state}${factor}${etc})',
108+
options: [
109+
{ name: 'n_samples', component: ['input_number'], default: 100, usePair: true },
110+
{ name: 'shuffle', component: ['bool_select'], default: 'True', usePair: true },
111+
{ name: 'noise', component: ['input_number'], usePair: true },
112+
{ name: 'random_state', component: ['input_number'], placeholder: '123', usePair: true },
113+
{ name: 'factor', component: ['input_number'], default: 0.8, usePair: true }
114+
]
115+
},
116+
'make_moons': {
117+
name: 'make_moons',
118+
import: 'from sklearn.datasets import make_moons',
119+
code: 'make_moons(${n_samples}${shuffle}${noise}${random_state}${etc})',
120+
options: [
121+
{ name: 'n_samples', component: ['input_number'], default: 100, usePair: true },
122+
{ name: 'shuffle', component: ['bool_select'], default: 'True', usePair: true },
123+
{ name: 'noise', component: ['input_number'], usePair: true },
124+
{ name: 'random_state', component: ['input_number'], placeholder: '123', usePair: true }
125+
]
126+
},
23127
/** Data Preparation - Encoding */
24128
'prep-onehot': {
25129
name: 'OneHotEncoder',

html/m_ml/dataSets.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<body>
2+
<div class="vp-grid-box">
3+
<div class="vp-grid-border-box">
4+
<div class="vp-grid-col-110">
5+
<label for="loadType" class="vp-orange-text">Load type</label>
6+
<select id="loadType" class="vp-select vp-state">
7+
<!-- Auto Create -->
8+
</select>
9+
</div>
10+
<div class="vp-grid-col-110 vp-data-option-box">
11+
12+
</div>
13+
</div>
14+
<div class="vp-grid-border-box vp-grid-col-110">
15+
<label for="allocateTo" class="vp-orange-text">Allocate to</label>
16+
<input type="text" class="vp-input vp-state" id="allocateTo" placeholder="New variable name" value="ldata"/>
17+
</div>
18+
</div>
19+
</body>

js/m_ml/DataSets.js

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
// [CLASS] DataSets
1414
//============================================================================
1515
define([
16+
'text!vp_base/html/m_ml/dataSets.html!strip',
1617
'vp_base/js/com/com_util',
1718
'vp_base/js/com/com_Const',
1819
'vp_base/js/com/com_String',
1920
'vp_base/js/com/component/PopupComponent',
20-
], function(com_util, com_Const, com_String, PopupComponent) {
21+
'vp_base/js/com/com_generatorV2',
22+
'vp_base/data/m_ml/mlLibrary',
23+
], function(dsHTML, com_util, com_Const, com_String, PopupComponent, com_generator, ML_LIBRARIES) {
2124

2225
/**
2326
* DataSets
@@ -29,15 +32,126 @@ define([
2932
this.config.dataview = false;
3033

3134
this.state = {
32-
35+
loadType: 'load_boston',
36+
userOption: '',
37+
allocateTo: 'ldata',
3338
...this.state
3439
}
40+
41+
this.mlConfig = ML_LIBRARIES;
42+
this.loadTypeList = {
43+
'Load Data': [
44+
'load_boston', 'load_iris', 'load_diabetes', 'load_digits', 'load_linnerud', 'load_wine', 'load_breast_cancer'
45+
],
46+
'Create Data': [
47+
'make_classification', 'make_blobs', 'make_circles', 'make_moons'
48+
]
49+
}
50+
51+
}
52+
53+
_bindEvent() {
54+
super._bindEvent();
55+
let that = this;
56+
57+
// select model
58+
$(this.wrapSelector('#loadType')).on('change', function() {
59+
let loadType = $(this).val();
60+
that.state.loadType = loadType;
61+
$(that.wrapSelector('.vp-data-option-box')).html(that.templateForOption(loadType));
62+
63+
// change allocateTo default variable name
64+
if (that.loadTypeList['Load Data'].includes(loadType)) {
65+
$(that.wrapSelector('#allocateTo')).val('ldata');
66+
that.state.allocateTo = 'ldata';
67+
} else {
68+
$(that.wrapSelector('#allocateTo')).val('df');
69+
that.state.allocateTo = 'df';
70+
}
71+
});
3572
}
3673

3774
templateForBody() {
38-
return 'Data Set test';
75+
let page = $(dsHTML);
76+
77+
let that = this;
78+
// load types
79+
let loadTypeTag = new com_String();
80+
Object.keys(this.loadTypeList).forEach(category => {
81+
let optionTag = new com_String();
82+
that.loadTypeList[category].forEach(opt => {
83+
let optConfig = that.mlConfig[opt];
84+
let selectedFlag = '';
85+
if (opt == that.state.modelType) {
86+
selectedFlag = 'selected';
87+
}
88+
optionTag.appendFormatLine('<option value="{0}" {1}>{2}</option>',
89+
opt, selectedFlag, optConfig.name);
90+
})
91+
loadTypeTag.appendFormatLine('<optgroup label="{0}">{1}</optgroup>',
92+
category, optionTag.toString());
93+
});
94+
$(page).find('#loadType').html(loadTypeTag.toString());
95+
96+
// render option page
97+
$(page).find('.vp-data-option-box').html(this.templateForOption(this.state.loadType));
98+
99+
return page;
39100
}
40101

102+
templateForOption(loadType) {
103+
let config = this.mlConfig[loadType];
104+
let state = this.state;
105+
106+
let optBox = new com_String();
107+
// render tag
108+
config.options.forEach(opt => {
109+
optBox.appendFormatLine('<label for="{0}" title="{1}">{2}</label>'
110+
, opt.name, opt.name, opt.name);
111+
let content = com_generator.renderContent(this, opt.component[0], opt, state);
112+
optBox.appendLine(content[0].outerHTML);
113+
});
114+
115+
// show user option
116+
if (config.code.includes('${etc}')) {
117+
// render user option
118+
optBox.appendFormatLine('<label for="{0}">{1}</label>', 'userOption', 'User option');
119+
optBox.appendFormatLine('<input type="text" class="vp-input vp-state" id="{0}" placeholder="{1}" value="{2}"/>',
120+
'userOption', 'key=value, ...', this.state.userOption);
121+
}
122+
return optBox.toString();
123+
}
124+
125+
generateCode() {
126+
let { loadType, userOption, allocateTo } = this.state;
127+
let code = new com_String();
128+
let config = this.mlConfig[loadType];
129+
code.appendLine(config.import);
130+
code.appendLine();
131+
132+
// model code
133+
let modelCode = config.code;
134+
modelCode = com_generator.vp_codeGenerator(this, config, this.state, userOption);
135+
136+
if (this.loadTypeList['Load Data'].includes(loadType)) {
137+
code.appendFormatLine('{0} = {1}', allocateTo, modelCode);
138+
// FIXME: decide between 2 codes
139+
// code.appendFormat("df_{0} = pd.concat([pd.DataFrame({1}.data, columns={2}.feature_names), pd.DataFrame({3}.target, columns=['target'])], axis=1)", allocateTo, allocateTo, allocateTo, allocateTo);
140+
code.appendFormat("df_{0} = pd.DataFrame(np.hstack(({1}.data, {2}.target.reshape(-1,1))), columns=np.hstack(({3}.feature_names, ['target'])))", allocateTo, allocateTo, allocateTo, allocateTo);
141+
} else {
142+
code.appendFormatLine("_X, _y = {0}", modelCode);
143+
code.appendLine("_columns = np.hstack((['X{}'.format(i+1) for i in range(len(_X[0]))],['target']))");
144+
code.appendFormat("{0} = pd.DataFrame(np.hstack((_X, _y.reshape(-1,1))), columns=_columns)", allocateTo);
145+
}
146+
147+
if (allocateTo != '') {
148+
code.appendLine();
149+
code.append(allocateTo);
150+
}
151+
152+
153+
return code.toString();
154+
}
41155
}
42156

43157
return DataSets;

0 commit comments

Comments
 (0)