{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [Fundamental Python Data Science Libraries: A Cheatsheet (Part 1/4)](https://hackernoon.com/fundamental-python-data-science-libraries-a-cheatsheet-part-1-4-58884e95c2bd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "by [Lauren Glass](https://www.linkedin.com/in/laurenjglass/), [Hackernoon](https://hackernoon.com/), Dec. 20, 2017" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy\n", "\n", "Just as it is written on NumPy’s website, this library is fundamental for scientific computing in Python. It includes powerful manipulation and mathematical functionality at super fast speeds.\n", "\n", "This library is all about the multidimensional array. It is similar in appearance to a list & indexes like a list, but carries a much more powerful set of tools." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create array from a list or tuple\n", "\n", "# 1 dimensional array (you can pick more)\n", "future_array1 = [1,2,3,4,5]\n", "array1 = np.array(future_array1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create array with placeholder content\n", "\n", "# there are other placeholder options, see jupyter notebook below\n", "placeholder_zero = np.zeros((3,4), dtype=np.int) # default np.float\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "placeholder_zero" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create array with a sequence\n", "\n", "# sequence based on steps (start, end, step_value)\n", "sequence_array1 = np.arange(10, 30, 4)\n", "# evenly spaced sequence based on number specified\n", "# (start, end, number_of_elements)\n", "sequence_array2 = np.linspace(10, 30, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "sequence_array1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sequence_array2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Upload data: csv file contain comma separated values\n", "#data = np.genfromtxt(\"your_file_here.csv\", delimiter=\",\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Makes Math Easy\n", "\n", "a1 = np.array([1,2,3,4])\n", "a2 = np.ones((1,4), dtype=np.int)\n", "a3 = np.zeros((1,4), dtype=np.int)\n", "# Addition/Subtraction\n", "A = a1 + a2 - a3\n", "# Multiplication/Division\n", "B = a2 * a3 / a2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "B" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Attributes & Methods\n", "\n", "# Summary Statistics\n", "\n", "array1.mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1.sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Reshape arrays\n", "\n", "A = np.array([1,2,3,4,5,6,7,8,9,100])\n", "B = A.reshape((2,5)) # takes a tuple of dimensions\n", "C = B.T # transpose" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "B" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "C" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# More Math\n", "\n", "D = A.reshape((1,10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application :: cumulative sum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# deposited $100,000 and then started buying and selling\n", "trades = [100000, -10000, 10500, -100000, 175000]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# convert my trades to a numpy array\n", "trades_array = np.array(trades)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trades_array.cumsum()" ] }, { "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }