Skip to content

Instantly share code, notes, and snippets.

@kantale
Created December 20, 2018 20:29
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 kantale/b95eca23aed4d9f9e0569914787833cf to your computer and use it in GitHub Desktop.
Save kantale/b95eca23aed4d9f9e0569914787833cf to your computer and use it in GitHub Desktop.
Πρόχειρες σημειώσεις από το μάθημα python, 10η διάλεξη, 20 Δεκεμβρίου 2018
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"$$\\int_{a}^{b}4$$\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import math as mitsos"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mitsos.sqrt(4)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Zen of Python, by Tim Peters\n",
"\n",
"Beautiful is better than ugly.\n",
"Explicit is better than implicit.\n",
"Simple is better than complex.\n",
"Complex is better than complicated.\n",
"Flat is better than nested.\n",
"Sparse is better than dense.\n",
"Readability counts.\n",
"Special cases aren't special enough to break the rules.\n",
"Although practicality beats purity.\n",
"Errors should never pass silently.\n",
"Unless explicitly silenced.\n",
"In the face of ambiguity, refuse the temptation to guess.\n",
"There should be one-- and preferably only one --obvious way to do it.\n",
"Although that way may not be obvious at first unless you're Dutch.\n",
"Now is better than never.\n",
"Although never is often better than *right* now.\n",
"If the implementation is hard to explain, it's a bad idea.\n",
"If the implementation is easy to explain, it may be a good idea.\n",
"Namespaces are one honking great idea -- let's do more of those!\n"
]
}
],
"source": [
"import this"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"a = [[1,2,3], [5,6,7]]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [5, 6, 7]]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"np_a = np.array(a)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(np_a)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"b = [[13,21,32], [55,6,7]]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [5, 6, 7]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np_a"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"np_b = np.array(b)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [5, 6, 7], [13, 21, 32], [55, 6, 7]]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a+b"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[14, 23, 35],\n",
" [60, 12, 14]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np_a + np_b"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [5, 6, 7]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np_a"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"[5 6 7]\n"
]
}
],
"source": [
"for x in np_a:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"[5, 6, 7]\n"
]
}
],
"source": [
"for x in a:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[[1 for y in range(10)] for x in range(10)]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((10,10))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((10,10), dtype=np.int)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((10,10), dtype=np.int) *2"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros((10,10), dtype=np.int)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.99988524, 0.86510183],\n",
" [0.06499213, 0.29126733],\n",
" [0.85561405, 0.54603335],\n",
" [0.96925109, 0.74056216],\n",
" [0.39407004, 0.15073097]])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.random((5,2))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"a = 4*np.random.random((10,5))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.0967211 , 3.01001326, 2.70078129, 1.90309973, 1.83821701],\n",
" [2.26258732, 1.91533238, 2.02872005, 1.52636384, 0.41162573],\n",
" [3.12783647, 1.63592953, 3.68203005, 1.68617224, 0.99090702],\n",
" [3.71891316, 0.69166238, 2.0872808 , 0.80809971, 3.71192219],\n",
" [0.22972117, 0.2942025 , 3.87960336, 0.02544306, 1.40753207],\n",
" [0.67993253, 3.96544174, 2.40521556, 1.75039836, 3.32693361],\n",
" [3.39718944, 3.30351026, 3.70871928, 0.97561574, 3.4301418 ],\n",
" [1.79564523, 0.05018239, 2.57251535, 3.51666764, 1.60540851],\n",
" [0.97521794, 3.99638388, 0.88086471, 0.743369 , 3.06635545],\n",
" [3.54381635, 3.20311735, 2.52148344, 3.67116687, 0.77571447]])"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.97521794, 3.99638388, 0.88086471, 0.743369 , 3.06635545],\n",
" [3.54381635, 3.20311735, 2.52148344, 3.67116687, 0.77571447]])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[-2:]"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.97521794, 3.99638388],\n",
" [3.54381635, 3.20311735]])"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[-2:, :2]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.0967211 , 2.26258732, 3.12783647, 3.71891316, 0.22972117,\n",
" 0.67993253, 3.39718944, 1.79564523, 0.97521794, 3.54381635])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[:,0]"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.91533238, 1.52636384],\n",
" [0.69166238, 0.80809971]])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[1:5:2, 1:5:2]"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.0967211 , 3.01001326, 2.70078129, 1.90309973, 1.83821701],\n",
" [2.26258732, 1.91533238, 2.02872005, 1.52636384, 0.41162573],\n",
" [3.12783647, 1.63592953, 3.68203005, 1.68617224, 0.99090702],\n",
" [3.71891316, 0.69166238, 2.0872808 , 0.80809971, 3.71192219],\n",
" [0.22972117, 0.2942025 , 3.87960336, 0.02544306, 1.40753207],\n",
" [0.67993253, 3.96544174, 2.40521556, 1.75039836, 3.32693361],\n",
" [3.39718944, 3.30351026, 3.70871928, 0.97561574, 3.4301418 ],\n",
" [1.79564523, 0.05018239, 2.57251535, 3.51666764, 1.60540851],\n",
" [0.97521794, 3.99638388, 0.88086471, 0.743369 , 3.06635545],\n",
" [3.54381635, 3.20311735, 2.52148344, 3.67116687, 0.77571447]])"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"a[0] = np.ones((1,5))"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "could not broadcast input array from shape (4) into shape (5)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-76-7716874988e9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mones\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: could not broadcast input array from shape (4) into shape (5)"
]
}
],
"source": [
"a[0] = np.ones((1,4))"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
"a[:2] = np.ones((2,5)) * 3"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3. , 3. , 3. , 3. , 3. ],\n",
" [3. , 3. , 3. , 3. , 3. ],\n",
" [3.12783647, 1.63592953, 3.68203005, 1.68617224, 0.99090702],\n",
" [3.71891316, 0.69166238, 2.0872808 , 0.80809971, 3.71192219],\n",
" [0.22972117, 0.2942025 , 3.87960336, 0.02544306, 1.40753207],\n",
" [0.67993253, 3.96544174, 2.40521556, 1.75039836, 3.32693361],\n",
" [3.39718944, 3.30351026, 3.70871928, 0.97561574, 3.4301418 ],\n",
" [1.79564523, 0.05018239, 2.57251535, 3.51666764, 1.60540851],\n",
" [0.97521794, 3.99638388, 0.88086471, 0.743369 , 3.06635545],\n",
" [3.54381635, 3.20311735, 2.52148344, 3.67116687, 0.77571447]])"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"a[:2] = np.ones((2,5), dtype=np.int) * 3"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.random((10,5))"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.49525177, 0.12257774, 0.54864786, 0.6325568 , 0.41421987],\n",
" [0.53596923, 0.8237734 , 0.15291302, 0.69768488, 0.22642805],\n",
" [0.46436095, 0.31840045, 0.16125843, 0.43207541, 0.52975538],\n",
" [0.4967983 , 0.31202898, 0.37738432, 0.11998686, 0.28020596],\n",
" [0.18063122, 0.89663146, 0.26310926, 0.38120443, 0.9937035 ],\n",
" [0.22409072, 0.27065114, 0.73922427, 0.53787195, 0.49375886],\n",
" [0.67059772, 0.96958925, 0.56539066, 0.0330741 , 0.03352285],\n",
" [0.30169753, 0.17239052, 0.67278735, 0.44900698, 0.66377398],\n",
" [0.08847538, 0.31850397, 0.36667714, 0.1037844 , 0.80992179],\n",
" [0.38198723, 0.07446757, 0.11637552, 0.38224956, 0.41157758]])"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.03307409713016263"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.min()"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.08847538, 0.07446757, 0.11637552, 0.0330741 , 0.03352285])"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.min(axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.12257774, 0.15291302, 0.16125843, 0.11998686, 0.18063122,\n",
" 0.22409072, 0.0330741 , 0.17239052, 0.08847538, 0.07446757])"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.min(axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([8, 9, 9, 6, 6])"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.argmin(axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5*random.random()"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.randint(1,5)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"def random_float(f,t):\n",
" return f+(t-f)*random.random()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.077093447646056"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random_float(4,4.1)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(10, 5)"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ndim"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.size"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.49525177, 0.53596923, 0.46436095, 0.4967983 , 0.18063122],\n",
" [0.22409072, 0.67059772, 0.30169753, 0.08847538, 0.38198723],\n",
" [0.12257774, 0.8237734 , 0.31840045, 0.31202898, 0.89663146],\n",
" [0.27065114, 0.96958925, 0.17239052, 0.31850397, 0.07446757],\n",
" [0.54864786, 0.15291302, 0.16125843, 0.37738432, 0.26310926],\n",
" [0.73922427, 0.56539066, 0.67278735, 0.36667714, 0.11637552],\n",
" [0.6325568 , 0.69768488, 0.43207541, 0.11998686, 0.38120443],\n",
" [0.53787195, 0.0330741 , 0.44900698, 0.1037844 , 0.38224956],\n",
" [0.41421987, 0.22642805, 0.52975538, 0.28020596, 0.9937035 ],\n",
" [0.49375886, 0.03352285, 0.66377398, 0.80992179, 0.41157758]])"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty((10,5))"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 3, 5, 7, 9])"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(1,10,2)"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. ,\n",
" 7.5, 8. , 8.5, 9. , 9.5])"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(1,10,0.5)"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1. , 3.25, 5.5 , 7.75, 10. ])"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(1,10, 5)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,\n",
" 3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,\n",
" 5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,\n",
" 8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(1,10,20)"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 2., 3., 4., 5.],\n",
" [ 6., 7., 8., 9., 10.]])"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(1,10,10).reshape((2,5))"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 2.],\n",
" [ 3., 4.],\n",
" [ 5., 6.],\n",
" [ 7., 8.],\n",
" [ 9., 10.]])"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(1,10,10).reshape((5,2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.linspace(1,10,10).reshape((5,2))"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [],
"source": [
"np.random.shuffle(a)"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 2, 1])"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.random((10,5))"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.66920122, 0.58930177, 0.27832244, 0.53593852, 0.35050451],\n",
" [0.75388272, 0.59260823, 0.69032962, 0.05952921, 0.22969259],\n",
" [0.60455621, 0.77495512, 0.89765653, 0.37345476, 0.85426931],\n",
" [0.69866754, 0.36097093, 0.10039905, 0.76880236, 0.69420014],\n",
" [0.2639787 , 0.82733112, 0.6011262 , 0.66171516, 0.20866982],\n",
" [0.16509007, 0.48083715, 0.58526657, 0.44555704, 0.80086467],\n",
" [0.82512004, 0.60872361, 0.33619742, 0.68197358, 0.4288447 ],\n",
" [0.94642836, 0.46150813, 0.2722412 , 0.21850036, 0.71796905],\n",
" [0.73898851, 0.82486489, 0.34006345, 0.75396871, 0.9760928 ],\n",
" [0.47007051, 0.44502802, 0.08714258, 0.97400608, 0.95015674]])"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.66920122, 0.58930177, 0.27832244, 0.53593852, 0.35050451,\n",
" 0.75388272, 0.59260823, 0.69032962, 0.05952921, 0.22969259,\n",
" 0.60455621, 0.77495512, 0.89765653, 0.37345476, 0.85426931,\n",
" 0.69866754, 0.36097093, 0.10039905, 0.76880236, 0.69420014,\n",
" 0.2639787 , 0.82733112, 0.6011262 , 0.66171516, 0.20866982,\n",
" 0.16509007, 0.48083715, 0.58526657, 0.44555704, 0.80086467,\n",
" 0.82512004, 0.60872361, 0.33619742, 0.68197358, 0.4288447 ,\n",
" 0.94642836, 0.46150813, 0.2722412 , 0.21850036, 0.71796905,\n",
" 0.73898851, 0.82486489, 0.34006345, 0.75396871, 0.9760928 ,\n",
" 0.47007051, 0.44502802, 0.08714258, 0.97400608, 0.95015674])"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape((1,50))[0]"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.66920122, 0.58930177, 0.27832244, 0.53593852, 0.35050451,\n",
" 0.75388272, 0.59260823, 0.69032962, 0.05952921, 0.22969259,\n",
" 0.60455621, 0.77495512, 0.89765653, 0.37345476, 0.85426931,\n",
" 0.69866754, 0.36097093, 0.10039905, 0.76880236, 0.69420014,\n",
" 0.2639787 , 0.82733112, 0.6011262 , 0.66171516, 0.20866982,\n",
" 0.16509007, 0.48083715, 0.58526657, 0.44555704, 0.80086467,\n",
" 0.82512004, 0.60872361, 0.33619742, 0.68197358, 0.4288447 ,\n",
" 0.94642836, 0.46150813, 0.2722412 , 0.21850036, 0.71796905,\n",
" 0.73898851, 0.82486489, 0.34006345, 0.75396871, 0.9760928 ,\n",
" 0.47007051, 0.44502802, 0.08714258, 0.97400608, 0.95015674])"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.flatten()"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.66920122, 0.58930177, 0.27832244, 0.53593852, 0.35050451,\n",
" 0.75388272, 0.59260823, 0.69032962, 0.05952921, 0.22969259,\n",
" 0.60455621, 0.77495512, 0.89765653, 0.37345476, 0.85426931,\n",
" 0.69866754, 0.36097093, 0.10039905, 0.76880236, 0.69420014,\n",
" 0.2639787 , 0.82733112, 0.6011262 , 0.66171516, 0.20866982,\n",
" 0.16509007, 0.48083715, 0.58526657, 0.44555704, 0.80086467,\n",
" 0.82512004, 0.60872361, 0.33619742, 0.68197358, 0.4288447 ,\n",
" 0.94642836, 0.46150813, 0.2722412 , 0.21850036, 0.71796905,\n",
" 0.73898851, 0.82486489, 0.34006345, 0.75396871, 0.9760928 ,\n",
" 0.47007051, 0.44502802, 0.08714258, 0.97400608, 0.95015674])"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ravel()"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.66920122, 0.58930177, 0.27832244, 0.53593852, 0.35050451],\n",
" [0.75388272, 0.59260823, 0.69032962, 0.05952921, 0.22969259],\n",
" [0.60455621, 0.77495512, 0.89765653, 0.37345476, 0.85426931],\n",
" [0.69866754, 0.36097093, 0.10039905, 0.76880236, 0.69420014],\n",
" [0.2639787 , 0.82733112, 0.6011262 , 0.66171516, 0.20866982],\n",
" [0.16509007, 0.48083715, 0.58526657, 0.44555704, 0.80086467],\n",
" [0.82512004, 0.60872361, 0.33619742, 0.68197358, 0.4288447 ],\n",
" [0.94642836, 0.46150813, 0.2722412 , 0.21850036, 0.71796905],\n",
" [0.73898851, 0.82486489, 0.34006345, 0.75396871, 0.9760928 ],\n",
" [0.47007051, 0.44502802, 0.08714258, 0.97400608, 0.95015674]])"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0.66920122, 0.58930177, 0.27832244, 0.53593852, 0.35050451],\n",
" [0.75388272, 0.59260823, 0.69032962, 0.05952921, 0.22969259]],\n",
"\n",
" [[0.60455621, 0.77495512, 0.89765653, 0.37345476, 0.85426931],\n",
" [0.69866754, 0.36097093, 0.10039905, 0.76880236, 0.69420014]],\n",
"\n",
" [[0.2639787 , 0.82733112, 0.6011262 , 0.66171516, 0.20866982],\n",
" [0.16509007, 0.48083715, 0.58526657, 0.44555704, 0.80086467]],\n",
"\n",
" [[0.82512004, 0.60872361, 0.33619742, 0.68197358, 0.4288447 ],\n",
" [0.94642836, 0.46150813, 0.2722412 , 0.21850036, 0.71796905]],\n",
"\n",
" [[0.73898851, 0.82486489, 0.34006345, 0.75396871, 0.9760928 ],\n",
" [0.47007051, 0.44502802, 0.08714258, 0.97400608, 0.95015674]]])"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape((5,2,5))"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
"a = 'kfjhlzkjdfhawlkghdkfhdslkfjghaslkfdghalskghsldkh'"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['kfjh',\n",
" 'lzkj',\n",
" 'dfha',\n",
" 'wlkg',\n",
" 'hdkf',\n",
" 'hdsl',\n",
" 'kfjg',\n",
" 'hasl',\n",
" 'kfdg',\n",
" 'hals',\n",
" 'kghs',\n",
" 'ldkh']"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[a[x:x+4] for x in range(0, len(a), 4)] # GOOD"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['kfjh',\n",
" 'lzkj',\n",
" 'dfha',\n",
" 'wlkg',\n",
" 'hdkf',\n",
" 'hdsl',\n",
" 'kfjg',\n",
" 'hasl',\n",
" 'kfdg',\n",
" 'hals',\n",
" 'kghs',\n",
" 'ldkh']"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[''.join(x) for x in zip(*[iter(a)]*4)] # !#$%@#$%^%$#%"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 's', 'd', 'f', 'a', 's', 'd', 'f']"
]
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list('asdfasdf')"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"g = iter('zdfgsdfgggsfg')"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'z'"
]
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'d'"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"f\n",
"g\n",
"s\n",
"d\n",
"f\n",
"g\n",
"g\n",
"g\n",
"s\n",
"f\n",
"g\n"
]
}
],
"source": [
"for x in g:\n",
" print (x)"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<zip at 0x7f75624595c8>"
]
},
"execution_count": 176,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zip(*[iter('abcd')]*2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"zip(iter1, iter2)"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"def f(a,b,c,d):\n",
" print (a+b+c+d)\n",
"\n",
"a = [1,2,3,4]\n",
"#f(a)\n",
"f(*a)"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[7, 2, 3], [7, 2, 3]]"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [1,2,3]\n",
"b = [a,a]\n",
"a[0]=7\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.15094485, 0.21621569, 0.24263828, 0.45191009, 0.63085068],\n",
" [0.23141515, 0.8452577 , 0.49028589, 0.15417115, 0.19185365],\n",
" [0.47131205, 0.97898047, 0.63771924, 0.07084381, 0.67522044],\n",
" [0.29879452, 0.21305817, 0.74877963, 0.40348537, 0.54431462]])"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.random.random((4,5))\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True, True, False],\n",
" [ True, False, True, True, True],\n",
" [ True, False, False, True, False],\n",
" [ True, True, False, True, False]])"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a<0.5"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.15094485, 0.21621569, 0.24263828, 0.45191009, 0.23141515,\n",
" 0.49028589, 0.15417115, 0.19185365, 0.47131205, 0.07084381,\n",
" 0.29879452, 0.21305817, 0.40348537])"
]
},
"execution_count": 180,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[a<0.5]"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [],
"source": [
"a[a<0.5] = 9"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[9. , 9. , 9. , 9. , 0.63085068],\n",
" [9. , 0.8452577 , 9. , 9. , 9. ],\n",
" [9. , 0.97898047, 0.63771924, 9. , 0.67522044],\n",
" [9. , 9. , 0.74877963, 9. , 0.54431462]])"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[9. , 9. , 9. , 9. , 0.63085068],\n",
" [9. , 0.8452577 , 9. , 9. , 9. ],\n",
" [9. , 0.97898047, 0.63771924, 9. , 0.67522044],\n",
" [9. , 9. , 0.74877963, 9. , 0.54431462]])"
]
},
"execution_count": 183,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.41211849, 0.41211849, 0.41211849, 0.41211849, 0.58983192],\n",
" [0.41211849, 0.74814213, 0.41211849, 0.41211849, 0.41211849],\n",
" [0.41211849, 0.82992904, 0.5953645 , 0.41211849, 0.6250694 ],\n",
" [0.41211849, 0.41211849, 0.68074532, 0.41211849, 0.51783188]])"
]
},
"execution_count": 184,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sin(a)"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[9. , 9. , 9. , 9. , 0.63085068],\n",
" [9. , 0.8452577 , 9. , 9. , 9. ],\n",
" [9. , 0.97898047, 0.63771924, 9. , 0.67522044],\n",
" [9. , 9. , 0.74877963, 9. , 0.54431462]])"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([9. , 0.8452577 , 0.63771924, 9. ])"
]
},
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.diag(a)"
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[9. , 9. , 9. , 9. , 0.63085068],\n",
" [9. , 0.8452577 , 9. , 9. , 9. ],\n",
" [9. , 0.97898047, 0.63771924, 9. , 0.67522044],\n",
" [9. , 9. , 0.74877963, 9. , 0.54431462]])"
]
},
"execution_count": 189,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.random((4,5))"
]
},
{
"cell_type": "code",
"execution_count": 191,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201, 0.97666903],\n",
" [0.85241108, 0.93890872, 0.36732208, 0.82139344, 0.13423189],\n",
" [0.43633982, 0.62573101, 0.23564769, 0.01438327, 0.13708558],\n",
" [0.441 , 0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 191,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201, 0.97666903,\n",
" 0.85241108, 0.93890872, 0.36732208, 0.82139344, 0.13423189],\n",
" [0.43633982, 0.62573101, 0.23564769, 0.01438327, 0.13708558,\n",
" 0.441 , 0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 193,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape((2,-1))"
]
},
{
"cell_type": "code",
"execution_count": 194,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 ],\n",
" [0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108],\n",
" [0.93890872, 0.36732208],\n",
" [0.82139344, 0.13423189],\n",
" [0.43633982, 0.62573101],\n",
" [0.23564769, 0.01438327],\n",
" [0.13708558, 0.441 ],\n",
" [0.82873119, 0.22185718],\n",
" [0.21602023, 0.23926975]])"
]
},
"execution_count": 194,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape((-1,2))"
]
},
{
"cell_type": "code",
"execution_count": 196,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201, 0.97666903],\n",
" [0.85241108, 0.93890872, 0.36732208, 0.82139344, 0.13423189],\n",
" [0.43633982, 0.62573101, 0.23564769, 0.01438327, 0.13708558],\n",
" [0.441 , 0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 196,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {},
"outputs": [],
"source": [
"a.resize((5,4))"
]
},
{
"cell_type": "code",
"execution_count": 200,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.82139344, 0.13423189, 0.43633982, 0.62573101],\n",
" [0.23564769, 0.01438327, 0.13708558, 0.441 ],\n",
" [0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 200,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.82139344, 0.13423189, 0.43633982, 0.62573101],\n",
" [0.23564769, 0.01438327, 0.13708558, 0.441 ],\n",
" [0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 203,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4823026128565033"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(a)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.7163616 , 0.28513066, 0.50688922, 0.42082897])"
]
},
"execution_count": 204,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(a, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.53976257, 0.78382773, 0.50442404, 0.20702913, 0.37646959])"
]
},
"execution_count": 205,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(a, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.43358091844105584"
]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.median(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.43358091844105584"
]
},
"execution_count": 209,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.percentile(a, 50)"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.82139344, 0.13423189, 0.43633982, 0.62573101],\n",
" [0.23564769, 0.01438327, 0.13708558, 0.441 ],\n",
" [0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 215,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.71936662, 0.2027699 , 0.80609175, 0.43082201]])"
]
},
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[[0,0]]"
]
},
{
"cell_type": "code",
"execution_count": 217,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 217,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[[0,0,1,1,-1]]"
]
},
{
"cell_type": "code",
"execution_count": 219,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.71936662, 0.43082201],\n",
" [0.97666903, 0.97666903, 0.36732208],\n",
" [0.82139344, 0.82139344, 0.62573101],\n",
" [0.23564769, 0.23564769, 0.441 ],\n",
" [0.82873119, 0.82873119, 0.23926975]])"
]
},
"execution_count": 219,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[:,[0,0,-1] ]"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.43082201, 0.43082201, 0.36732208])"
]
},
"execution_count": 220,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[[0,0,1], [-1,-1,-1]]"
]
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.82139344, 0.13423189, 0.43633982, 0.62573101],\n",
" [0.23564769, 0.01438327, 0.13708558, 0.441 ],\n",
" [0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 221,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.71936662, 0.2027699 , 0.80609175, 0.43082201],\n",
" [0.97666903, 0.85241108, 0.93890872, 0.36732208],\n",
" [0.82139344, 0.13423189, 0.43633982, 0.62573101],\n",
" [0.23564769, 0.01438327, 0.13708558, 0.441 ],\n",
" [0.82873119, 0.22185718, 0.21602023, 0.23926975]])"
]
},
"execution_count": 222,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [],
"source": [
"b = a.flat"
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.719366623449258"
]
},
"execution_count": 225,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(b)"
]
},
{
"cell_type": "code",
"execution_count": 226,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.20276990219298874"
]
},
"execution_count": 226,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(b)"
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {},
"outputs": [],
"source": [
"c=iter(a)"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.71936662, 0.2027699 , 0.80609175, 0.43082201])"
]
},
"execution_count": 228,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(c)"
]
},
{
"cell_type": "code",
"execution_count": 229,
"metadata": {},
"outputs": [],
"source": [
"c = iter(['aa', 'bb'])"
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'aa'"
]
},
"execution_count": 230,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(c)"
]
},
{
"cell_type": "code",
"execution_count": 231,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'bb'"
]
},
"execution_count": 231,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(c)"
]
},
{
"cell_type": "code",
"execution_count": 232,
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-232-e846efec376d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(c)"
]
},
{
"cell_type": "code",
"execution_count": 233,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.71936662, 0.2027699 , 0.80609175, 0.43082201, 0.97666903,\n",
" 0.85241108, 0.93890872, 0.36732208, 0.82139344, 0.13423189,\n",
" 0.43633982, 0.62573101, 0.23564769, 0.01438327, 0.13708558,\n",
" 0.441 , 0.82873119, 0.22185718, 0.21602023, 0.23926975])"
]
},
"execution_count": 233,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.flatten()"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 234,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(20,)"
]
},
"execution_count": 234,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.flatten().shape"
]
},
{
"cell_type": "code",
"execution_count": 235,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 20)"
]
},
"execution_count": 235,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape((1,20)).shape"
]
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 236,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.flatten().ndim"
]
},
{
"cell_type": "code",
"execution_count": 238,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 238,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape((1,20)).ndim"
]
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 237,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(a.flatten().shape)"
]
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [],
"source": [
"b = a.flat"
]
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.976669033875903"
]
},
"execution_count": 245,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(b)"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.852411083738822\n",
"0.9389087177533151\n",
"0.3673220756605342\n",
"0.8213934438624219\n",
"0.13423189144270298\n",
"0.43633982229341384\n",
"0.6257310078634168\n",
"0.23564769452635748\n",
"0.014383268429825624\n",
"0.13708557844282443\n",
"0.4409999981444901\n",
"0.8287311882905316\n",
"0.22185717734844634\n",
"0.21602023407012816\n",
"0.23926974845819282\n"
]
}
],
"source": [
"for x in b:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {},
"outputs": [],
"source": [
"b=(x for x in [4,5,6])"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 248,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(b)"
]
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6]"
]
},
"execution_count": 249,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(b)"
]
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([[1,2,3], [4,5,6]])\n",
"b=np.array([[11,22,44], [44,55,66]])"
]
},
{
"cell_type": "code",
"execution_count": 251,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [ 4, 5, 6],\n",
" [11, 22, 44],\n",
" [44, 55, 66]])"
]
},
"execution_count": 251,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.vstack((a,b))"
]
},
{
"cell_type": "code",
"execution_count": 252,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 11, 22, 44],\n",
" [ 4, 5, 6, 44, 55, 66]])"
]
},
"execution_count": 252,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.hstack((a,b))"
]
},
{
"cell_type": "code",
"execution_count": 255,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [11, 22, 44],\n",
" [ 4, 5, 6],\n",
" [44, 55, 66]])"
]
},
"execution_count": 255,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(list(zip(a,b))).reshape((4,3))"
]
},
{
"cell_type": "code",
"execution_count": 256,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 11, 22, 44],\n",
" [ 4, 5, 6, 44, 55, 66]])"
]
},
"execution_count": 256,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.block([a,b])"
]
},
{
"cell_type": "code",
"execution_count": 257,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [ 4, 5, 6],\n",
" [11, 22, 44],\n",
" [44, 55, 66]])"
]
},
"execution_count": 257,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.block([[a], [b]])"
]
},
{
"cell_type": "code",
"execution_count": 274,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 11, 22, 44],\n",
" [ 4, 5, 6, 44, 55, 66],\n",
" [11, 22, 44, 1, 2, 3],\n",
" [44, 55, 66, 4, 5, 6]])"
]
},
"execution_count": 274,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = np.block([[a,b], [b,a]])\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[array([[ 1, 2, 3],\n",
" [ 4, 5, 6],\n",
" [11, 22, 44],\n",
" [44, 55, 66]]), array([[11, 22, 44],\n",
" [44, 55, 66],\n",
" [ 1, 2, 3],\n",
" [ 4, 5, 6]])]"
]
},
"execution_count": 269,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.hsplit(c, 2) # HATE"
]
},
{
"cell_type": "code",
"execution_count": 272,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 1, 2, 3],\n",
" [ 4, 5, 6],\n",
" [11, 22, 44],\n",
" [44, 55, 66]],\n",
"\n",
" [[11, 22, 44],\n",
" [44, 55, 66],\n",
" [ 1, 2, 3],\n",
" [ 4, 5, 6]]])"
]
},
"execution_count": 272,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(np.hsplit(c, 2))"
]
},
{
"cell_type": "code",
"execution_count": 276,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 1, 2, 3, 11, 22, 44],\n",
" [ 4, 5, 6, 44, 55, 66]],\n",
"\n",
" [[11, 22, 44, 1, 2, 3],\n",
" [44, 55, 66, 4, 5, 6]]])"
]
},
"execution_count": 276,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(np.vsplit(c, 2))"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.random((10,4))"
]
},
{
"cell_type": "code",
"execution_count": 278,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.54757584, 0.81388927, 0.75674728, 0.84512381],\n",
" [0.31745494, 0.01165131, 0.91995453, 0.42057797],\n",
" [0.41689452, 0.95440997, 0.14272043, 0.01840746],\n",
" [0.55393792, 0.41093877, 0.02058662, 0.9415191 ],\n",
" [0.66557091, 0.43060359, 0.44773495, 0.41561166],\n",
" [0.49907173, 0.30026948, 0.67208129, 0.06195707],\n",
" [0.03455357, 0.40328541, 0.50471072, 0.41111368],\n",
" [0.12572964, 0.85374167, 0.16786075, 0.55240132],\n",
" [0.4733099 , 0.12450461, 0.9524449 , 0.01331248],\n",
" [0.16187874, 0.83928556, 0.49922418, 0.57006802]])"
]
},
"execution_count": 278,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 279,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[False, False, False, False],\n",
" [ True, True, False, True],\n",
" [ True, False, True, True],\n",
" [False, True, True, False],\n",
" [False, True, True, True],\n",
" [ True, True, False, True],\n",
" [ True, True, False, True],\n",
" [ True, False, True, False],\n",
" [ True, True, False, True],\n",
" [ True, False, True, False]])"
]
},
"execution_count": 279,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a<0.5"
]
},
{
"cell_type": "code",
"execution_count": 284,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[False, True, False, True],\n",
" [False, True, True, False],\n",
" [False, True, True, True],\n",
" [False, False, True, True],\n",
" [False, False, False, False],\n",
" [False, False, False, True],\n",
" [ True, False, False, False],\n",
" [ True, True, True, False],\n",
" [False, True, True, True],\n",
" [ True, True, False, False]])"
]
},
"execution_count": 284,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.bitwise_or(a<0.2,a>0.8)"
]
},
{
"cell_type": "code",
"execution_count": 285,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.81388927, 0.84512381, 0.01165131, 0.91995453, 0.95440997,\n",
" 0.14272043, 0.01840746, 0.02058662, 0.9415191 , 0.06195707,\n",
" 0.03455357, 0.12572964, 0.85374167, 0.16786075, 0.12450461,\n",
" 0.9524449 , 0.01331248, 0.16187874, 0.83928556])"
]
},
"execution_count": 285,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[np.bitwise_or(a<0.2,a>0.8)]"
]
},
{
"cell_type": "code",
"execution_count": 292,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.81388927, 0.84512381, 0.01165131, 0.91995453, 0.95440997,\n",
" 0.14272043, 0.01840746, 0.02058662, 0.9415191 , 0.06195707,\n",
" 0.03455357, 0.12572964, 0.85374167, 0.16786075, 0.12450461,\n",
" 0.9524449 , 0.01331248, 0.16187874, 0.83928556])"
]
},
"execution_count": 292,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[np.bitwise_xor(a<0.2,a>0.8)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 286,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 286,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or True"
]
},
{
"cell_type": "code",
"execution_count": 287,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 287,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or False"
]
},
{
"cell_type": "code",
"execution_count": 288,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 288,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False or False"
]
},
{
"cell_type": "code",
"execution_count": 289,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 289,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True ^ True "
]
},
{
"cell_type": "code",
"execution_count": 290,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 290,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True ^ False"
]
},
{
"cell_type": "code",
"execution_count": 291,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 291,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False ^ False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment