1
+ using Common ;
2
+ using DatabaseLoaderConsoleApp . DataModels ;
3
+ using Microsoft . ML ;
4
+ using Microsoft . ML . Data ;
5
+ using Microsoft . ML . Transforms ;
6
+ using System ;
7
+ using System . Collections ;
8
+ using System . Data ;
9
+ using System . Data . Common ;
10
+ using System . Data . SqlClient ;
11
+ using System . Diagnostics ;
12
+ using System . Drawing ;
13
+ using System . IO ;
14
+ using System . Linq ;
15
+
16
+ namespace DatabaseLoaderConsoleApp
17
+ {
18
+ public class Program
19
+ {
20
+ public static void Main ( )
21
+ {
22
+ var mlContext = new MLContext ( ) ;
23
+
24
+ // localdb SQL database connection string using a filepath to attach the database file into localdb
25
+ string dbFilePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "SqlLocalDb" , "Criteo-100k-rows.mdf" ) ;
26
+ string connectionString = $ "Data Source = (LocalDB)\\ MSSQLLocalDB;AttachDbFilename={ dbFilePath } ;Database=Criteo-100k-rows;Integrated Security = True";
27
+
28
+ // ConnString Example: localdb SQL database connection string for 'localdb default location' (usually files located at /Users/YourUser/)
29
+ //string connectionString = @"Data Source=(localdb)\MSSQLLocalDb;Initial Catalog=YOUR_DATABASE;Integrated Security=True;Pooling=False";
30
+ //
31
+ // ConnString Example: on-premises SQL Server Database (Integrated security)
32
+ //string connectionString = @"Data Source=YOUR_SERVER;Initial Catalog=YOUR_DATABASE;Integrated Security=True;Pooling=False";
33
+ //
34
+ // ConnString Example: Azure SQL Database connection string
35
+ //string connectionString = @"Server=tcp:yourserver.database.windows.net,1433; Initial Catalog = YOUR_DATABASE; Persist Security Info = False; User ID = YOUR_USER; Password = YOUR_PASSWORD; MultipleActiveResultSets = False; Encrypt = True; TrustServerCertificate = False; Connection Timeout = 60; ConnectRetryCount = 5; ConnectRetryInterval = 10;";
36
+
37
+ string commandText = "SELECT * from URLClicks" ;
38
+
39
+ DatabaseLoader loader = mlContext . Data . CreateDatabaseLoader < UrlClick > ( ) ;
40
+
41
+ DatabaseSource dbSource = new DatabaseSource ( SqlClientFactory . Instance ,
42
+ connectionString ,
43
+ commandText ) ;
44
+
45
+ IDataView dataView = loader . Load ( dbSource ) ;
46
+
47
+ var trainTestData = mlContext . Data . TrainTestSplit ( dataView ) ;
48
+
49
+ //do the transformation in IDataView
50
+ //Transform categorical features into binary
51
+ var CatogoriesTranformer = mlContext . Transforms . Conversion . ConvertType ( nameof ( UrlClick . Label ) , outputKind : Microsoft . ML . Data . DataKind . Boolean ) .
52
+ Append ( mlContext . Transforms . Categorical . OneHotEncoding ( new [ ] {
53
+ new InputOutputColumnPair ( "Cat14Encoded" , "Cat14" ) ,
54
+ new InputOutputColumnPair ( "Cat15Encoded" , "Cat15" ) ,
55
+ new InputOutputColumnPair ( "Cat16Encoded" , "Cat16" ) ,
56
+ new InputOutputColumnPair ( "Cat17Encoded" , "Cat17" ) ,
57
+ new InputOutputColumnPair ( "Cat18Encoded" , "Cat18" ) ,
58
+ new InputOutputColumnPair ( "Cat19Encoded" , "Cat19" ) ,
59
+ new InputOutputColumnPair ( "Cat20Encoded" , "Cat20" ) ,
60
+ new InputOutputColumnPair ( "Cat21Encoded" , "Cat21" ) ,
61
+ new InputOutputColumnPair ( "Cat22Encoded" , "Cat22" ) ,
62
+ new InputOutputColumnPair ( "Cat23Encoded" , "Cat23" ) ,
63
+ new InputOutputColumnPair ( "Cat24Encoded" , "Cat24" ) ,
64
+ new InputOutputColumnPair ( "Cat25Encoded" , "Cat25" ) ,
65
+ new InputOutputColumnPair ( "Cat26Encoded" , "Cat26" ) ,
66
+ new InputOutputColumnPair ( "Cat27Encoded" , "Cat27" ) ,
67
+ new InputOutputColumnPair ( "Cat28Encoded" , "Cat28" ) ,
68
+ new InputOutputColumnPair ( "Cat29Encoded" , "Cat29" ) ,
69
+ new InputOutputColumnPair ( "Cat30Encoded" , "Cat30" ) ,
70
+ new InputOutputColumnPair ( "Cat31Encoded" , "Cat31" ) ,
71
+ new InputOutputColumnPair ( "Cat32Encoded" , "Cat32" ) ,
72
+ new InputOutputColumnPair ( "Cat33Encoded" , "Cat33" ) ,
73
+ new InputOutputColumnPair ( "Cat34Encoded" , "Cat34" ) ,
74
+ new InputOutputColumnPair ( "Cat35Encoded" , "Cat35" ) ,
75
+ new InputOutputColumnPair ( "Cat36Encoded" , "Cat36" ) ,
76
+ new InputOutputColumnPair ( "Cat37Encoded" , "Cat37" ) ,
77
+ new InputOutputColumnPair ( "Cat38Encoded" , "Cat38" ) ,
78
+ new InputOutputColumnPair ( "Cat39Encoded" , "Cat39" )
79
+ } , OneHotEncodingEstimator . OutputKind . Binary ) ) ;
80
+
81
+ var featuresTransformer = CatogoriesTranformer . Append (
82
+ mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat01Featurized" , inputColumnName : nameof ( UrlClick . Feat01 ) ) )
83
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat02Featurized" , inputColumnName : nameof ( UrlClick . Feat02 ) ) )
84
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat03Featurized" , inputColumnName : nameof ( UrlClick . Feat03 ) ) )
85
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat04Featurized" , inputColumnName : nameof ( UrlClick . Feat04 ) ) )
86
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat05Featurized" , inputColumnName : nameof ( UrlClick . Feat05 ) ) )
87
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat06Featurized" , inputColumnName : nameof ( UrlClick . Feat06 ) ) )
88
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat07Featurized" , inputColumnName : nameof ( UrlClick . Feat07 ) ) )
89
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat08Featurized" , inputColumnName : nameof ( UrlClick . Feat08 ) ) )
90
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat09Featurized" , inputColumnName : nameof ( UrlClick . Feat09 ) ) )
91
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat10Featurized" , inputColumnName : nameof ( UrlClick . Feat10 ) ) )
92
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat11Featurized" , inputColumnName : nameof ( UrlClick . Feat11 ) ) )
93
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat12Featurized" , inputColumnName : nameof ( UrlClick . Feat12 ) ) )
94
+ . Append ( mlContext . Transforms . Text . FeaturizeText ( outputColumnName : "Feat13Featurized" , inputColumnName : nameof ( UrlClick . Feat13 ) ) ) ;
95
+
96
+ var finalTransformerPipeLine = featuresTransformer . Append ( mlContext . Transforms . Concatenate ( "Features" ,
97
+ "Feat01Featurized" , "Feat02Featurized" , "Feat03Featurized" , "Feat04Featurized" , "Feat05Featurized" ,
98
+ "Feat06Featurized" , "Feat07Featurized" , "Feat08Featurized" , "Feat09Featurized" , "Feat10Featurized" ,
99
+ "Feat11Featurized" , "Feat12Featurized" , "Feat12Featurized" ,
100
+ "Cat14Encoded" , "Cat15Encoded" , "Cat16Encoded" , "Cat17Encoded" , "Cat18Encoded" , "Cat19Encoded" ,
101
+ "Cat20Encoded" , "Cat21Encoded" , "Cat22Encoded" , "Cat23Encoded" , "Cat24Encoded" , "Cat25Encoded" ,
102
+ "Cat26Encoded" , "Cat27Encoded" , "Cat28Encoded" , "Cat29Encoded" , "Cat30Encoded" , "Cat31Encoded" ,
103
+ "Cat32Encoded" , "Cat33Encoded" , "Cat34Encoded" , "Cat35Encoded" , "Cat36Encoded" , "Cat37Encoded" ,
104
+ "Cat38Encoded" , "Cat39Encoded" ) ) ;
105
+
106
+ // Apply the ML algorithm
107
+ var trainingPipeLine = finalTransformerPipeLine . Append ( mlContext . BinaryClassification . Trainers . FieldAwareFactorizationMachine ( labelColumnName : "Label" , featureColumnName : "Features" ) ) ;
108
+
109
+ Console . WriteLine ( "Training the ML model while streaming data from a SQL database..." ) ;
110
+ Stopwatch watch = new Stopwatch ( ) ;
111
+ watch . Start ( ) ;
112
+
113
+ var model = trainingPipeLine . Fit ( trainTestData . TrainSet ) ;
114
+
115
+ watch . Stop ( ) ;
116
+ Console . WriteLine ( "Elapsed time for training the model = {0} seconds" , watch . ElapsedMilliseconds / 1000 ) ;
117
+
118
+ Console . WriteLine ( "Evaluating the model..." ) ;
119
+ Stopwatch watch2 = new Stopwatch ( ) ;
120
+ watch2 . Start ( ) ;
121
+
122
+ var predictions = model . Transform ( trainTestData . TestSet ) ;
123
+ // Now that we have the test predictions, calculate the metrics of those predictions and output the results.
124
+ var metrics = mlContext . BinaryClassification . Evaluate ( predictions ) ;
125
+
126
+ watch2 . Stop ( ) ;
127
+ Console . WriteLine ( "Elapsed time for evaluating the model = {0} seconds" , watch2 . ElapsedMilliseconds / 1000 ) ;
128
+
129
+ ConsoleHelper . PrintBinaryClassificationMetrics ( "==== Evaluation Metrics training from a Database ====" , metrics ) ;
130
+
131
+ //
132
+ Console . WriteLine ( "Trying a single prediction:" ) ;
133
+
134
+ var predictionEngine = mlContext . Model . CreatePredictionEngine < UrlClick , ClickPrediction > ( model ) ;
135
+
136
+ UrlClick sampleData = new UrlClick ( ) {
137
+ Label = String . Empty ,
138
+ Feat01 = "32" , Feat02 = "3" , Feat03 = "5" , Feat04 = "NULL" , Feat05 = "1" ,
139
+ Feat06 = "0" , Feat07 = "0" , Feat08 = "61" , Feat09 = "5" , Feat10 = "0" ,
140
+ Feat11 = "1" , Feat12 = "3157" , Feat13 = "5" ,
141
+ Cat14 = "e5f3fd8d" , Cat15 = "a0aaffa6" , Cat16 = "aa15d56f" , Cat17 = "da8a3421" ,
142
+ Cat18 = "cd69f233" , Cat19 = "6fcd6dcb" , Cat20 = "ab16ed81" , Cat21 = "43426c29" ,
143
+ Cat22 = "1df5e154" , Cat23 = "00c5ffb7" , Cat24 = "be4ee537" , Cat25 = "f3bbfe99" ,
144
+ Cat26 = "7de9c0a9" , Cat27 = "6652dc64" , Cat28 = "99eb4e27" , Cat29 = "4cdc3efa" ,
145
+ Cat30 = "d20856aa" , Cat31 = "a1eb1511" , Cat32 = "9512c20b" , Cat33 = "febfd863" ,
146
+ Cat34 = "a3323ca1" , Cat35 = "c8e1ee56" , Cat36 = "1752e9e8" , Cat37 = "75350c8a" ,
147
+ Cat38 = "991321ea" , Cat39 = "b757e957"
148
+ } ;
149
+
150
+ var clickPrediction = predictionEngine . Predict ( sampleData ) ;
151
+
152
+ Console . WriteLine ( $ "Predicted Label: { clickPrediction . PredictedLabel } - Score:{ Sigmoid ( clickPrediction . Score ) } ", Color . YellowGreen ) ;
153
+ Console . WriteLine ( ) ;
154
+
155
+ //*** Detach database from localdb only if you used a conn-string with a filepath to attach the database file into localdb ***
156
+ Console . WriteLine ( "... Detaching database from SQL localdb ..." ) ;
157
+ DetachDatabase ( connectionString ) ;
158
+
159
+ Console . WriteLine ( "=============== Press any key ===============" ) ;
160
+ Console . ReadKey ( ) ;
161
+ }
162
+
163
+ public static float Sigmoid ( float x )
164
+ {
165
+ return ( float ) ( 100 / ( 1 + Math . Exp ( - x ) ) ) ;
166
+ }
167
+
168
+ public static void DetachDatabase ( string userConnectionString ) //DELETE PARAM *************
169
+ {
170
+ string dbName = string . Empty ;
171
+ using ( SqlConnection userSqlDatabaseConnection = new SqlConnection ( userConnectionString ) )
172
+ {
173
+ userSqlDatabaseConnection . Open ( ) ;
174
+ dbName = userSqlDatabaseConnection . Database ;
175
+ }
176
+
177
+ string masterConnString = $ "Data Source = (LocalDB)\\ MSSQLLocalDB;Integrated Security = True";
178
+ using ( SqlConnection sqlDatabaseConnection = new SqlConnection ( masterConnString ) )
179
+ {
180
+ sqlDatabaseConnection . Open ( ) ;
181
+
182
+ string prepareDbcommandString = $ "ALTER DATABASE [{ dbName } ] SET OFFLINE WITH ROLLBACK IMMEDIATE ALTER DATABASE [{ dbName } ] SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
183
+ //(ALTERNATIVE) string prepareDbcommandString = $"ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
184
+ SqlCommand sqlPrepareCommand = new SqlCommand ( prepareDbcommandString , sqlDatabaseConnection ) ;
185
+ sqlPrepareCommand . ExecuteNonQuery ( ) ;
186
+
187
+ string detachCommandString = "sp_detach_db" ;
188
+ SqlCommand sqlDetachCommand = new SqlCommand ( detachCommandString , sqlDatabaseConnection ) ;
189
+ sqlDetachCommand . CommandType = CommandType . StoredProcedure ;
190
+ sqlDetachCommand . Parameters . AddWithValue ( "@dbname" , dbName ) ;
191
+ sqlDetachCommand . ExecuteNonQuery ( ) ;
192
+ }
193
+ }
194
+ }
195
+ }
0 commit comments