- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
NDArray
The base structure in numpy
is ndarray
, used to represent vectors, matrices and higher-dimensional arrays. Each ndarray
has the following attributes:
- dtype = correspond to data types in C
- shape = dimensionns of array
- strides = number of bytes to step in each direction when traversing the array
x = np.array([1,2,3,4,5,6]) print x print 'dytpe', x.dtype print 'shape', x.shape print 'strides', x.strides
[1 2 3 4 5 6] dytpe int64 shape (6,) strides (8,)
x.shape = (2,3) print x print 'dytpe', x.dtype print 'shape', x.shape print 'strides', x.strides
[[1 2 3] [4 5 6]] dytpe int64 shape (2, 3) strides (24, 8)
x = x.astype('complex') print x print 'dytpe', x.dtype print 'shape', x.shape print 'strides', x.strides
[[ 1.+0.j 2.+0.j 3.+0.j] [ 4.+0.j 5.+0.j 6.+0.j]] dytpe complex128 shape (2, 3) strides (48, 16)
Creating arrays
# from lists x_list = [(i,j) for i in range(2) for j in range(3)] print x_list, '\n' x_array = np.array(x_list) print x_array
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)] [[0 0] [0 1] [0 2] [1 0] [1 1] [1 2]]
# Using convenience functions print np.ones((3,2)), '\n' print np.zeros((3,2)), '\n' print np.eye(3), '\n' print np.diag([1,2,3]), '\n' print np.fromfunction(lambda i, j: (i-2)**2+(j-2)**2, (5,5))
[[ 1. 1.] [ 1. 1.] [ 1. 1.]] [[ 0. 0.] [ 0. 0.] [ 0. 0.]] [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] [[1 0 0] [0 2 0] [0 0 3]] [[ 8. 5. 4. 5. 8.] [ 5. 2. 1. 2. 5.] [ 4. 1. 0. 1. 4.] [ 5. 2. 1. 2. 5.] [ 8. 5. 4. 5. 8.]]
Array indexing
# Create a 10 by 6 array from normal deviates and convert to ints n, nrows, ncols = 100, 10, 6 xs = np.random.normal(n, 15, size=(nrows, ncols)).astype('int') xs
array([[ 84, 108, 96, 93, 82, 115], [ 87, 70, 96, 132, 111, 108], [ 96, 85, 120, 72, 62, 66], [112, 86, 98, 86, 74, 98], [ 75, 91, 116, 105, 82, 122], [ 95, 119, 84, 89, 93, 87], [118, 113, 94, 89, 67, 107], [120, 105, 85, 100, 131, 120], [ 91, 137, 103, 94, 115, 92], [ 73, 98, 81, 106, 128, 75]])
# Use slice notation print(xs[0,0]) print(xs[-1,-1]) print(xs[3,:]) print(xs[:,0]) print(xs[::2,::2]) print(xs[2:5,2:5])
84 75 [112 86 98 86 74 98] [ 84 87 96 112 75 95 118 120 91 73] [[ 84 96 82] [ 96 120 62] [ 75 116 82] [118 94 67] [ 91 103 115]] [[120 72 62] [ 98 86 74] [116 105 82]]
# Indexing with list of integers print(xs[0, [1,2,4,5]])
[108 96 82 115]
# Boolean indexing print(xs[xs % 2 == 0]) xs[xs % 2 == 0] = 0 # set even entries to zero print(xs)
[ 84 108 96 82 70 96 132 108 96 120 72 62 66 112 86 98 86 74 98 116 82 122 84 118 94 120 100 120 94 92 98 106 128] [[ 0 0 0 93 0 115] [ 87 0 0 0 111 0] [ 0 85 0 0 0 0] [ 0 0 0 0 0 0] [ 75 91 0 105 0 0] [ 95 119 0 89 93 87] [ 0 113 0 89 67 107] [ 0 105 85 0 131 0] [ 91 137 103 0 115 0] [ 73 0 81 0 0 75]]
# Extracting lower triangular, diagonal and upper triangular matrices a = np.arange(16).reshape(4,4) print a, '\n' print np.tril(a, -1), '\n' print np.diag(np.diag(a)), '\n' print np.triu(a, 1)
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[ 0 0 0 0] [ 4 0 0 0] [ 8 9 0 0] [12 13 14 0]] [[ 0 0 0 0] [ 0 5 0 0] [ 0 0 10 0] [ 0 0 0 15]] [[ 0 1 2 3] [ 0 0 6 7] [ 0 0 0 11] [ 0 0 0 0]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论