Files
FYS-STK4155/doc/Programs/JupyterFiles/Examples/My Own Examples/Functions in mglearn.ipynb
T
Morten Hjorth-Jensen 1c714ef427 small changes
2018-09-11 11:06:31 +02:00

108 lines
3.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import mglearn\n",
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"from scipy import signal\n",
"from sklearn.datasets import load_boston\n",
"from sklearn.preprocessing import MinMaxScaler, PolynomialFeatures\n",
"from mglearn.make_blobs import make_blobs\n",
"\n",
"#DATA_PATH = os.path.join(os.path.dirname(__file__), \"data\")\n",
"\n",
"\n",
"def make_forge():\n",
" # a carefully hand-designed dataset lol\n",
" X, y = make_blobs(centers=2, random_state=4, n_samples=30)\n",
" y[np.array([7, 27])] = 0\n",
" mask = np.ones(len(X), dtype=np.bool)\n",
" mask[np.array([0, 1, 5, 26])] = 0\n",
" X, y = X[mask], y[mask]\n",
" return X, y\n",
"\n",
"\n",
"def make_wave(n_samples=100):\n",
" rnd = np.random.RandomState(42)\n",
" x = rnd.uniform(-3, 3, size=n_samples)\n",
" y_no_noise = (np.sin(4 * x) + x)\n",
" y = (y_no_noise + rnd.normal(size=len(x))) / 2\n",
" return x.reshape(-1, 1), y\n",
"\n",
"\n",
"def load_extended_boston():\n",
" boston = load_boston()\n",
" X = boston.data\n",
"\n",
" X = MinMaxScaler().fit_transform(boston.data)\n",
" X = PolynomialFeatures(degree=2, include_bias=False).fit_transform(X)\n",
" return X, boston.target\n",
"\n",
"\n",
"def load_citibike():\n",
" data_mine = pd.read_csv(os.path.join(DATA_PATH, \"citibike.csv\"))\n",
" data_mine['one'] = 1\n",
" data_mine['starttime'] = pd.to_datetime(data_mine.starttime)\n",
" data_starttime = data_mine.set_index(\"starttime\")\n",
" data_resampled = data_starttime.resample(\"3h\").sum().fillna(0)\n",
" return data_resampled.one\n",
"\n",
"\n",
"def make_signals():\n",
" # fix a random state seed\n",
" rng = np.random.RandomState(42)\n",
" n_samples = 2000\n",
" time = np.linspace(0, 8, n_samples)\n",
" # create three signals\n",
" s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal\n",
" s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal\n",
" s3 = signal.sawtooth(2 * np.pi * time) # Signal 3: saw tooth signal\n",
"\n",
" # concatenate the signals, add noise\n",
" S = np.c_[s1, s2, s3]\n",
" S += 0.2 * rng.normal(size=S.shape)\n",
"\n",
" S /= S.std(axis=0) # Standardize data\n",
" S -= S.min()\n",
" return S\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}