Skip to content

Instantly share code, notes, and snippets.

@nesscx
Created March 25, 2019 20:21
Show Gist options
  • Save nesscx/bed6545ca2af60462eddcc44cd8b9a64 to your computer and use it in GitHub Desktop.
Save nesscx/bed6545ca2af60462eddcc44cd8b9a64 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> Get to Know a numpy Array </h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"cast the following list to a numpy array:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"a=[1,2,3,4,5]\n",
"\n",
"a = np.array(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) type using the function type "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2) the shape of the array "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3) the type of data in the array "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4) find the mean of the array "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> Creating and Plotting Functions </h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) create the following functions using the numpy array <code> x </code>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$y=sin(x)+2$$"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607,\n",
" 0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,\n",
" 0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,\n",
" 0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,\n",
" 1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,\n",
" 1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,\n",
" 1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,\n",
" 2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,\n",
" 2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,\n",
" 2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,\n",
" 3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,\n",
" 3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458,\n",
" 3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717,\n",
" 4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976,\n",
" 4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235,\n",
" 4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494,\n",
" 5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753,\n",
" 5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012,\n",
" 5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272,\n",
" 6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=np.linspace(0,2*np.pi,100)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2) plot the function"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline \n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<small>Copyright &copy; 2018 IBM Cognitive Class. This notebook and its source code are released under the terms of the [MIT License](https://cognitiveclass.ai/mit-license/).</small>"
]
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment