Skip to content

Instantly share code, notes, and snippets.

@drmingle
Last active May 2, 2018 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmingle/0641a39297f41198781319dc11308fd1 to your computer and use it in GitHub Desktop.
Save drmingle/0641a39297f41198781319dc11308fd1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"---\n",
"title: \"Make Simulated Data For Regression\"\n",
"author: \"Damian Mingle\"\n",
"date: 05/01/2018\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preliminaries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn.datasets import make_regression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create Simulated Data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Generate fetures, outputs, and true coefficient of 1000 samples,\n",
"features, output, coef = make_regression(n_samples = 1000,\n",
" # four features\n",
" n_features = 4,\n",
" # where only two features are useful,\n",
" n_informative = 3,\n",
" # a single target value per observation\n",
" n_targets = 1,\n",
" # 0.10 standard deviation of the guassian noise\n",
" noise = 0.10,\n",
" # show the true coefficient used to generated the data\n",
" coef = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## View Simulated Data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Western Region</th>\n",
" <th>Northern Region</th>\n",
" <th>Southern Region</th>\n",
" <th>Eastern Region</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-1.198623</td>\n",
" <td>1.579461</td>\n",
" <td>-1.030068</td>\n",
" <td>-0.244612</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-0.065129</td>\n",
" <td>-0.343513</td>\n",
" <td>0.735071</td>\n",
" <td>-0.301276</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-1.344454</td>\n",
" <td>0.680595</td>\n",
" <td>-0.996430</td>\n",
" <td>-1.998609</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.028145</td>\n",
" <td>-0.570871</td>\n",
" <td>1.325140</td>\n",
" <td>0.721115</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-0.042687</td>\n",
" <td>0.324216</td>\n",
" <td>-1.989905</td>\n",
" <td>1.302356</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Western Region Northern Region Southern Region Eastern Region\n",
"0 -1.198623 1.579461 -1.030068 -0.244612\n",
"1 -0.065129 -0.343513 0.735071 -0.301276\n",
"2 -1.344454 0.680595 -0.996430 -1.998609\n",
"3 1.028145 -0.570871 1.325140 0.721115\n",
"4 -0.042687 0.324216 -1.989905 1.302356"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# View the features of the first five rows\n",
"pd.DataFrame(features, columns=['Western Region', 'Northern Region', 'Southern Region', 'Eastern Region']).head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Books Sold</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>99.279766</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-37.248474</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-20.634296</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>-7.606406</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>57.091988</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Books Sold\n",
"0 99.279766\n",
"1 -37.248474\n",
"2 -20.634296\n",
"3 -7.606406\n",
"4 57.091988"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# View the output of the first five rows\n",
"pd.DataFrame(output, columns=['Books Sold']).head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>True Coefficient Values</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>22.119634</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>83.369898</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>23.830838</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" True Coefficient Values\n",
"0 22.119634\n",
"1 83.369898\n",
"2 0.000000\n",
"3 23.830838"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# View the actual, true coefficients used to generate the data\n",
"pd.DataFrame(coef, columns=['True Coefficient Values'])"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment