Skip to content

Commit

Permalink
ds
Browse files Browse the repository at this point in the history
  • Loading branch information
llshao committed Nov 23, 2019
1 parent 4efe7fc commit 209c20c
Show file tree
Hide file tree
Showing 12 changed files with 2,289 additions and 12 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .ipynb_checkpoints/Google_DS_Simulations-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
275 changes: 275 additions & 0 deletions .ipynb_checkpoints/Numpy_Examples-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## matmul v.s dot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From the documentation:\n",
"\n",
"1matmul differs from dot in two important ways.\n",
"\n",
"Multiplication by scalars is not allowed.\n",
"Stacks of matrices are broadcast together as if the matrices were elements.\n",
"The last point makes it clear that dot and matmul methods behave differently when passed 3D (or higher dimensional) arrays. Quoting from the documentation some more:\n",
"\n",
"2For matmul:\n",
"\n",
"If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the **last two indexes** and broadcast accordingly.\n",
"\n",
"3For np.dot:\n",
"\n",
"For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the **last axis of a and the second-to-last of b**"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"c shape:(8, 15, 16) d shape: (8, 15, 8, 16)\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"a = np.random.rand(8,15,13)\n",
"b = np.random.rand(8,13,16)\n",
"c = a @ b # Python 3.5+\n",
"d = np.dot(a, b)\n",
"print('c shape:{} d shape: {}'.format(c.shape,d.shape))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a.ravel():\n",
"(i) Return only reference/view of original array\n",
"(ii) If you modify the array you would notice that the value of original array also changes.\n",
"(iii) Ravel is faster than flatten() as it does not occupy any memory.\n",
"(iv) Ravel is a library-level function.\n",
"\n",
"a.flatten() :\n",
"(i) Return copy of original array\n",
"(ii) If you modify any value of this array value of original array is not affected.\n",
"(iii) Flatten() is comparatively slower than ravel() as it occupies memory.\n",
"(iv) Flatten is a method of an ndarray object.\n",
"\n",
"Let us check out the difference by this cod"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(12).reshape((3,4))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 4, 8],\n",
" [ 1, 5, 9],\n",
" [ 2, 6, 10],\n",
" [ 3, 7, 11]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.flatten()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ravel()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.simulation coding,9个normal rv, mean和sigma都不同,问这9个数的 max expectation是多少,期望的uncertainty是多少"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"n = 9\n",
"sample_size =1000\n",
"mean = 10*np.random.random(size=n)\n",
"var = 100*np.random.random(size=n)\n",
"sample_mean = []\n",
"sample_var = []\n",
"samples = []\n",
"for m, v in zip(mean, var):\n",
" sample = np.random.normal(loc=m, scale=v,size=sample_size)\n",
" if len(samples) == 0:\n",
" samples = sample\n",
" else:\n",
" samples = np.vstack((sample,samples))\n",
"sample_max = np.max(samples,axis=0)\n",
"sample_mean = np.mean(sample_max)\n",
"sample_var = np.var(sample_max)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"list comprehension\n",
"Python's conditional expression \n",
"\n",
"1.[a if C else b]\n",
"2.[ expression for item in list if conditional ]"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[27, 18, 46, 51, 99, 70, 48, 49, 6]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [22, 13, 45, 50, 98, 69, 43, 44, 1]\n",
"[x+1 if x >= 45 else x+5 for x in l]"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 6 additions & 0 deletions .ipynb_checkpoints/Pandas_Examples-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 209c20c

Please sign in to comment.