Skip to content

Commit c6f2a82

Browse files
committed
adv query
1 parent d54c7a9 commit c6f2a82

File tree

5 files changed

+577
-0
lines changed

5 files changed

+577
-0
lines changed
92.4 KB
Binary file not shown.
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"gather": {
8+
"logged": 1661782492602
9+
}
10+
},
11+
"outputs": [
12+
{
13+
"name": "stdout",
14+
"output_type": "stream",
15+
"text": [
16+
"<class 'pandas.core.frame.DataFrame'>\n",
17+
"RangeIndex: 442 entries, 0 to 441\n",
18+
"Data columns (total 10 columns):\n",
19+
" # Column Non-Null Count Dtype \n",
20+
"--- ------ -------------- ----- \n",
21+
" 0 AGE 442 non-null int64 \n",
22+
" 1 SEX 442 non-null int64 \n",
23+
" 2 BMI 442 non-null float64\n",
24+
" 3 BP 442 non-null float64\n",
25+
" 4 S1 442 non-null int64 \n",
26+
" 5 S2 442 non-null float64\n",
27+
" 6 S3 442 non-null float64\n",
28+
" 7 S4 442 non-null float64\n",
29+
" 8 S5 442 non-null float64\n",
30+
" 9 S6 442 non-null int64 \n",
31+
"dtypes: float64(6), int64(4)\n",
32+
"memory usage: 34.7 KB\n"
33+
]
34+
}
35+
],
36+
"source": [
37+
"from azureml.opendatasets import Diabetes\n",
38+
"\n",
39+
"diabetes = Diabetes.get_tabular_dataset()\n",
40+
"X = diabetes.drop_columns(\"Y\")\n",
41+
"y = diabetes.keep_columns(\"Y\")\n",
42+
"X_df = X.to_pandas_dataframe()\n",
43+
"y_df = y.to_pandas_dataframe()\n",
44+
"X_df.info()"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 2,
50+
"metadata": {
51+
"gather": {
52+
"logged": 1661782499120
53+
},
54+
"jupyter": {
55+
"outputs_hidden": false,
56+
"source_hidden": false
57+
},
58+
"nteract": {
59+
"transient": {
60+
"deleting": false
61+
}
62+
}
63+
},
64+
"outputs": [
65+
{
66+
"data": {
67+
"text/plain": [
68+
"['sklearn_regression_model.pkl']"
69+
]
70+
},
71+
"execution_count": 2,
72+
"metadata": {},
73+
"output_type": "execute_result"
74+
}
75+
],
76+
"source": [
77+
"import joblib\n",
78+
"from sklearn.linear_model import Ridge\n",
79+
"\n",
80+
"model = Ridge().fit(X_df,y_df)\n",
81+
"joblib.dump(model, 'sklearn_regression_model.pkl')"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 3,
87+
"metadata": {
88+
"gather": {
89+
"logged": 1661782505031
90+
},
91+
"jupyter": {
92+
"outputs_hidden": false,
93+
"source_hidden": false
94+
},
95+
"nteract": {
96+
"transient": {
97+
"deleting": false
98+
}
99+
}
100+
},
101+
"outputs": [
102+
{
103+
"name": "stdout",
104+
"output_type": "stream",
105+
"text": [
106+
"Registering model my-sklearn-model\n",
107+
"Name: my-sklearn-model\n",
108+
"Version: 1\n"
109+
]
110+
}
111+
],
112+
"source": [
113+
"import sklearn\n",
114+
"\n",
115+
"from azureml.core import Workspace\n",
116+
"from azureml.core import Model\n",
117+
"from azureml.core.resource_configuration import ResourceConfiguration\n",
118+
"\n",
119+
"ws = Workspace.from_config()\n",
120+
"\n",
121+
"model = Model.register(workspace=ws,\n",
122+
" model_name='my-sklearn-model', # Name of the registered model in your workspace.\n",
123+
" model_path='./sklearn_regression_model.pkl', # Local file to upload and register as a model.\n",
124+
" model_framework=Model.Framework.SCIKITLEARN, # Framework used to create the model.\n",
125+
" model_framework_version=sklearn.__version__, # Version of scikit-learn used to create the model.\n",
126+
" sample_input_dataset=X,\n",
127+
" sample_output_dataset=y,\n",
128+
" resource_configuration=ResourceConfiguration(cpu=2, memory_in_gb=4),\n",
129+
" description='Ridge regression model to predict diabetes progression.',\n",
130+
" tags={'area': 'diabetes', 'type': 'regression'})\n",
131+
"\n",
132+
"print('Name:', model.name)\n",
133+
"print('Version:', model.version)"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 4,
139+
"metadata": {
140+
"jupyter": {
141+
"outputs_hidden": false,
142+
"source_hidden": false
143+
},
144+
"nteract": {
145+
"transient": {
146+
"deleting": false
147+
}
148+
}
149+
},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"Writing score.py\n"
156+
]
157+
}
158+
],
159+
"source": [
160+
"%%writefile score.py\n",
161+
"\n",
162+
"import json\n",
163+
"import pickle\n",
164+
"import numpy as np\n",
165+
"import pandas as pd\n",
166+
"import os\n",
167+
"import joblib\n",
168+
"from azureml.core.model import Model\n",
169+
"\n",
170+
"from inference_schema.schema_decorators import input_schema, output_schema\n",
171+
"from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType\n",
172+
"from inference_schema.parameter_types.pandas_parameter_type import PandasParameterType\n",
173+
"\n",
174+
"\n",
175+
"def init():\n",
176+
" global model\n",
177+
" # Replace filename if needed.\n",
178+
" path = os.getenv('AZUREML_MODEL_DIR') \n",
179+
" model_path = os.path.join(path, 'sklearn_regression_model.pkl')\n",
180+
" # Deserialize the model file back into a sklearn model.\n",
181+
" model = joblib.load(model_path)\n",
182+
"\n",
183+
"\n",
184+
"input_sample = pd.DataFrame(data=[{\n",
185+
" \"AGE\": 5,\n",
186+
" \"SEX\": 2,\n",
187+
" \"BMI\": 3.1,\n",
188+
" \"BP\": 3.1,\n",
189+
" \"S1\": 3.1,\n",
190+
" \"S2\": 3.1,\n",
191+
" \"S3\": 3.1,\n",
192+
" \"S4\": 3.1,\n",
193+
" \"S5\": 3.1,\n",
194+
" \"S6\": 3.1\n",
195+
"}])\n",
196+
"\n",
197+
"# This is an integer type sample. Use the data type that reflects the expected result.\n",
198+
"output_sample = np.array([0])\n",
199+
"\n",
200+
"# To indicate that we support a variable length of data input,\n",
201+
"# set enforce_shape=False\n",
202+
"@input_schema('data', PandasParameterType(input_sample))\n",
203+
"@output_schema(NumpyParameterType(output_sample))\n",
204+
"def run(data):\n",
205+
" try:\n",
206+
" print(\"input_data....\")\n",
207+
" print(data.columns)\n",
208+
" print(type(data))\n",
209+
" result = model.predict(data)\n",
210+
" print(\"result.....\")\n",
211+
" print(result)\n",
212+
" # You can return any data type, as long as it can be serialized by JSON.\n",
213+
" return result.tolist()\n",
214+
" except Exception as e:\n",
215+
" error = str(e)\n",
216+
" return error"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 5,
222+
"metadata": {
223+
"gather": {
224+
"logged": 1661782512382
225+
},
226+
"jupyter": {
227+
"outputs_hidden": false,
228+
"source_hidden": false
229+
},
230+
"nteract": {
231+
"transient": {
232+
"deleting": false
233+
}
234+
}
235+
},
236+
"outputs": [],
237+
"source": [
238+
"from azureml.core.model import InferenceConfig\n",
239+
"from azureml.core import Environment\n",
240+
"from azureml.core.conda_dependencies import CondaDependencies\n",
241+
"\n",
242+
"environment = Environment('my-sklearn-environment')\n",
243+
"environment.python.conda_dependencies = CondaDependencies.create(pip_packages=[\n",
244+
" 'azureml-defaults',\n",
245+
" 'inference-schema[numpy-support]',\n",
246+
" 'joblib',\n",
247+
" 'numpy',\n",
248+
" 'pandas',\n",
249+
" 'Jinja2<3.1',\n",
250+
" 'scikit-learn=={}'.format(sklearn.__version__)\n",
251+
"])\n",
252+
"\n",
253+
"inference_config = InferenceConfig(entry_script='./score.py',environment=environment)"
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": 6,
259+
"metadata": {
260+
"jupyter": {
261+
"outputs_hidden": false,
262+
"source_hidden": false
263+
},
264+
"nteract": {
265+
"transient": {
266+
"deleting": false
267+
}
268+
}
269+
},
270+
"outputs": [
271+
{
272+
"name": "stdout",
273+
"output_type": "stream",
274+
"text": [
275+
"Tips: You can try get_logs(): https://aka.ms/debugimage#dockerlog or local deployment: https://aka.ms/debugimage#debug-locally to debug if deployment takes longer than 10 minutes.\n",
276+
"Running\n",
277+
"2022-08-29 14:15:19+00:00 Creating Container Registry if not exists."
278+
]
279+
}
280+
],
281+
"source": [
282+
"service_name = 'my-diabetes-model'\n",
283+
"\n",
284+
"service = Model.deploy(ws, service_name, [model], inference_config, overwrite=True)\n",
285+
"service.wait_for_deployment(show_output=True)"
286+
]
287+
}
288+
],
289+
"metadata": {
290+
"kernel_info": {
291+
"name": "python38-azureml"
292+
},
293+
"kernelspec": {
294+
"display_name": "Python 3.8 - AzureML",
295+
"language": "python",
296+
"name": "python38-azureml"
297+
},
298+
"language_info": {
299+
"codemirror_mode": {
300+
"name": "ipython",
301+
"version": 3
302+
},
303+
"file_extension": ".py",
304+
"mimetype": "text/x-python",
305+
"name": "python",
306+
"nbconvert_exporter": "python",
307+
"pygments_lexer": "ipython3",
308+
"version": "3.8.13"
309+
},
310+
"nteract": {
311+
"version": "[email protected]"
312+
}
313+
},
314+
"nbformat": 4,
315+
"nbformat_minor": 2
316+
}
55.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)