Skip to content

Commit 9a319a9

Browse files
committed
take linear algebra to its own file
1 parent 75469f6 commit 9a319a9

3 files changed

Lines changed: 153 additions & 102 deletions

File tree

census.ipynb renamed to Calgary Census.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
"name": "python",
371371
"nbconvert_exporter": "python",
372372
"pygments_lexer": "ipython3",
373-
"version": "3.7.0"
373+
"version": "3.6.8"
374374
}
375375
},
376376
"nbformat": 4,

Linear Algebra.ipynb

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Linear Algebra In Supervised Learning: \n",
8+
"\n",
9+
"Linear Algebra is the basis of building models in machine learning. \n",
10+
"A very simple example: \n",
11+
"\n",
12+
"Predict House Price from its size:\n",
13+
"\n",
14+
"Create a math function called **Hypothesis** to predict the price of the house.\n",
15+
"\n",
16+
"\\begin{align}\n",
17+
"\\Large\n",
18+
"y^i = h_\\theta(x^i) & = \\theta_0 + \\theta_1 x^i \\\\\n",
19+
"\\end{align}\n",
20+
"\n",
21+
"\n",
22+
"Linear algebra is using as the basis of any math in any machine learning algorithm. \n",
23+
"\n",
24+
"The above formula for one input variable. \n",
25+
"But what if we have multi input variables? \n",
26+
"\n",
27+
"\\begin{align}\n",
28+
"\\Large\n",
29+
"y^i = h_\\theta(x_1^i, x_2^i, ...) & = \\theta_0 + \\theta_1 x_1^i + \\theta_2 x_2^i + ... \\\\\n",
30+
"\\end{align}\n"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"## Practical Example: Use Matrix calculation in Supervised Learning\n",
38+
"\n",
39+
"\n",
40+
"![Matrix calc](img/LA-26.png)\n"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"### Iris dataset in Scikit-learn module:\n",
48+
"scikit-learn comes with a few standard datasets, and iris is one of them. \n",
49+
"It is formatted in a way to be used by machine learning. "
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 1,
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"type of iris: <class 'sklearn.utils.Bunch'>\n",
62+
"dict keys: dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])\n",
63+
"type of iris data: <class 'numpy.ndarray'>\n",
64+
"iris column names: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n",
65+
"the target: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
66+
" 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
67+
" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2\n",
68+
" 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n",
69+
" 2 2]\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"from sklearn import datasets\n",
75+
"iris_sk = datasets.load_iris()\n",
76+
"print ('type of iris: ', type(iris_sk))\n",
77+
"print ('dict keys: ' , iris_sk.keys())\n",
78+
"print ('type of iris data: ', type(iris_sk.data))\n",
79+
"print ('iris column names: ' , iris_sk.feature_names)\n",
80+
"print ('the target: ', iris_sk.target)"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 2,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"import numpy as np\n",
90+
"\n",
91+
"X = np.asmatrix(iris_sk.data)\n",
92+
"\n",
93+
"x = np.transpose(X)\n",
94+
"\n",
95+
"T = x.dot(X)\n",
96+
"\n",
97+
"inv = np.linalg.inv(T)\n",
98+
"\n",
99+
"theta = (inv.dot(X.T)).dot(iris_sk.target)"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 3,
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"data": {
109+
"text/plain": [
110+
"matrix([[-0.0844926 , -0.02356211, 0.22487123, 0.59972247]])"
111+
]
112+
},
113+
"execution_count": 3,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
118+
"source": [
119+
"theta"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {},
126+
"outputs": [],
127+
"source": []
128+
}
129+
],
130+
"metadata": {
131+
"kernelspec": {
132+
"display_name": "Python 3",
133+
"language": "python",
134+
"name": "python3"
135+
},
136+
"language_info": {
137+
"codemirror_mode": {
138+
"name": "ipython",
139+
"version": 3
140+
},
141+
"file_extension": ".py",
142+
"mimetype": "text/x-python",
143+
"name": "python",
144+
"nbconvert_exporter": "python",
145+
"pygments_lexer": "ipython3",
146+
"version": "3.6.8"
147+
}
148+
},
149+
"nbformat": 4,
150+
"nbformat_minor": 2
151+
}
Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -63,106 +63,6 @@
6363
"# resampled=data.resample('A')\n",
6464
"# resampled.plot()"
6565
]
66-
},
67-
{
68-
"cell_type": "code",
69-
"execution_count": 2,
70-
"metadata": {},
71-
"outputs": [],
72-
"source": [
73-
"from datetime import datetime as dt\n",
74-
"import mpl_finance"
75-
]
76-
},
77-
{
78-
"cell_type": "code",
79-
"execution_count": 8,
80-
"metadata": {},
81-
"outputs": [
82-
{
83-
"ename": "AttributeError",
84-
"evalue": "module 'mpl_finance' has no attribute 'quotes_historical_yahoo_ochl'",
85-
"output_type": "error",
86-
"traceback": [
87-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
88-
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
89-
"\u001b[0;32m<ipython-input-8-bfdf1a64e1da>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0msymbol\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"EWN\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m quotes = mpl_finance.quotes_historical_yahoo_ochl(symbol, start,\n\u001b[0m\u001b[1;32m 6\u001b[0m end, asobject=True)\n",
90-
"\u001b[0;31mAttributeError\u001b[0m: module 'mpl_finance' has no attribute 'quotes_historical_yahoo_ochl'"
91-
]
92-
}
93-
],
94-
"source": [
95-
"start = dt(1996, 3, 22) \n",
96-
"end = dt(2013, 5, 4)\n",
97-
"\n",
98-
"symbol = \"EWN\"\n",
99-
"quotes = mpl_finance.quotes_historical_yahoo_ochl(symbol, start,\n",
100-
"end, asobject=True)\n"
101-
]
102-
},
103-
{
104-
"cell_type": "code",
105-
"execution_count": 7,
106-
"metadata": {},
107-
"outputs": [
108-
{
109-
"data": {
110-
"text/plain": [
111-
"['Affine2D',\n",
112-
" 'Line2D',\n",
113-
" 'LineCollection',\n",
114-
" 'PolyCollection',\n",
115-
" 'Rectangle',\n",
116-
" 'TICKLEFT',\n",
117-
" 'TICKRIGHT',\n",
118-
" '__builtins__',\n",
119-
" '__cached__',\n",
120-
" '__doc__',\n",
121-
" '__file__',\n",
122-
" '__loader__',\n",
123-
" '__name__',\n",
124-
" '__package__',\n",
125-
" '__spec__',\n",
126-
" '_candlestick',\n",
127-
" '_check_input',\n",
128-
" '_plot_day_summary',\n",
129-
" 'absolute_import',\n",
130-
" 'candlestick2_ochl',\n",
131-
" 'candlestick2_ohlc',\n",
132-
" 'candlestick_ochl',\n",
133-
" 'candlestick_ohlc',\n",
134-
" 'division',\n",
135-
" 'index_bar',\n",
136-
" 'mcolors',\n",
137-
" 'np',\n",
138-
" 'plot_day_summary2_ochl',\n",
139-
" 'plot_day_summary2_ohlc',\n",
140-
" 'plot_day_summary_oclh',\n",
141-
" 'plot_day_summary_ohlc',\n",
142-
" 'print_function',\n",
143-
" 'unicode_literals',\n",
144-
" 'volume_overlay',\n",
145-
" 'volume_overlay2',\n",
146-
" 'volume_overlay3',\n",
147-
" 'xrange',\n",
148-
" 'zip']"
149-
]
150-
},
151-
"execution_count": 7,
152-
"metadata": {},
153-
"output_type": "execute_result"
154-
}
155-
],
156-
"source": [
157-
"dir (mpl_finance)"
158-
]
159-
},
160-
{
161-
"cell_type": "code",
162-
"execution_count": null,
163-
"metadata": {},
164-
"outputs": [],
165-
"source": []
16666
}
16767
],
16868
"metadata": {
@@ -181,7 +81,7 @@
18181
"name": "python",
18282
"nbconvert_exporter": "python",
18383
"pygments_lexer": "ipython3",
184-
"version": "3.7.0"
84+
"version": "3.6.8"
18585
}
18686
},
18787
"nbformat": 4,

0 commit comments

Comments
 (0)