{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## an array looks like a Python list that has different shapes\n",
"- That is why you see the square brackets\n",
"- Array is just a group of items of the same type"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## [Importing into array /exporting array](#Importing-into-array/-exporting-array)\n",
"\n",
"- np.loadtxt('./data/data1.txt')\n",
"- np.savetxt('./data/ar1.txt',ar1,delimiter=' ')\n",
"***\n",
"\n",
"\n",
"## [Convert from pandas](#convert-from-pandas)\n",
"- df.values\n",
"\n",
"
\n",
"Tip: np.arange is similar to np.linspace with following 3 differences:\n",
"1. end point is not included; 2. you specify the interval; 3.np.linspace returns array of floats whereas np,.arange returns integers\n",
"
\n",
"\n",
"> ### [np.random](#np-random)\n",
"- np.random.rand(n,m) retarts array with floats between 0-1\n",
"- np.random.rand(n,m)*k retarts array with floats between 0-k\n",
"- np.random.randint(start, end, size=(n,m)) If we do not specify size =, it will return 1 random integer between start and end; np.random.randint(0,10,size=(2,3))\n",
"- np.random.randn(n,m) returns array with floats from standard normal distribution with mean 0 and standard deviation of 1\n",
"- np.random.randn(n,m) * sigma + mu, returns array with floats shifted and scaled from the standard normal distribution\n",
"\n",
"***\n",
"\n",
"> ### [Inspecting array](#inspect)\n",
"- ar.size | number of elements in ar\n",
"- ar.shape | dimensions of ar (rows,columns)\n",
"- ar.ndim | number of dimensions\n",
"- ar.dtype | type of elements in ar\n",
"- ar.astype(dtype) | Convert arr elements to type dtype\n",
"***\n",
"\n",
"\n",
"## [Copying/sorting/reshaping](#copy)\n",
"- np.copy(ar) \t| copies to *new* memory, i.e. deep copy. \n",
" In comparison,ar2 = ar1 is a shallow copy because changing ar1 will change ar2, and vice versa! Because shallow copy is just giving the original array another name, nothing else. \n",
"- ar.sort() \t| sort\n",
"\n",
"- ar.sort(axis=0) \n",
"\n",
"\n",
"- two_d_ar.flatten() \t| Flattens 2D aray to 1D\n",
"\n",
"\n",
"- ar.T \t| Transposes ar (rows become columns and vice versa)\n",
"\n",
"\n",
"- ar.reshape(3,4) \t| Reshapes ar to 3 rows, 4 columns without changing data\n",
"\n",
"\n",
"- ar.resize((5,6)) \t| Changes ar shape to 5x6 and fills new values with 0\n",
"\n",
"- ar.view(dtype) \t| Creates view of ar elements with type dtype\n",
"
\n",
"Tip: b = array.copy() is a deep copy (i.e. new memory)\n",
"This is very different from python list. Copied python list is not a deep copy unless you make it a deep copy using import copy.\n",
"
\n",
"\n",
"***\n",
"\n",
"> ## [Combining/splitting](#Combining-splitting)\n",
"- np.concatenate((ar1,ar2),axis=0) | Adds ar2 as rows to the end of ar1\n",
"- np.concatenate((ar1,ar2),axis=1) | Adds ar2 as columns to end of ar1\n",
"- np.split(ar,3) | Splits ar into 3 sub-arays\n",
"- np.hsplit(ar,5) | Splits ar horizontally on the 5th index\n",
"\n",
"***\n",
"\n",
"> ## [Adding/removing Elements](#add-remove)\n",
"- np.append(arr,values) | Appends values to end of arr\n",
"- np.insert(arr,2,values) | Inserts values into arr before index 2\n",
"- np.delete(arr,3,axis=0) | Deletes row on index 3 of arr\n",
"- np.delete(arr,4,axis=1) | Deletes column on index 4 of arr\n",
"\n",
"***\n",
"\n",
"> ## [Indexing/slicing/subsetting](#indexing-slicing-subsetting)\n",
"- ar[5] | the element at index 5\n",
"- ar[2,5] | element on index [2][5]\n",
"- ar[1]=4 | Assigns aray element on index 1 the value 4\n",
"- ar[0:3] | Returns the elements at indices 0,1,2 (On a 2D aray: returns rows 0,1,2)\n",
"- ar[0:3,4] | Returns the elements on rows 0,1,2 at column 4\n",
"- ar[:2] | Returns the elements at indices 0,1 (On a 2D aray: returns rows 0,1)\n",
"- ar[:,1] | Returns the elements at index 1 on all rows\n",
"- ar\\\\<5 | Returns an aray with boolean values\n",
"- (ar1\\\\<3) & (ar2\\\\>5) | Returns an aray with boolean values\n",
"- ~ar | Inverts a boolean aray\n",
"\n",
"- ar[ar<5] | boolean slicing\n",
"\n",
"\n",
"## [More examples](#more-exaples)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*** \n",
"\n",
"## Importing into array /exporting array\n",
"\n",
"#### txt file (data separated with space)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 2., 3.],\n",
" [4., 5., 6.]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#data1.txt just have 6 numbers, separated with spaces, and a line breaker\n",
"ar1 = np.loadtxt('./data/data1.txt')\n",
"ar1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#save txt file\n",
"np.savetxt('./data/ar1.txt',ar1,delimiter=' ')"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 2., 3.],\n",
" [4., 5., 6.]])"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#re-import it to verfiy it is working\n",
"ar1 = np.loadtxt('./data/ar1.txt')\n",
"ar1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Convert from pandas\n",
"- df.values\n",
"\n",
"## Convert to list\n",
"- arr.tolist() | Convert arr to a Python list"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"Tip: If you don't specify axis, then the result from `np.append` will be flattened.\n",
"\n",
"If you specify axis, then you must provide exactly the same shape of array(s)\n",
"