2. Alex Gorbachev
• Chief Technology Officer at Pythian
• Blogger
• Cloudera Champion of Big Data
• OakTable Network member
• Oracle ACE Director
• Founder of BattleAgainstAnyGuess.com
• Founder of Sydney Oracle Meetup
• IOUG Director of Communities
• EVP, Ottawa Oracle User Group
3. Agenda
• What’s Machine Learning
– Typical Machine Learning applications
• Why using Oracle Database for
Machine Learning
• Practical examples
– Classifying PL/SQL code
– Classifying database schemas into good
and bad
– SQL statements clustering
– Detecting anomalies in database
workload
9. Tom Mitchell’s definition
• Machine Learning is the study of computer
algorithms that improve automatically through
experience.
!
• A computer program is said to learn from
experience E with respect to some task T and some
performance measure P, if its performance on T, as
measured by P, improves with experience E.
14. Classes of ML algorithms
• Supervised learning
– Input: data + known facts; Output - predictions
• Unsupervised learning
– Input: data; Output – hypothesis
!
– Other less common algorithms such as reinforcement
learning, recommenders and etc
21. Machine Learning in Oracle DB?
• That’s where the data is
• Data in an RDBMS is often clean
• Easy to transform data with SQL
• Powerful algorithms implemented
– Oracle Data Mining option
– Analytic SQL
22. Machine Learning by example
Applying Machine Learning
to the business of DBAs
23. Problem: Detect bad PL/SQL
• Goal: automated PL/SQL code grading
– Classify as Good or Bad
• Typical classification task
– Assignment of labels to the set of unlabeled items
based on prior observations
24. Classification process
• Parse input data
• Extract features
– Manually or automatically or they are clearly defined (if
row is an item, columns may be features)
• Train – calculate model based on labeled input
• Verify – test model on labeled input
• Apply labels to unlabeled input
!
• Classification is supervised learning
28. PL/SQL code features
• Automatically extract words from the text as
features (tokenize)
– EASY TO AUTOMATE
• Assign features intelligently
– Code size
– Author
– Percent of comment lines
– Presence of specific code patterns
– DIFFICULT TO AUTOMATE
29. Classification model workflow
1. Create Oracle Text policy (define lexer)
2. Configure and build the model on training set
3. Apply model to the testing set
4. Assess model performance
5. Adjust model settings/features/size and repeat
32. Basic probability lesson
• p(A) is the probability that A is true
• Axioms of Probability
!
!
!
!
• Bayes Law
33. How Bayes Law can work for us?
!
!
!
• A – presence of a feature
like WHEN OTHERS THEN NULL in PL/SQL
• B – bad PL/SQL code
A
B
Area is 1
B|A
34. PL/SQL data source
• OBJECT_ID – case ID
• CODE – text column
• TARGET_VALUE – 0 is good and 1 is bad
• Training set
– where mod(object_id, 10) < 5
• Testing set
– where mod(object_id, 10) >= 5
35. Oracle Text policy
begin
begin
ctx_ddl.drop_policy('plsql_nb_policy');
exception when others then null;
end;
begin
ctx_ddl.drop_preference('plsql_nb_lexer');
exception when others then null;
end;
ctx_ddl.create_preference
('plsql_nb_lexer’, 'BASIC_LEXER');
ctx_ddl.create_policy
('plsql_nb_policy', lexer=>'plsql_nb_lexer');
end;
/
36. Model settings
CREATE TABLE plsql_nb_settings (
setting_name VARCHAR2(30),
setting_value VARCHAR2(4000));
BEGIN
-- Populate settings table
INSERT INTO plsql_svm_settings VALUES
(dbms_data_mining.algo_name, dbms_data_mining.algo_naive_bayes);
INSERT INTO plsql_nb_settings VALUES
(dbms_data_mining.prep_auto, dbms_data_mining.prep_auto_on);
INSERT INTO plsql_nb_settings VALUES
(dbms_data_mining.odms_text_policy_name, 'plsql_nb_policy');
-- INSERT INTO plsql_nb_settings VALUES
-- (dbms_data_mining.NABS_PAIRWISE_THRESHOLD,0.01);
-- INSERT INTO plsql_nb_settings VALUES
-- (dbms_data_mining.NABS_SINGLETON_THRESHOLD,0.01);
COMMIT;
END;
/
37. Build model
DECLARE
xformlist dbms_data_mining_transform.TRANSFORM_LIST;
BEGIN
BEGIN DBMS_DATA_MINING.DROP_MODEL('PLSQL_NB');
EXCEPTION WHEN OTHERS THEN NULL; END;
!
dbms_data_mining_transform.SET_TRANSFORM(
xformlist, 'code', null, 'code', null, 'TEXT(TOKEN_TYPE:NORMAL)');
!
DBMS_DATA_MINING.CREATE_MODEL(
model_name => 'PLSQL_NB',
mining_function => dbms_data_mining.classification,
data_table_name => 'plsql_build',
case_id_column_name => 'object_id',
target_column_name => 'target_value',
settings_table_name => 'plsql_nb_settings',
xform_list => xformlist);
END;
/
38. Test model
SELECT
target_value AS actual_target,
PREDICTION(plsql_nb USING *) AS predicted_target,
COUNT(*) AS cases_count
FROM plsql_test
GROUP BY target_value,
PREDICTION(plsql_nb USING *)
ORDER BY 1, 2;
42. Thanks and Q&A
Contact info
gorbachev@pythian.com
+1-877-PYTHIAN
To follow us
pythian.com/blog
@alexgorbachev
@pythian
linkedin.com/company/pythian