<jupyter_start><jupyter_text>Python tutorial 2 ![python](http://i.imgur.com/bc2xk.png)  1. random number generators: numpy.random[https://docs.scipy.org/doc/numpy/reference/routines.random.html](https://docs.scipy.org/doc/numpy/reference/routines.random.html)<jupyter_code>import numpy as np
print(dir(np.random))<jupyter_output>['Lock', 'RandomState', '__RandomState_ctor', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_numpy_tester', 'absolute_import', 'bench', 'beta', 'binomial', 'bytes', 'chisquare', 'choice', 'dirichlet', 'division', 'exponential', 'f', 'gamma', 'geometric', 'get_state', 'gumbel', 'hypergeometric', 'info', 'laplace', 'logistic', 'lognormal', 'logseries', 'mtrand', 'multinomial', 'multivariate_normal', 'negative_binomial', 'noncentral_chisquare', 'noncentral_f', 'normal', 'np', 'operator', 'pareto', 'permutation', 'poisson', 'power', 'print_function', 'rand', 'randint', 'randn', 'random', 'random_integers', 'random_sample', 'ranf', 'rayleigh', 'sample', 'seed', 'set_state', 'shuffle', 'standard_cauchy', 'standard_exponential', 'standard_gamma', 'standard_normal', 'standard_t', 'test', 'triangular', 'uniform', 'vonmises', 'wald', 'warnings', 'weibull', 'zipf']<jupyter_text>how to draw samples from a gaussian distribution<jupyter_code>%pylab inline
import matplotlib.pyplot as plt

from matplotlib import rcParams
rcParams.update({'font.size': 20})

rdata = np.random.randn(1000)
fig = plt.figure(figsize=(6, 4))
plt.hist(rdata)
print(np.mean(rdata), np.median(rdata), np.std(rdata))
np.std(np.random.randn(1000) + np.random.randn(1000))<jupyter_output><empty_output><jupyter_text>other distributions ...<jupyter_code>randexp = np.random.exponential(2., size=(1000))
hist(randexp, np.linspace(0,10,50));
randps = np.random.poisson(10, size=(10000,))
hist(randps, np.arange(20));
M = 4.
m = 15.
merr = 0.1

rand_m = np.random.randn(1000)*0.1+m
hist(rand_m);<jupyter_output><empty_output><jupyter_text>$\log_{10}(d) = 1 + \mu /5 $<jupyter_code>rand_d = 10.**(1+0.2*(rand_m-M))
hist(rand_d, np.linspace(1300, 1900, 30));<jupyter_output><empty_output><jupyter_text>2. plotting<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams.update({'font.size':20})
# fig = plt.figure(figsize=(10,10))
x = np.linspace(0, 6*np.pi, 100)
plt.plot(x, np.cos(x), 'rv--');
plt.plot(x, np.sin(x), 'bs-.', alpha=.1);

plt.scatter(x, np.cos(x)+0.2, s=np.random.rand(*x.shape)*80, c=np.sin(x)+1)<jupyter_output><empty_output><jupyter_text>3. IO (text files & fits files)<jupyter_code># use numpy.savetxt & numpy.loadtxt
a = np.random.randn(4, 5)
print(a)
np.savetxt('./data/text/rdata.dat', a)
!gedit ./data/text/rdata.dat
b = np.loadtxt('./data/text/rdata.dat')
print(b)
a==b.reshape(4, 5)
impath = "./data/image_data/G178_final.850.fits"

%pylab inline
from matplotlib import rcParams
rcParams.update({'font.size': 20})

from aplpy import FITSFigure
fig = FITSFigure(impath)
fig.show_colorscale()
impath = "./data/wise_image/w1_cut.fits"

%pylab inline
%matplotlib inline
from matplotlib import rcParams
rcParams.update({'font.size': 20})
from aplpy import FITSFigure
fig = FITSFigure(impath)
fig.show_colorscale()
fig.show_grayscale()<jupyter_output>Populating the interactive namespace from numpy and matplotlib
INFO: Auto-setting vmin to -3.489e+01 [aplpy.core]
INFO: Auto-setting vmax to  4.563e+02 [aplpy.core]
INFO: Auto-setting vmin to -3.489e+01 [aplpy.core]
INFO: Auto-setting vmax to  4.563e+02 [aplpy.core]<jupyter_text>LAMOST spectra<jupyter_code>ls ./data/lamost_dr2_spectra/
specpath = "./data/lamost_dr2_spectra/spec-55892-F9205_sp09-174.fits"
from astropy.io import fits
hl = fits.open(specpath)
hl.info()
hl
hl[0]
hl[0].header<jupyter_output><empty_output><jupyter_text>CRVAL1  =               3.5682 / Central wavelength (log10) of first pixel      CD1_1   =               0.0001 / Log10 dispersion per pixel                     CRPIX1  =                    1 / Starting pixel (1-indexed)                     CTYPE1  = 'LINEAR  '           /<jupyter_code>wave = 10.**(hl[0].header['CRVAL1']+np.arange(hl[0].header['NAXIS1'])*hl[0].header['CD1_1'])
wave
np.log10(wave)
flux = hl[0].data # [flux, ivar, wave, and_mask, or_mask]
flux
%pylab
%matplotlib inline
fig = figure(figsize=(10, 5))
plt.plot(wave, flux[0, :])
# fig.savefig("here goes the file path")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17995955964951696, 'fraction_numerical': 0.04066501909683217, 'mean_word_length': 5.786585365853658, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '73506', 'license': 'MIT', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1740, 'n_words': 322, 'path': 'python/slides2.ipynb', 'repo_name': 'hypergravity/astrostatistics', 'size': '4451'}