0% found this document useful (0 votes)
13 views

BDP Week3

Uploaded by

pd9cnrpyfz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

BDP Week3

Uploaded by

pd9cnrpyfz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Big data Physics

week 3
TA: Changhee Cho
[email protected]
Overview
• numpy array

• matplotlib.pyplot

• read/write files
What is numpy?
• numpy (Numerical Python) is a popular library for scientific
computing

• numpy use data structure called ‘array’

• numpy can do vector calculations, n-dimensional array, etc.

• numpy is much faster than python’s standard library!


Numpy array
you can use some basic functions like a list
You need to import numpy first
zeros, ones, shape, size
np.zeros(shape) shape of the array number of elements
index
You can get elements using index
append
Wrong code!

recommended correct, but not recommended


n-dimensional array
2d array from list

3d array using np.zeros


n-dimensional array
axis 0, axis1, axis2
n-dimensional array
axis 0 ↔ axis1
Numpy functions – sum, mean, std
sum, mean, std
Numpy functions - random
random arrays
Numpy functions – random.choice
Practice 1
1. Obtain 10000 poisson random integer numbers(with
lambda=100) and calculate the standard deviation
hint: use std

2. Repeat 1 for 2000 times. You will get 2000 standard deviations.
what is the mean value of 2000 standard deviations?
hint: use for loop, and save the data in additional array or list

3. [Hard]Can you make a code in question2 without using any loop?


Numpy functions - arange
Arithmetic in array
If two arrays have same size, It is also possible to add or multiply
arithmetic operators perform element-wise calculations single value to numpy array
Mathematical functions
useful functions
useful constants
Mathematical functions
you can use numpy functions to array
Matplotlib library
line plot scatter plot
Matplotlib library
bar chart histogram
Matplotlib library
imshow in 2d imshow 3d (2d+RGB)
Matplotlib library

line plot
plt.plot(x,y) good when x data is sequential
(2d data)
good when we want to see correlation or trend
scatter plot
plt.scatter(x,y,(s),(c)) between 2 variables
(2d, 3d, 4d)
or want to draw 3d, 4d plot (spatial 2d+a)
bar plot
plt.bar(x,height) good when x data is categorical
(2d data)

histogram
plt.hist(x,(bins)) when we want to see the distribution of data
(1d data)

image plot draw 2d images, or 2d array


plt.imshow(array)
(2d image, 3d data) or if data is 3d(spatial 2d+ additional 1d)
Example
• plot y=sin(x) in range[0,8)
Useful functions
plt.xlabel(‘label name’)
add label name on axis
plt.ylabel(‘label name’)
plt.title(‘title name’) add title on top middle
plt.xscale(‘log’)
plot in log scale
plt.yscale(‘log’)
plt.text(x, y, ’text’) add ‘text’ on (x, y) position
plt.xlim(xmin, xmax)
set minimum and maximum value of axis
plt.ylim(ymin, ymax)
plt.figure(figsize=(width,height)) change figure size
plt.savefig(‘figure name.png’) save figure
plt.show() show figure(not necessary in jupyter notebook)
plt.legend() show legend
Example
Loading files in colab (1)

2) Upload

1) Files
Loading files in colab (2)

2) Mount to Google drive

1) Files
Loading files in colab (2)

2) Go to drive/MyDrive/ …

1) Files

3) Copy path
savetxt, loadtxt
Practice 2
1
1. plot 𝑦 = in range x=[0,10)
1+𝑒 −𝑥

1 𝜋2
2. Check ∞
σ𝑖=1 2 =
𝑖 6
hint: use arange, sum

3. Differentiate y=sin(x) in range x=[0,10) using finite difference


𝑓 𝑥+Δ𝑥 −𝑓(𝑥)
method 𝑓′(𝑥) ≈
Δ𝑥
hint: use index! (do not use for loop)
Practice - 3
1. plot at least 3 different figures using mass-height data
data is given in I-class system (sample code below)

You might also like