Numpy - Python for Data Science & Machine Learning Bootcamp


Python for Data Science & Machine Learning Bootcamp
notes by Lucas, Sep 15, 2018


Numpy

1. Introduction

  • The core of Numpy library is Linear Algebra. So Numpy is very fast, and many scientific libraries are based on it;
  • Importing Numpy: import numpy as np;

2. Numpy Arrays

  • The dimension of nd.array can be seen from the numbers of [...] in array(); e.g. array([...]) indicates 1 dim, array([[...]...]) indicates 2 dim, etc.;
  • Note the difference between np.arange() (NOT np.range) and np.linspace();
  • np.eye() creates an Identity Matrix;
  • np.random.rand() generates the random numbers from 0 to 1;
  • np.random.randn() generates the random numbers which obey the normal distribution (mean = 0);
  • np.random.randint() generates ONE random number in [start, end), add a third paramter if wanna generates a certain mount of random numbers;
  • arr.reshape() creates a specific shape of n dimentional array;
  • Note the difference between arr.max() and arr.argmax(), for the real value and for the position (index);
  • arr.shape checks the dim of an array;
  • arr.dtype checks the data type in an array;
  • For np.random.randint(), you can just input from numpy.random import randint, and then just input randint();

3. Numpy Array Indexing & Selection

  • Slice: arr[0:5] selects the group of elements whose index are from 0 to 4;
  • Broadcast: arr[0:3] = 520 makes the first three elements become a bunch of numbers of 520;
  • Copy: arr.copy() makes a copy of the array;
  • arr_2d[0][0] equals arr_2d[0,0], but the later notation is recommended;
  • Conditional selection: arr[arr>5];
  • Combination of functions: arr_2d = np.arrange(0:50).reshape(5,10);

4. Numpy Operations

  • arr + arr means every elements of arr has been doubled, and so were the subtraction and multiplication;
  • The operation of a numpy array with a scalar number is easy to understand;
  • Universal functions: np.sqrt(), np.exp(), np.max(arr) equals arr.max(), np.sin(), np.log();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,458评论 0 10
  • numpy.random.randint Return random integers fromlow(inclu...
    onepedalo阅读 1,209评论 0 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,516评论 0 13
  • 【书籍思想记录】: 《IDEO,设计改变一切》的第二章,描述了设计师的主要挑战:帮助人们明确表达那些甚至连他们自己...
    独一无二的狐狸阅读 573评论 1 4
  • 我之前想每天写文章,坚持了一个月,可我就是坚持不下来,后来我就加入了007,一周写一篇,一直在写,但我感觉从写作到...
    匀速小跑阅读 108评论 1 2