I am going to give a talk at Department of Computer Science, the University of Massachusetts Boston, on January 31. The following is the abstract: Abstract: Will a new mobile phone customer pay her monthly bill? Is a check deposited into a bank customer’s account fraudulent? Will an insurance claim become expensive? For nearly two decades, Dr. Jiang (Jay) Zhou has been helping mobile phone service providers, banks, insurance companies and other businesses solve problems like these. The predictive models that he built have resulted in over $200 million savings for clients. In the seminar, Dr. Zhou will discuss topics including challenges and best practices when building these predictive models in real world environments.
Popular Topics
Popular Topics
Thursday, January 26, 2017
Wednesday, October 26, 2016
Ranking High in Kaggle Competition is a Huge Advantage for Job Seekers
For someone who is looking for a job in data analytics field, high rankings in Kaggle Competition will give him tremendous advantage. Employers see the competition winners having strong problem solving skills and hands-on expertise. Indeed, to be able to complete some projects for Kaggle competitions, participants have to put in a lot of effort to deal with data issues even before any predictive models can be built. This is very similar to that in the real world applications where 80% of time is spent on data cleanse and manipulation. It is not a surprise that some employers prefer Kaggle competition winners over PhD graduates whose skills are perceived as more theoretical. Taking my nephew, Yuyu Zhou, as an example. He got a master degree in data analytics. While he was in school, he spent a few weeks with other classmates to participate in Kaggle competitions. His team has achieved the top 3% and 5% in two Kaggle prediction competitions respectively (see my blog post a Young Data Scientist- Kaggle Competition Top 5% Winner: Yuyu Zhou. Once he graduated, he was quickly hired by a prestigious company and has been earning PhD level salary. Those few weeks he spent on Kaggle Competition was the best time investment of his life.
Thursday, October 20, 2016
Oracle Ora_Hash function- Part 1 Random Sampling
Oracle ora_hash() is a very useful function. I have used it for different purposes such as generating random number. The following query generate 5 buckets from 0 to 4, each of them have the similar number of records. First, we create a table and populate it with 1,000 numbers.
create table t_n (n number); begin for i in 1..1000 loop insert into t_n values(i); end loop; commit; end;In the query below, the parameter 5 of ora_hash defines the number of buckets is 5. As we see, each bucket has simlilar number of records.
with tbl as ( select ora_hash(n, 5) bucket, n from t_n) select bucket, count(*), min(n), max(n) from tbl group by bucket order by 1; BBUCKET COUNT(*) MIN(N) MAX(N) 0 154 2 993 1 164 7 999 2 168 6 991 3 175 4 995 4 173 8 1000
Saturday, July 02, 2016
SQL Listagg Function
Listagg is a useful function to combine column values from different rows. In a sense, it is similar to sum() function that aggregates information across records. To show how listagg works, first we create a table that has record id, group id and words.
create table t(id number, gp number, word varchar2(128)); insert into t values(1,1,'Hello'); insert into t values(2,1,'World!'); insert into t values(3,2,'Good'); insert into t values(4,2,'Morning!');
select * from t; ID GP WORD 1 1 Hello 2 1 World! 3 2 Good 4 2 Morning!We can combine the words by group using listagg function.
select gp, listagg(word,' ') within group(order by id) from t group by gp order by gp; GP LISTAGG(WORD,'')WITHINGROUP(ORDERBYID) 1 Hello World! 2 Good Morning!
Saturday, June 11, 2016
Run Oracle SQL Online
I have found an excellent website to learn Oracle PL/SQL Oracle SQL live. From the website, we can run SQL or PL/SQL commands. There are also many scripts in Code Library that we can learn from. Here are screenshots.
Wednesday, May 11, 2016
Featured Company: ZSAnalytics
- ZSales: 70% increase in response rate
- ZClaim: 40% reduction in insurance claim losses
- ZCheck: 70% reduction in charge off
- ZCredit: 50% reduction in delinquency rate
Sunday, March 06, 2016
the Danger of Predictive Model Overfitting
In the post a Young Data Scientist- Kaggle Competition Top 5% Winner: Yuyu Zhou, Yuyu talks about the important role of feature engineering, i.e, finding good derived variables, and gradient boosting trees in their success. He also tells me a very interesting observations on Kaggle Competition ranking.
"After participating teams finished building their predictive models, they apply their models to two data sets to generate predictions: a smaller set containing target variable and a larger data set where the target variable is removed. Each participating team's model is temporarily ranked based on the result on the smaller data set with the target variable. After the competition's deadline is due, Kaggle will calculate the final ranking of each team based on a model's prediction on the larger data set. "
"It is interesting to see that the rankings of some top 1% models based on the smaller data set drop more than 20% on the larger data set. I figure out what might cause the huge discrepancies in their model performance. Those teams' models fit the smaller data set so well that they lose their capability to generalize. It is a typical overfitting problem."It is important to avoid model overfitting. After all, a predictive model is only useful if it can generalize, i.e., able to handle new data reasonably well.
Saturday, March 05, 2016
a Young Data Scientist- Kaggle Competition Top 5% Winner: Yuyu Zhou
Yuyu Zhou is a graduate student in Analytics in University of New Hampshire. His team has achieved the top 3% and 5% in two Kaggle prediction competitions respectively. In an interview, I asked him how their predictive models performed so well. Yuyu said,
"One of the keys to the success is that we spend tremendous amount of time working on building feature variables. Those variables are usually the results of combining several raw variables. For example, the ratio between the body weight and height is a better variable in predicting a patient's health than using body weight or height alone."
"My training in computer science is extremely helpful in these projects. I am able to write Java, Python and SQL scripts to perform tasks such as data cleansing, data merge, and data transform, etc. As we know, more than 80% of time in a project is typically spent on those tasks before we start building predictive models."
"We have tried many type of predictive models and found that gradient boosting trees have consistently perform the best."
The following is a summary of Yuyu's contribution in those two projects.
Kaggle Competition: Rossmann Store Sales Prediction (ranked top 5%) Oct 2015 – Dec 2015
- Built the Predictive Model for daily sales for Rossmann Stores using Python Machine Learning library.
- Conducted data cleaning and feature engineering for increasing data quality.
- Designed final prediction model by combining the multiple gradient boosting trees algorithms
- Prediction accuracy was ranked at 163 out of 3303 teams
Kaggle Competition: Property Risk Level Prediction (ranked top 3%) July 2015 – Aug 2015
- Developed Statistics models to predict risk level of properties which Liberty Mutual Inc is going to protect.
- Led the team and conducted cost and benefit analysis on new ideas.
- Implemented ideas using statistical packages from Python.
- Prediction accuracy was ranked at 71 out of 2236 teams.
Friday, March 04, 2016
Find Tablespaces for Oracle Tables
We may run the following query from SYS to find the tablespace names for all tables, including IOTs (index organized table) and partitioned tables (replacing 'DMUSER' with your user name).
select u.name owner, o.name table_name,t.name tablspace_name
from
obj$ o, ts$ t, sys_objects s, user$ u
where o.obj#=s.object_id and s.ts_number=t.ts#
and o.owner#=u.user#
and o.type#=2
and u.name='DMUSER'
order by 1,2;
OWNER TABLE_NAME TABLSPACE_NAME
-------------------------------- --------------------------------
DMUSER AI_EXPLAIN_OUTPUT TBS_1
DMUSER AR_SH_SAMPLE_SETTINGS TBS_1
DMUSER DM$P0AR_SH_SAMPLE TBS_1
DMUSER DM$P0AR_SH_SAMPLE_2COL TBS_1
DMUSER DM$P0EM_SH_CLUS_SAMPLE TBS_1
DMUSER DM$P0NB_SH_CLAS_SAMPLE TBS_1
DMUSER DM$P0OC_SH_CLUS_SAMPLE TBS_1
DMUSER DM$P1EM_SH_CLUS_SAMPLE TBS_1
DMUSER DM$P1NB_SH_CLAS_SAMPLE TBS_1
DMUSER DM$P1OC_SH_CLUS_SAMPLE TBS_1
Wednesday, March 02, 2016
Wednesday, February 17, 2016
Dr. Jiang Zhou's Presentation at UNH Analytics
I shared my analytics experience with a group of students and professors from University of New Hampshire Analytics on February 25. I really enjoyed the interaction with professors and students. The following is the news on UNH Graduate Analytics LinkedIn page.
UNH Graduate Analytics would like to thank Dr. Jiang Zhou for his wonderful presentation this morning, which was part of our Analytics Speaker Series.Dr. Zhou highlighted the advantages and disadvantages of using statistical models vs. intuition to solve business problems throughout his 10+ years of experience. Among his many accomplishments, Dr. Zhou built a fraud detection model for large banks, which detects 70% more fraud than using business intuition rules alone.
Free Data Sources
Bernard Marr wrote a post Big Data: 33 Brilliant And Free Data Sources For 2016. He gives a list of free data sources provided by governments, social websites and companies including Google and Facebook. The data types include census, climate, financial, health, news, etc. I have found the list very interesting. Springboard "19 Free Public Data Sets For Your First Data Science Project" has good information about free data sets such as United States Census Data, FBI Crime Data and CDC Cause of Death etc.
Friday, December 25, 2015
Convert Values Using Decode Function
We use decode() function to convert a value to other values. We have the following table.
SQL> select * from tbl_test order by id;
ID MESSAGE
------- --------------------------------
1 A
2 B
3 C
4 D
The following query transforms characters values into numbers or weights.
SQL> select a.*, decode(message, 'A', 0.3, 'B',0.4, 0.35) weight from tbl_test a order by id;
ID MESSAGE WEIGHT
------- -------------------------------- ----------
1 A .3
2 B .4
3 C .35
4 D .35
In the above query, A is mapped to 0.3, B to 0.4. All other values are mapped to the default value of 0.35. If the default value is not defined, other characters where the mapping is not defined will be given a NULL value.
SQL> select a.*, decode(message, 'A', 0.3, 'B',0.4) weight from tbl_test a order by id;
ID MESSAGE WEIGHT
------- -------------------------------- ----------
1 A .3
2 B .4
3 C
4 D
I have used decode function to convert categorical values into weights that are derived from a logistic regression model. For example, I used the following query to convert transaction code into weights. Before decode function is applied, CARD_TXN_CDE is cleaned up using trim, substr and nvl functions. The trim function removes leading and trailing blanks. The substr function extracts the first 18 characters of the string. The nvl function converts NULL values to blank.
decode(nvl(substr( trim(CARD_TXN_CDE),1,18),' '), ' ',-.660841, '01',-.518927, '1',-.076546, '10',-.294631, '12',.077699, '14',-.709884, '25',-.30619, '40',.021855, '51',-.004593, '52',-.069521, '53',-.000344, '57',.570421, '59',.858444, '67',1.481654, '81',.29988, '91',-.004755, '96',.02628, 'AD',-.001036, 'AK',-.150162, 'CB',-.001588, 'D5',.364975, 'G',-.015795, 'H',-.274374, 'I',-1.065177, 'J',-.027991, 'N',.157622, 'NK',.763406, 'NR',.080558, 'P',-.961133, 'PE',-.19558, 'PH',.479081, 'PR',.134741, 'S',-.239287, 'SV',.475934, 'T1',.241061, 'T2',-.277572, 'T3',.901487, 'T4',-.0137, 'TS',.01362, 'U2',-.186914, 'X',1.301152, 'XX',-.11462, 'Z0',-.7141, 'Z4',-.004642, 'Z6',-.014541, 0)
Thursday, July 30, 2015
Remove the Rows with Lowest or Highest Values
In a project, I need to remove rows with lowest or highest values within groups. I used the following table to illustrate the problem. For grp 10, rows with val 1 and 4 need to be removed and for grp 11, rows with val 5.5 and 9.6.
SQL> select * from tbl_x2 order by 1,2;
GRP VAL
---------- ----------
10 1
10 2
10 3
10 4
11 5.5
11 7.3
11 9.6
7 rows selected.
I used the following SQL statement to do it.
delete from tbl_x2 a where a.rowid in ( select rid from ( with tbl as ( select b.rowid rid, row_number() over(partition by grp order by val) rnk, row_number() over(partition by grp order by val desc) rnkr from tbl_x2 b) select rid from tbl where rnk=1 or rnkr=1 ) );The following query shows that rows with lowest or highest values within groups are removed.
SQL> select * from tbl_x2 order by 1,2;
GRP VAL
---------- ----------
10 2
10 3
11 7.3
The trick is to use row_number() function to generate ranks or reverse ranks based on val for each grp. For example, the following query show all rows in the original table and the ranks (rnk) and reversed ranks (rnkr). The lowest val has a rank of 1 and the highest val has a reversed rank of 1.
select b.*,
row_number() over(partition by grp order by val) rnk,
row_number() over(partition by grp order by val desc) rnkr
from tbl_x3 b
GRP VAL RNK RNKR
---------- ---------- ---------- ----------
10 1 1 4
10 2 2 3
10 3 3 2
10 4 4 1
11 5.5 1 3
11 7.3 2 2
11 9.6 3 1
Friday, April 10, 2015
Extract SQL Source Code For Objects From Datapump Dumpfile
When we dump the whole schema as a dumpfile, the SQL statements for creating those database objects (DDL) can be extracted from the dumpfile. First, we create the dumpfile using Oracle datapump export utility expdp.
expdp myuser/myuser dumpfile=myuserdump.dmp directory=myuser_dirWe use Oracle datapump import utility impdp and specify "sqlfile=".
impdp myuser/myuser dumpfile=myuserdump.dmp directory=myuser_dir sqlfile=myuserdump.sqlWhen "sqlfile=" is used, data import is not actually done. Instead, it produces a file containing the SQL statements to create database objects. For example, the following "create table" and "create index" are taken from the sqlfile produced by impdp. We can simple run those SQL queries to create objects.
CREATE TABLE "MYUSER"."DMPZKM"
( "CLUSTER_ID" NUMBER,
"RECORD_COUNT" NUMBER,
"PARENT" NUMBER,
"TREE_LEVEL" NUMBER,
"DISPERSION" BINARY_DOUBLE,
"NORM" BINARY_DOUBLE,
CONSTRAINT "DMPSKM" PRIMARY KEY ("CLUSTER_ID") ENABLE
) ORGANIZATION INDEX NOCOMPRESS PCTFREE 10 INITRANS 2 MAXTRANS 255 LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT);
CREATE UNIQUE INDEX "MYUSER"."DMPQKM" ON "MYUSER"."DMPXKM"
("CLUSTER_ID", "ATTRIBUTE_NAME", "ATTRIBUTE_SUBNAME", "BIN_ID")
PCTFREE 10 INITRANS 2 MAXTRANS 164
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT)
TABLESPACE "DMUSER" PARALLEL 1 ;
Sunday, April 05, 2015
Table Joining Using Function Based Index
The post Create Function Based Index on Oracle Table shows that indexes can be created based on functions. Here is an example of the actual use of function based index in table joining. In a project, I try to join the following two tables based on a unique sequence numbers: 1. Checks deposited, tbl_checks_deposited. 2. Checks that are returned, tbl_checks_returned. Both tables contain column sequence_ids that are unique for each record and are supposed to link these two tables. I used the following query to join them.
select a.*, b.return_code from tbl_checks_deposited a, tbl_checks_returned b where a.sequence_id=b.sequence_id;However, I did not get many successful matches. After some investigation, I have found that the sequence_id in the second table tbl_checks_returned has extra leading and trailing blanks. This could happen when the data is loaded from text files into an Oracle database. I used trim() function to remove the leading and trailing blanks and redo the join using the following query. To make the join faster, I first create a function based index on table tbl_checks_returned.
create index tbl_checks_returned_idx on tbl_checks_returned(trim(sequence_id)); select a.*, b.return_code from tbl_checks_deposited a, tbl_checks_returned b where a.sequence_id=trim(b.sequence_id);Now the join returns the correct number of records.
Remove Character at Specific Location in String
In the post Remove the Trailing Character From a String Using SQL, we use substr SQL function. To remove a character at a specific location in a string, I create a function that takes advantage of substr again as shown below.
create or replace function remove_at( str IN varchar2, pos IN number) return varchar2 is begin return substr(str,1,pos-1)||substr(str, pos+1, length(str)); end; /Now, I apply this function using different locations and the results look correct.
SQL> select id, val from tbl_val;
ID VAL
------- --------------------------------
1 abcdef
SQL> select id, remove_at(val,3) val from tbl_val;
ID VAL
------- --------------------------------
1 abdef
SQL> select id, remove_at(val,5) val from tbl_val;
ID VAL
------- --------------------------------
1 abcdf
SQL> select id, remove_at(val,1) val from tbl_val;
ID VAL
------- --------------------------------
1 bcdef
SQL> select id, remove_at(val,0) val from tbl_val;
ID VAL
------- --------------------------------
1 abcdef
SQL> select id, remove_at(val,10) val from tbl_val;
ID VAL
------- --------------------------------
1 abcdef
Saturday, April 04, 2015
Remove the Trailing Character From a String Using SQL
We can using SQL function substr to remove the last character from a string. For example, the following query shows there is a extra comma at the end of the string. Please also see Remove Character at Specific Location in String.
SQL> select * from tbl_test;
ID VAL
------- --------------------------------
1 Hello,
The following query will remove it. Substr takes three parameters. The first parameter is the string to process, the second is the starting position and the third is the length of the substring to extract. Here, we define the length to be the total length of the original string minus one.
SQL> select id, substr(val, 1, length(val)-1) val from tbl_test;
ID VAL
------- --------------------------------
1 Hello
Convert Oracle Number to Hex and Vice Verse
To convert a hexadecimal number (base 16) to a decimal number, we use to_number function and specify the format as 'XXXXX' as shown below.
SQL> select to_number('30003','XXXXXX') from dual;
TO_NUMBER('30003','XXXXXX')
---------------------------
196611
We can verify the conversion by using the following query.The fifth position from the right represents 16 to the 4th power.
SQL> select power(16,4)*3+3 from dual;
POWER(16,4)*3+3
---------------
196611
To convert a decimal number to a hex number, we use to_char() function.
SQL> select to_char(196611, 'XXXXXX') from dual; TO_CHAR ------- 30003
Friday, April 03, 2015
Chi-square Test in Oracle Database
When we want to test the association of two categorical variables, such as sex and certain disease, we may use Chi-square test. Fortunately, with Oracle statistical function stats_crosstab, this can be done using a SQL query. For example, we have a table containing two columns of discrete values, sex and target.
SQL> select sex, target, count(*) num from tbl_chiqs group by sex, target order by sex, target; S TARGET NUM - ---------- ---------- F 0 152 F 1 13 M 0 411 M 1 26We want to find out if sex and target are associated. Since both are categorical variables, we perform Chi-square test using a SQL query.
select stats_crosstab(sex, target, 'CHISQ_OBS') chi_squared, stats_crosstab(sex, target, 'CHISQ_SIG') p_value from tbl; CHI_SQUARED P_VALUE ----------- ---------- .735719671 .391035481Based on the p value of the Chi-square statistics, we can not reject the NULL hypothesis that sex and target are independent given the significant level of 0.05. As we see, it is very convenient to perform statistical tests such as Chi-square, within a database using SQL query.It is not necessary to move the data to another statistical software to perform those tests.



