examples/demos/yelp_demo/notebooks/simple_ml_model.ipynb

113 lines
2.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple Machine Learning Model\n",
"\n",
"Credit: [Raman Sah](https://towardsdatascience.com/simple-machine-learning-model-in-python-in-5-lines-of-code-fe03d72e78c6)\n",
"\n",
"1. Generate data\n",
"1. Train a model\n",
"1. Make a prediction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from random import randint\n",
"from sklearn.linear_model import LinearRegression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"TRAIN_SET_LIMIT = 1000\n",
"TRAIN_SET_COUNT = 100\n",
"\n",
"TRAIN_INPUT = list()\n",
"TRAIN_OUTPUT = list()\n",
"for i in range(TRAIN_SET_COUNT):\n",
" a = randint(0, TRAIN_SET_LIMIT)\n",
" b = randint(0, TRAIN_SET_LIMIT)\n",
" c = randint(0, TRAIN_SET_LIMIT)\n",
" op = a + (2*b) + (3*c)\n",
" TRAIN_INPUT.append([a, b, c])\n",
" TRAIN_OUTPUT.append(op)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Train a model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictor = LinearRegression(n_jobs=-1)\n",
"predictor.fit(X=TRAIN_INPUT, y=TRAIN_OUTPUT)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make a prediction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_TEST = [[10, 20, 30]]\n",
"outcome = predictor.predict(X=X_TEST)\n",
"coefficients = predictor.coef_\n",
"\n",
"print('Outcome : {}\\nCoefficients : {}'.format(outcome, coefficients))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}