Skip to content

Instantly share code, notes, and snippets.

@ananya868
Created July 27, 2021 08:16
Show Gist options
  • Save ananya868/492c3fbac71fabe1a9b3c06683c4e04f to your computer and use it in GitHub Desktop.
Save ananya868/492c3fbac71fabe1a9b3c06683c4e04f to your computer and use it in GitHub Desktop.
Numpy 2
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "05eba4df",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2d677a70",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[3 2 1 6 8]\n",
" [6 7 8 3 4]]\n"
]
}
],
"source": [
"array1 = np.array([[3,2,1,6,8],[6,7,8,3,4]], dtype = \"int32\")\n",
"print(array1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4904ee84",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 5)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1.shape"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5a5722dc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int32')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1.dtype"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c7d5ac8b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros((4,6))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f8e1c974",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones(6).dtype"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "96e56fff",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty((4,6))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ba5f6dc4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 2, 1, 6, 8],\n",
" [6, 7, 8, 3, 4]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "733d932c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 9, 4, 1, 36, 64],\n",
" [36, 49, 64, 9, 16]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1*array1"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e19967c4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 27, 4, 1, 46656, 16777216],\n",
" [ 46656, 823543, 16777216, 27, 256]], dtype=int32)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1**array1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c0fbd540",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.33333333, 0.5 , 1. , 0.16666667, 0.125 ],\n",
" [0.16666667, 0.14285714, 0.125 , 0.33333333, 0.25 ]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1/array1"
]
},
{
"cell_type": "markdown",
"id": "8e75c356",
"metadata": {},
"source": [
"# Slicing"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "a0940e09",
"metadata": {},
"outputs": [],
"source": [
"array2 = np.array([2,4,6,8,6,12])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "9af5a5aa",
"metadata": {},
"outputs": [],
"source": [
"arr = array2[4:6].copy()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "67e76ced",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, 12])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "95f22157",
"metadata": {},
"outputs": [],
"source": [
"arr[0] = 9"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "71a47492",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 4, 6, 8, 6, 12])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "419d6d7f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 9, 12])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment