/
learn_to_multiply.py
62 lines (47 loc) · 2.18 KB
/
learn_to_multiply.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import pandas
import random
import lightwood
from lightwood import Predictor
import os
from sklearn.metrics import r2_score
import numpy as np
lightwood.config.config.CONFIG.HELPER_MIXERS = False
random.seed(66)
### Generate a dataset
n = 100
m = n * 100
op = '*'
# generate random numbers between -10 and 10
data_train = {'x': [random.randint(-15, 5) for i in range(n)],
'y': [random.randint(-15, 5) for i in range(n)]}
data_test = {'x': [random.randint(-15, 5) for i in range(m)],
'y': [random.randint(-15, 5) for i in range(m)]}
if op == '/':
for i in range(n):
if data_train['y'][i] == 0:
data_train['y'][i] = 1
if op == '/':
for i in range(m):
if data_test['y'][i] == 0:
data_test['y'][i] = 1
# target variable to be the multiplication of the two
data_train['z'] = eval(f"""[data_train['x'][i] {op} data_train['y'][i] for i in range(n)]""")
data_test['z'] = eval(f"""[data_test['x'][i] {op} data_test['y'][i] for i in range(m)]""")
df_train = pandas.DataFrame(data_train)
df_test = pandas.DataFrame(data_test)
predictor = Predictor(output=['z'])
def iter_function(epoch, training_error, test_error, test_error_gradient, test_accuracy):
print(f'Epoch: {epoch}, Train Error: {training_error}, Test Error: {test_error}, Test Error Gradient: {test_error_gradient}, Test Accuracy: {test_accuracy}')
predictor.learn(from_data=df_train, callback_on_iter=iter_function, eval_every_x_epochs=200)
predictor.save('ok.pkl')
predictor = Predictor(load_from_path='ok.pkl')
print('Train accuracy: ', predictor.train_accuracy)
print('Test accuracy: ', predictor.calculate_accuracy(from_data=df_test))
predictions = predictor.predict(when_data=df_test)
print('Confidence mean for both x and y present: ', np.mean(predictions['z']['selfaware_confidences']))
print(list(df_test['z'])[30:60])
print(predictions['z']['predictions'][30:60])
predictions = predictor.predict(when_data=df_test.drop(columns=['x']))
print('Confidence mean for x missing: ', np.mean(predictions['z']['selfaware_confidences']))
predictions = predictor.predict(when_data=df_test.drop(columns=['y']))
print('Confidence mean for y missing: ', np.mean(predictions['z']['selfaware_confidences']))