---
title: 'Project documentation template'
disqus: hackmd
---
numpy
===
[TOC]
random.choice
-------------
```python=
import numpy as np
for a in range(10):
np1=np.random.choice(5, 3)
print(np1,end=" ")
print("\n----1---")
for a in range(10):
np1 = np.random.choice(5, 3, p=[0.1, 0.3, 0.6, 0, 0])
print(np1,end=" ")
print("\n----2---")
```
>[3 0 4] [2 1 3] [3 4 1] [1 2 1] [3 3 0] [1 4 3] [2 3 1] [3 0 2] [4 3 1] [1 3 4]
----1---
[2 2 1] [2 2 2] [2 1 2] [2 1 0] [2 2 2] [2 2 1] [0 2 2] [1 2 1] [2 1 2] [1 2 1]
----2---
```python=
import numpy as np
for a in range(10):
np1=np.random.choice(5, 3,p=[0.1,0.3,0.6,0,0],replace=False)
print(np1,end=" ")
print("\n----3---")
for a in range(10):
np1=np.random.choice(5, 3,replace=False)
print(np1,end=" ")
print("\n----4---")
```
>[1 2 0] [1 2 0] [0 2 1] [2 1 0] [2 1 0] [2 1 0] [1 2 0] [2 0 1] [1 0 2] [1 2 0]
----3---
[4 0 2] [3 4 0] [4 1 2] [0 3 2] [1 3 4] [4 0 3] [2 4 3] [2 0 3] [4 0 2] [4 3 0]
----4---
```python=
import numpy as np
aa_milne_arr = ['a', 'b', 'c', 'd']
for a in range(5):
np2=np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
print(np2,end=" ")
print("\n----5---")
```
>['d' 'd' 'a' 'a' 'b'] ['b' 'a' 'a' 'a' 'd'] ['c' 'a' 'a' 'a' 'd'] ['a' 'a' 'a' 'd' 'a'] ['d' 'a' 'a' 'd' 'c']
----5---
random.binomial
-------------
```python=
import numpy.random as nr
import numpy as np
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
```
>0.26
0.3
0.28
0.3
0.28
0.4
```python=
#二項分配調查 (HW)
import numpy.random as nr
import numpy as np
import matplotlib.pyplot as plt
results = [np.mean(nr.binomial(1, 0.25, 30)) for _ in range(100)]
sample_mean = np.mean(results)
plt.hist(results)
plt.vlines(0.25, 0.0, 28.0, color = 'red')
plt.vlines(sample_mean, 0.0, 28.0, color = 'black')
plt.xlabel('Results')
plt.ylabel('Frequency')
plt.title('Histogram of results')
plt.show()
```
>
```python=
#產生大量二項分配結果
import numpy.random as nr
import numpy as np
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
print(np.mean(nr.binomial(1, 0.3, 50)))
#請加入語法
results = [np.mean(nr.binomial(1,0.3,50)) for _ in range(100)]
print(results)
```
>0.3
0.28
0.3
0.28
0.4
0.26
[0.28, 0.34, 0.2, 0.34, 0.34, 0.26, 0.3, 0.2, 0.18, 0.28, 0.24, 0.34, 0.28, 0.28, 0.22, 0.22, 0.34, 0.26, 0.26, 0.28, 0.24, 0.24, 0.36, 0.22, 0.22, 0.24, 0.38, 0.24, 0.18, 0.3, 0.28, 0.3, 0.24, 0.3, 0.34, 0.26, 0.38, 0.24, 0.28, 0.22, 0.36, 0.24, 0.38, 0.28, 0.24, 0.38, 0.26, 0.3, 0.3, 0.44, 0.26, 0.3, 0.34, 0.32, 0.32, 0.38, 0.36, 0.34, 0.18, 0.22, 0.32, 0.3, 0.32, 0.32, 0.24, 0.32, 0.32, 0.22, 0.24, 0.14, 0.26, 0.38, 0.38, 0.28, 0.18, 0.34, 0.24, 0.32, 0.24, 0.34, 0.36, 0.32, 0.36, 0.28, 0.24, 0.26, 0.14, 0.52, 0.28, 0.32, 0.22, 0.24, 0.32, 0.22, 0.26, 0.24, 0.32, 0.28, 0.26, 0.28]
random.normal
-------------
```python=
import numpy.random as nr
nr.seed(12345)
#請加入語法
p1=nr.normal(10,2,50)
nr.seed(3344)
#請加入語法
p2=nr.binomial(1,0.3,50)
nr.seed(3344)
#請加入語法
sample1=nr.binomial(1,0.3,50)
print(p1)
print(p2)
print(sample1)
from scipy import stats
#請加入語法
#print(stats.itemfreq(sample1))
```
>[ 9.59058468 10.95788668 8.96112257 8.88853939 13.93156115 12.78681167
10.18581575 10.56349231 11.53804514 12.49286947 12.01437872 7.40755778
10.54998327 10.45782576 12.70583367 11.77285868 5.99672538 9.25631493
13.33805062 9.12286053 8.92051711 10.95397002 16.49788784 7.95754495
8.84582539 10.24824255 10.60522712 11.04754414 10.00188056 12.68761959
8.57291203 8.33769292 5.25953669 6.27847842 8.2784852 11.12029059
7.46813102 10.23965425 7.8729751 10.66576543 5.28116239 9.60091409
6.91600894 8.05852818 7.3859395 10.57269949 10.75596822 8.49222693
10.6625713 12.69948443]
[0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0 0 1 1]
[0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0 0 1 1]
```python=
#P1
import numpy as np
import numpy.random as nr
nr.seed(515212)
#collect a sample of 100 males
males = nr.normal(5, 3, 100)
#nr.seed(515212)
#collect a sample of 100 females
females = nr.normal(5, 3, 100)
print(np.mean(males))
print(np.mean(females))
```
>5.337790559409466
4.8650214885878595
random.sample
-------------
```python=
import random
a=random.sample(range(1,39),6)
print (a)
```
>[20, 6, 7, 22, 11, 36]
arange
----
```python=
import matplotlib.pyplot as plt
import numpy as np
#x+y=9
#5a+15b=75
x=np.arange(0,10,1)
y=np.empty(9)
#y-2x=1
y=9-x
plt.plot(x,y)
a=np.arange(0,16,1)
b=np.empty(15)
#y-2x=1
b=(75-5*a)/15
plt.plot(a,b)
plt.grid()
plt.show()
```
>
linspace
----
```python=
import matplotlib.pyplot as plt
import numpy as np
# create 1000 equally spaced points between -10 and 10
x = np.linspace(-10, 10, 1000)
# calculate the y value for each element of the x vector
y = x**2 + 2*x + 2
plt.plot(x,y)
plt.show()
```
> 
percentile
----
```python=
import numpy as np
a = np.array ([25,26,27,29,32,32,33,33,34,35,35,36,37,39,41,42,42,43,43,43,44,44,44,44,45,48,50,50,50,50,51,53,54,55,56,57,57,60,61,61,65,66,67,68,74,74,76,77,77,80])
print (np.percentile(a, 25))
print (np.percentile(a, 50))
print (np.percentile(a, 75))
print (len(a))
```
>37.5
46.5
59.25
50
quantile
----
```python=
import numpy as np
#import stats as sts
a = np.array ([25,26,27,29,32,32,33,33,34,35,35,36,37,39,41,42,42,43,43,43,44,44,44,44,45,48,50,50,50,50,51,53,54,55,56,57,57,60,61,61,65,66,67,68,74,74,76,77,77,80])
#test = np.array(a)
print('上四分位數為',np.quantile(a,0.25))
print('下四分位數為',np.quantile(a,0.75))
```
>上四分位數為 37.5
下四分位數為 59.25
```python=
import pandas as pd
s = pd.Series(a)
t = pd.Series(b)
print (s.quantile([.25, .5, .75]))
print ("===============")
print (t.quantile([.25, .5, .75]))
```
>0.25 37.50
0.50 46.50
0.75 59.25
dtype: float64
===============
0.25 35.25
0.50 43.50
0.75 51.00
dtype: float64
loadtxt()
---
```python=
year, age_m, age_f = np.loadtxt('eg7-marriage-ages.txt', unpack=True,skiprows=3)
fig = plt.figure()
ax = fig.add_subplot(111)
#請加入語法
ax.plot(year,age_m,marker='$/u2642$',markersize=14,c='blue',lw=2,mfc='blue',mec='blue')
ax.plot(year,age_f,marker='$/u2640$',markersize=14,c='magenta',lw=2,mfc='magenta',mec='magenta')
ax.grid()
ax.set_xlabel('Year')
ax.set_ylabel('Age')
ax.set_title('Median age at first marriage in the US, 1890 - 2010')
plt.show()
```
```python=
fig = plt.figure()
ax = fig.add_subplot(111)
cities = ['Boston', 'Houston', 'Detroit', 'San Jose', 'Phoenix']
linestyles = [{'ls': '-'}, {'ls': '--'}, {'ls': ':'}, {'ls': '-.'},
{'dashes': [2, 4, 2, 4, 8, 4]}]
for i, city in enumerate(cities):
filename = '{}.csv'.format(city.lower()).replace(' ', '_')
#請加入語法
yr,pop = np.loadtxt(filename,unpack=True)
line, = ax.plot(yr,pop/1.e6,label=city,color='k',**linestyles[i])
ax.legend(loc='upper left')
ax.set_xlim(1800,2020)
ax.set_xlabel('Year')
ax.set_ylabel('Population(millions)')
plt.show()
```
```python=
arr = np.loadtxt('./datasets/data1.csv',skiprows=1, delimiter=',')
print(arr)
v1, v2 = np.loadtxt('./datasets/data1.csv',skiprows=1,unpack=True, delimiter=',')
print("Value1=",v1)
print("Value2=",v2)
```
>[[0.2 0.1]
[0.4 0.5]
[0.1 0.6]
[0.1 0.3]
[0.7 0.3]]
>Value1= [0.2 0.4 0.1 0.1 0.7]
>Value2= [0.1 0.5 0.6 0.3 0.3]
```python=
arr = np.loadtxt('./datasets/data2.csv',skiprows=1, delimiter=',')
print(arr)
v1, v2 = np.loadtxt('./datasets/data2.csv',skiprows=1,unpack=True, delimiter=',')
print("Value1=",v1)
print("Value2=",v2)
```
>[[0.2 0.1]
[0.4 0.6]
[0.1 0.3]
[0.1 0.3]]
>Value1= [0.2 0.4 0.1 0.1]
>Value2= [0.1 0.6 0.3 0.3]
genfromtxt
---
```python=
arr1 = np.genfromtxt('./datasets/data2.csv', delimiter=',', skip_header =1)
print(arr1)
np.savetxt('data2a.csv', arr1, delimiter=',')
arr2 = np.genfromtxt('./datasets/data2.csv', delimiter=',', skip_header =1, filling_values= 0.0)
print(arr2)
np.savetxt('data2b.csv', arr2, delimiter=',',fmt='%.2f')
```
>[[0.2 0.1]
[0.4 0.6]
[0.1 0.3]
[0.1 0.3]]
[[0.2 0.1]
[0.4 0.6]
[0.1 0.3]
[0.1 0.3]]
```python=
arr = np.genfromtxt('./datasets/data3.csv', delimiter=',', dtype='int', skip_header=1, encoding='utf-8')
print(arr)
print(arr.shape)
print(np.size(arr, axis=0))
print(np.size(arr, axis=1))
print(arr[0,0])
print(arr[1,2])
print(arr[2,3])
```
>[[ 106 1 8397 364 209 291 398 516 524 630 633 608 615 713
648 584 509 308 304 298 171 58 15 1]
[ 106 2 8393 359 210 293 394 513 524 624 636 599 630 709
649 584 515 303 302 300 174 59 15 1]
[ 106 3 8389 353 219 283 397 509 530 617 635 602 636 707
643 585 517 304 307 297 174 59 14 1]
[ 106 4 8376 349 211 277 395 511 532 614 641 603 635 699
647 586 519 304 305 297 175 61 14 1]
[ 106 5 8378 352 218 273 395 512 526 610 642 602 638 687
652 594 518 305 307 298 172 61 15 1]
[ 106 6 8395 352 225 271 393 516 529 612 642 603 629 693
656 597 516 308 306 297 174 59 16 1]
[ 106 7 8411 363 220 276 387 518 529 611 650 602 619 699
652 606 521 305 308 294 174 60 16 1]
[ 106 8 8409 362 222 277 380 515 533 605 649 608 615 705
646 607 529 301 309 295 177 57 16 1]
[ 106 9 8406 359 223 271 371 527 528 601 645 606 621 705
651 606 531 305 302 302 176 59 16 1]]
(9, 24)
9
24
106
8393
353
random.random_integers
---
```python=
arr1 = np.random.random_integers(1, 9, [2,3])
print(arr1)
np.save('myArray', arr1)
print('----------')
arr2 = np.load('myArray.npy')
print(arr2)
print('----------')
```
>[[5 1 4]
[3 1 8]]
>[[5 1 4]
[3 1 8]]