forked from arvindsh/YelpDatasetSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_ReviewTextClassification.sql
More file actions
75 lines (61 loc) · 3.41 KB
/
Copy path4_ReviewTextClassification.sql
File metadata and controls
75 lines (61 loc) · 3.41 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
This sample code shows how to import and work with the Yelp Dataset (https://www.yelp.com/dataset_challenge) using Microsoft SQL Server 2017.
Please first review the terms of use for the dataset on the Yelp website (https://www.yelp.com/html/pdf/Dataset_Challenge_Academic_Dataset_Agreement.pdf).
*/
-- Machine learning examples on top of the Yelp Reviews dataset
USE YelpReviews;
GO
-- This example shows how to perform multi-class classification based on text data
-- It uses the MicrosoftML functions 'featurizeText' and 'rxLogisticRegression' to
-- build a model with test data and then uses 'rxPredict' to score the training data
-- Training and test are split from the Yelp Reviews table in a 75% - 25% ratio
-- At the end 'rxCrossTabs' is used to print a 'confusion matrix' of the multi-class
-- classification results
DECLARE @R_script AS NVARCHAR (MAX) = CONCAT(N'
library(MicrosoftML)
sqlServerConnString <- "Server=', REPLACE(@@SERVERNAME, '\', '\\'), N';Database=YelpReviews;trusted_connection=true;"
ReviewDataRaw <- RxSqlServerData(sqlQuery = "
SELECT TOP 10000 R.review_text, BC.category
FROM Review R JOIN BusinessCategory BC
ON R.business_id = BC.business_id
WHERE BC.category IN (''American'', ''Mexican'', ''French'', ''Italian'', ''Chinese'', ''Japanese'', ''Indian'', ''Malay'')
", connectionString = sqlServerConnString, rowBuffering = FALSE, stringsAsFactors = FALSE
,
);
ReviewsDataSet <- rxImport(ReviewDataRaw, maxRowsByCols = 1000000)
ReviewsDataSet$category <- as.factor(ReviewsDataSet$category)
boundary <- floor((nrow(ReviewsDataSet) / 4)*3)
train_complete_data <- ReviewsDataSet[1:boundary, ]
test_complete_data <- ReviewsDataSet[(boundary+1):nrow(ReviewsDataSet),]
multiFormula = category ~ review_text
mlTransList = list (featurizeText(vars = ''review_text'',
stopwordsRemover = stopwordsDefault(),
vectorNormalizer = ''none'',
wordFeatureExtractor = ngramCount(),
keepPunctuations = FALSE))
modelLR = rxLogisticRegression(
formula = multiFormula,
type = "multiClass",
mlTransforms = mlTransList,
normalize = "no",
data = train_complete_data
)
scored_data = rxPredict(modelLR, data = test_complete_data, extraVarsToWrite = "category")
print(rxCrossTabs(~ category:PredictedLabel, scored_data, verbose= 0))
');
EXECUTE sp_execute_external_script @language = N'R', @script = @R_script;
/*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This sample code is not supported under any Microsoft standard support program or service.
The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.
In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts
be liable for any damages whatsoever (including, without limitation, damages for loss of business profits,
business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability
to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
*/