{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numpy Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ndarray\n", "Arrays are important because they enable you to express batch operations on data without writing any for loops. This is usually called **vectorization**." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 4, 5, 6, 7])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# creating an array\n", "data1 = [2, 3, 4, 5, 6, 7]\n", "np.array(data1)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 4],\n", " [5, 6, 7, 8]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = ([1, 2, 3, 4], [5, 6, 7, 8])\n", "arr2 = np.array(data2)\n", "arr2" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 4)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr2.shape" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(10)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((2, 3))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(15)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4, 6],\n", " [3, 5, 9]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.array([[2, 4, 6], \n", " [3, 5, 9]])\n", "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 12, 18],\n", " [ 9, 15, 27]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data + data + data" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 4, 8, 12],\n", " [ 6, 10, 18]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data * 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | 0 | \n", "1 | \n", "2 | \n", "
---|---|---|---|
0 | \n", "2 | \n", "4 | \n", "6 | \n", "
1 | \n", "3 | \n", "5 | \n", "9 | \n", "