Skip to content

Commit cc8cdf3

Browse files
author
xuming06
committed
add xlearn demo.
1 parent 775df49 commit cc8cdf3

6 files changed

Lines changed: 482 additions & 1 deletion

File tree

21xlearn/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:

21xlearn/example_lr_iris.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:
4+
import time
5+
import datetime
6+
import xlearn as xl
7+
from sklearn.datasets import load_iris
8+
from sklearn.model_selection import train_test_split
9+
from sklearn.linear_model import LogisticRegression
10+
11+
# Load dataset
12+
iris_data = load_iris()
13+
X = iris_data['data']
14+
y = (iris_data['target'] == 2)
15+
16+
X_train, \
17+
X_val, \
18+
y_train, \
19+
y_val = train_test_split(X, y, test_size=0.3, random_state=0)
20+
21+
# xlearn
22+
# param:
23+
# 0. binary classification
24+
# 1. model scale: 0.1
25+
# 2. epoch number: 10 (auto early-stop)
26+
# 3. learning rate: 0.1
27+
# 4. regular lambda: 1.0
28+
# 5. use sgd optimization method
29+
linear_model = xl.LRModel(task='binary', init=0.1,
30+
epoch=10, lr=0.1,
31+
reg_lambda=1.0, opt='sgd')
32+
print(datetime.datetime.now())
33+
# Start to train
34+
linear_model.fit(X_train, y_train,
35+
eval_set=[X_val, y_val],
36+
is_lock_free=False)
37+
38+
# print model weights
39+
# print(linear_model.weights)
40+
41+
# Generate predictions
42+
y_pred = linear_model.predict(X_val)
43+
print(datetime.datetime.now())
44+
# sklearn
45+
lr = LogisticRegression()
46+
lr.fit(X_train, y_train)
47+
print('{0}, val mean acc:{1}'.format(lr.__str__(), lr.score(X_val, y_val)))
48+
print(datetime.datetime.now())

21xlearn/xlearn_demo.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:
4+
import xlearn as xl
5+
6+
# Training task
7+
ffm_model = xl.create_ffm() # Use field-aware factorization machine
8+
ffm_model.setTrain("../data/xlearn_data/small_train.txt") # Training data
9+
ffm_model.setValidate("../data/xlearn_data/small_test.txt") # Validation data
10+
11+
# param:
12+
# 0. binary classification
13+
# 1. learning rate: 0.2
14+
# 2. regular lambda: 0.002
15+
# 3. evaluation metric: accuracy
16+
param = {'task':'binary', 'lr':0.2,
17+
'lambda':0.002, 'metric':'acc'}
18+
19+
# Start to train
20+
# The trained model will be stored in model.out
21+
ffm_model.fit(param, './model.out')
22+
23+
# Prediction task
24+
ffm_model.setTest("../data/xlearn_data/small_test.txt") # Test data
25+
ffm_model.setSigmoid() # Convert output to 0-1
26+
27+
# Start to predict
28+
# The output result will be stored in output.txt
29+
ffm_model.predict("./model.out", "./output.txt")

0 commit comments

Comments
 (0)