Skip to content

Commit 6be3205

Browse files
committed
minor changes
1 parent 7a7cbea commit 6be3205

8 files changed

+2048
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:24d12ab64e5e93c82e0acf22e043d94d7006b033acc4160a5a99e702b6873929"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": []
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:d274a9480a3c582c98c551412b6b8f6d82e57bc95757b304c6c65df44b6d6e71"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Finding A Trend Line Of Data\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/).\n",
18+
"\n",
19+
"Based on: SciPy And NumPy"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"### Import modules"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"collapsed": false,
32+
"input": [
33+
"import numpy as np\n",
34+
"from scipy.optimize import curve_fit"
35+
],
36+
"language": "python",
37+
"metadata": {},
38+
"outputs": [],
39+
"prompt_number": 3
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"### Creating some clean data"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"collapsed": false,
51+
"input": [
52+
"# create a function called func(),\n",
53+
"def func(x, a, b):\n",
54+
" # that multiplies a and x, then adds b\n",
55+
" return a * x + b"
56+
],
57+
"language": "python",
58+
"metadata": {},
59+
"outputs": [],
60+
"prompt_number": 7
61+
},
62+
{
63+
"cell_type": "code",
64+
"collapsed": false,
65+
"input": [
66+
"# create 100 elements, evenly spaced between 0 and 10\n",
67+
"x = np.linspace(0, 10, 100)\n",
68+
"\n",
69+
"# mix up the data by applying func()\n",
70+
"y = func(x, 1, 2)\n",
71+
"\n",
72+
"# view the first few entries\n",
73+
"y[0:10]"
74+
],
75+
"language": "python",
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"metadata": {},
80+
"output_type": "pyout",
81+
"prompt_number": 18,
82+
"text": [
83+
"array([ 2. , 2.1010101 , 2.2020202 , 2.3030303 , 2.4040404 ,\n",
84+
" 2.50505051, 2.60606061, 2.70707071, 2.80808081, 2.90909091])"
85+
]
86+
}
87+
],
88+
"prompt_number": 18
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"### Add noise to the data"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"collapsed": false,
100+
"input": [
101+
"# Add some randomness to the data by multiplying y by 100 values drawn from the normal distribution\n",
102+
"yn = y + 0.9 * np.random.normal(size=len(x))\n",
103+
"\n",
104+
"# view the first few entries\n",
105+
"yn[0:10]"
106+
],
107+
"language": "python",
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"metadata": {},
112+
"output_type": "pyout",
113+
"prompt_number": 20,
114+
"text": [
115+
"array([ 3.63606696, 3.51496281, 2.68882029, 2.11995938, 2.36446115,\n",
116+
" 3.41454019, 1.66117548, 2.59706936, 3.22359943, 2.16499171])"
117+
]
118+
}
119+
],
120+
"prompt_number": 20
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"### Fitting a line to the data\n",
127+
"\n",
128+
"- func = the model function\n",
129+
"- x = the independent variable\n",
130+
"- y = the dependent variable"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"collapsed": false,
136+
"input": [
137+
"# return an array of values (popt) so that the squared error of the line is minimized\n",
138+
"# also return a 2d array with eh covariance of popt\n",
139+
"popt, pcov = curve_fit(func, x, yn)"
140+
],
141+
"language": "python",
142+
"metadata": {},
143+
"outputs": [],
144+
"prompt_number": 25
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"### What is popt?\n",
151+
"\n",
152+
"popt returns the values of x (the independent variable) and yn (the dependent variable) with the smallest squared error"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"collapsed": false,
158+
"input": [
159+
"popt"
160+
],
161+
"language": "python",
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"metadata": {},
166+
"output_type": "pyout",
167+
"prompt_number": 26,
168+
"text": [
169+
"array([ 0.92825153, 2.25863607])"
170+
]
171+
}
172+
],
173+
"prompt_number": 26
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"This means that if x = 0.92 and y = 2.25, the function (called func()) best fits the data"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"collapsed": false,
185+
"input": [],
186+
"language": "python",
187+
"metadata": {},
188+
"outputs": []
189+
}
190+
],
191+
"metadata": {}
192+
}
193+
]
194+
}

0 commit comments

Comments
 (0)