Skip to content

Commit 6abb884

Browse files
committed
add numpy and scipy。xuming 20170329
1 parent fcfede5 commit 6abb884

8 files changed

Lines changed: 326 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ ENV/
9090

9191
.idea
9292

93-
.idea/*
93+
.idea/*
94+
.idea/

02.numpy/06.txt2array.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,59 @@ def date_converter(s):
7878
# pandas ——一个用来处理时间序列的包中包含处理各种文件的方法,
7979
# 具体可参见它的文档:
8080
# http://pandas.pydata.org/pandas-docs/stable/io.html
81+
82+
# 将数组写入文件¶
83+
# savetxt 可以将数组写入文件,默认使用科学计数法的形式保存:
84+
a = np.array([[1, 2, 3], [5, 6, 7]])
85+
np.savetxt("out.txt", a)
86+
# 可以用类似printf 的方式指定输出的格式:
87+
a = np.array([[1, 2, 3], [5, 6, 7]])
88+
print(a.shape[1])
89+
np.savetxt('out_fmt.txt', a, fmt=['%d'] * a.shape[1], newline='\n')
90+
91+
with open('out_fmt.txt') as f:
92+
for line in f:
93+
print(line)
94+
95+
m = zip([1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e'])
96+
print(m)
97+
z = np.array(m)
98+
print(z)
99+
np.savetxt('out_str_fmt.txt', z, fmt=['%s'] * z.shape[1])
100+
101+
# Numpy 二进制格式
102+
# 保存的方法:
103+
#
104+
# save(file, arr) 保存单个数组,.npy 格式
105+
# savez(file, *args, **kwds) 保存多个数组,无压缩的 .npz 格式
106+
# savez_compressed(file, *args, **kwds) 保存多个数组,有压缩的 .npz 格式
107+
a = np.array([[1, 2], [3, 4]])
108+
np.save('out.npy', a)
109+
110+
# 二进制与文本大小比较
111+
a = np.arange(10000.)
112+
np.savetxt('a.txt', a)
113+
# 查看大小:
114+
import os
115+
116+
print(os.stat('a.txt').st_size)
117+
# 保存为二进制
118+
np.save('a.npy', a)
119+
print(os.stat('a.npy').st_size)
120+
# 二进制文件大约是文本文件的三分之一
121+
122+
# 保存多个数组
123+
a = np.array([[1, 2], [3, 4]])
124+
b = np.arange(1000)
125+
print(b)
126+
np.savez('ab.npz', a=a, b=b)
127+
128+
# 加载数据
129+
ab = np.load('ab.npz')
130+
print(os.stat('ab.npz').st_size) # file size
131+
print(ab.keys())
132+
print(ab['a'])
133+
print(ab['b'].shape)
134+
135+
np.savez_compressed('ab_compressed.npz', a=a, b=b)
136+
print(os.stat('ab_compressed.npz').st_size) # file size

02.numpy/07.generate_array.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@description:
4+
@author:XuMing
5+
"""
6+
from __future__ import print_function
7+
from __future__ import unicode_literals
8+
9+
import numpy as np
10+
11+
# 生成数组的函数
12+
# arange 生成数组,[start,stop)
13+
# arange(start, stop=None, step=1, dtype=None)
14+
print(np.arange(5)) # [0 1 2 3 4]
15+
16+
a = np.arange(0, 2 * np.pi, np.pi / 4)
17+
print(a)
18+
19+
# linspace
20+
# linspace(start,stop,N)
21+
# 产生N个等距分布在[start,stop]间的元素组成的数组,包括start,stop
22+
print(np.linspace(0, 1, 5)) # [ 0. 0.25 0.5 0.75 1. ]
23+
24+
# logspace
25+
# logspace(start, stop, N)
26+
# 产生 N 个对数等距分布的数组,默认以10为底:
27+
print(np.logspace(0, 1, 5))
28+
# 产生的值为$\left[10^0, 10^{0.25},10^{0.5},10^{0.75},10^1\right]$。
29+
30+
# 二维平面中生成一个网格:meshgrid
31+
x_ticks = np.linspace(-1, 1, 5)
32+
y_ticks = np.linspace(-1, 1, 5)
33+
x, y = np.meshgrid(x_ticks, y_ticks)
34+
print(x_ticks)
35+
print(x)
36+
37+
# 图例
38+
import matplotlib.pyplot as plt
39+
from matplotlib import cm
40+
41+
42+
def f(x, y):
43+
# sinc 函数
44+
r = np.sqrt(x ** 2 + y ** 2)
45+
result = np.sin(r) / r
46+
result[r == 0] = 1.0
47+
return result
48+
49+
50+
x_ticks = np.linspace(-10, 10, 51)
51+
y_ticks = np.linspace(-10, 10, 51)
52+
53+
x, y = np.meshgrid(x_ticks, y_ticks, sparse=True)
54+
print(x) # x, y 中有很多冗余的元素,这里提供了一个 sparse 的选项去冗余
55+
z = f(x, y)
56+
57+
fig = plt.figure()
58+
ax = fig.add_subplot(111, projection='3d')
59+
ax.plot_surface(x, y, z,
60+
rstride=1, cstride=1,
61+
cmap=cm.YlGnBu_r)
62+
ax.set_xlabel('x')
63+
ax.set_ylabel('y')
64+
ax.set_zlabel('z')
65+
plt.show()

02.numpy/08.memmap.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@description:
4+
@author:XuMing
5+
"""
6+
from __future__ import print_function
7+
from __future__ import unicode_literals
8+
9+
# Numpy 有对内存映射的支持。
10+
11+
# 内存映射也是一种处理文件的方法,主要的函数有:
12+
#
13+
# memmap
14+
# frombuffer
15+
# ndarray constructor
16+
17+
# 使用内存映射文件处理存储于磁盘上的文件时,将不必再对文件执行I/O操作,
18+
# 使得内存映射文件在处理大数据量的文件时能起到相当重要的作用。
19+
20+
# memmap(filename,
21+
# dtype=uint8,
22+
# mode='r+'
23+
# offset=0
24+
# shape=None
25+
# order=0)
26+
27+
# mode 表示文件被打开的类型:
28+
# r 只读
29+
# c 复制+写,但是不改变源文件
30+
# r+ 读写,使用 flush 方法会将更改的内容写入文件
31+
# w+ 写,如果存在则将数据覆盖

02.numpy/09.add.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@description:
4+
@author:XuMing
5+
"""
6+
from __future__ import print_function
7+
from __future__ import unicode_literals
8+
9+
import numpy as np
10+
11+
a = np.array([[1, 2], [3, 4]])
12+
b = np.arange(2)
13+
print(a, b)
14+
15+
a_list = list(a)
16+
b_list = list(b)
17+
d = a_list + b_list
18+
print(d)
19+
c = a_list.extend(b_list)
20+
print(c)
21+
print(a_list)
22+
print(np.array(a_list))
23+
24+
print(np.append(a, b))
25+
26+
k = []
27+
k.append(b_list)
28+
print(k)
29+
30+
k_list = [1, 2]
31+
k.extend(k_list)
32+
print(k)

03.scipy/01.module.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@description:
4+
@author:XuMing
5+
"""
6+
from __future__ import print_function
7+
from __future__ import unicode_literals
8+
9+
# 子模块 描述
10+
# cluster 聚类算法
11+
# constants 物理数学常数
12+
# fftpack 快速傅里叶变换
13+
# integrate 积分和常微分方程求解
14+
# interpolate 插值
15+
# io 输入输出
16+
# linalg 线性代数
17+
# odr 正交距离回归
18+
# optimize 优化和求根
19+
# signal 信号处理
20+
# sparse 稀疏矩阵
21+
# spatial 空间数据结构和算法
22+
# special 特殊方程
23+
# stats 统计分布和函数
24+
# weave C/C++ 积分
25+
26+
# 使用scipy之前,基础模块需要导入:
27+
import numpy as np
28+
29+
# 使用scipy的子模块时,需要导入:
30+
from scipy import optimize
31+
32+
print(np.info(optimize))

03.scipy/02.polynomial.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@description:
4+
@author:XuMing
5+
"""
6+
from __future__ import print_function
7+
from __future__ import unicode_literals
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
data = np.genfromtxt("C-067.txt",
13+
delimiter="\t", # TAB 分隔
14+
skip_header=2)
15+
16+
print(data[:7])
17+
print(data[0])
18+
p = plt.plot(data[0], data[1], 'kx')
19+
t = plt.title("janaf data for mechane")
20+
a = plt.axis([0, 6000, 30, 120])
21+
x = plt.xlabel("temperature (K)")
22+
y = plt.ylabel(r"$C_p$ ($\frac{kJ}{kg K}$)")
23+
# plt.show()
24+
plt.close()
25+
26+
# 径向基函数
27+
x = np.linspace(-3, 3, 100)
28+
# 高斯函数
29+
plt.plot(x, np.exp(-1 * x ** 2))
30+
t = plt.title("Gaussian")
31+
plt.savefig('Gaussian.png')
32+
plt.show()
33+
34+
# 高维 RBF 插值
35+
# 三维数据点:
36+
x, y = np.mgrid[-np.pi / 2:np.pi / 2:5j, -np.pi / 2:np.pi / 2:5j]
37+
z = np.cos(np.sqrt(x ** 2 + y ** 2))
38+
fig = plt.figure(figsize=(12, 6))
39+
ax = fig.gca(projection="3d")
40+
ax.scatter(x, y, z)
41+
fig.savefig("mplot3d.jpg")
42+
plt.show()

03.scipy/C-067.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
C-067
2+
A B C D E F G H
3+
100 33.258 149.500 216.485 -6.698 -69.644 -64.353 33.615
4+
200 33.473 172.577 189.418 -3.368 -72.027 -58.161 15.190
5+
250 34.216 180.113 186.829 -1.679 -73.426 -54.536 11.395
6+
298.15 35.639 186.251 186.251 0. -74.873 -50.768 8.894
7+
300 35.708 186.472 186.252 0.066 -74.929 -50.618 8.813
8+
350 37.874 192.131 186.694 1.903 -76.461 -46.445 6.932
9+
400 40.500 197.356 187.704 3.861 -77.969 -42.054 5.492
10+
450 43.374 202.291 189.053 5.957 -79.422 -37.476 4.350
11+
500 46.342 207.014 190.614 8.200 -80.802 -32.741 3.420
12+
600 52.227 215.987 194.103 13.130 -83.308 -22.887 1.993
13+
700 57.794 224.461 197.840 18.635 -85.452 -12.643 0.943
14+
800 62.932 232.518 201.675 24.675 -87.238 -2.115 0.138
15+
900 67.601 240.205 205.532 31.205 -88.692 8.616 -0.500
16+
1000 71.795 247.549 209.370 38.179 -89.849 19.492 -1.018
17+
1100 75.529 254.570 213.162 45.549 -90.750 30.472 -1.447
18+
1200 78.833 261.287 216.895 53.270 -91.437 41.524 -1.807
19+
1300 81.744 267.714 220.558 61.302 -91.945 52.626 -2.115
20+
1400 84.305 273.868 224.148 69.608 -92.308 63.761 -2.379
21+
1500 86.556 279.763 227.660 78.153 -92.553 74.918 -2.609
22+
1600 88.537 285.413 231.095 86.910 -92.703 86.088 -2.810
23+
1700 90.283 290.834 234.450 95.853 -92.780 97.265 -2.989
24+
1800 91.824 296.039 237.728 104.960 -92.797 108.445 -3.147
25+
1900 93.188 301.041 240.930 114.212 -92.770 119.624 -3.289
26+
2000 94.399 305.853 244.057 123.592 -92.709 130.802 -3.416
27+
2100 95.477 310.485 247.110 133.087 -92.624 141.975 -3.531
28+
2200 96.439 314.949 250.093 142.684 -92.521 153.144 -3.636
29+
2300 97.301 319.255 253.007 152.371 -92.409 164.308 -3.732
30+
2400 98.075 323.413 255.854 162.141 -92.291 175.467 -3.819
31+
2500 98.772 327.431 258.638 171.984 -92.174 186.622 -3.899
32+
2600 99.401 331.317 261.359 181.893 -92.060 197.771 -3.973
33+
2700 99.971 335.080 264.020 191.862 -91.954 208.916 -4.042
34+
2800 100.489 338.725 266.623 201.885 -91.857 220.058 -4.105
35+
2900 100.960 342.260 269.171 211.958 -91.773 231.196 -4.164
36+
3000 101.389 345.690 271.664 222.076 -91.705 242.332 -4.219
37+
3100 101.782 349.021 274.106 232.235 -91.653 253.465 -4.271
38+
3200 102.143 352.258 276.498 242.431 -91.621 264.598 -4.319
39+
3300 102.474 355.406 278.842 252.662 -91.609 275.730 -4.364
40+
3400 102.778 358.470 281.139 262.925 -91.619 286.861 -4.407
41+
3500 103.060 361.453 283.391 273.217 -91.654 297.993 -4.447
42+
3600 103.319 364.360 285.600 283.536 -91.713 309.127 -4.485
43+
3700 103.560 367.194 287.767 293.881 -91.798 320.262 -4.521
44+
3800 103.783 369.959 289.894 304.248 -91.911 331.401 -4.555
45+
3900 103.990 372.658 291.982 314.637 -92.051 342.542 -4.588
46+
4000 104.183 375.293 294.032 325.045 -92.222 353.687 -4.619
47+
4100 104.363 377.868 296.045 335.473 -92.422 364.838 -4.648
48+
4200 104.531 380.385 298.023 345.918 -92.652 375.993 -4.676
49+
4300 104.688 382.846 299.967 356.379 -92.914 387.155 -4.703
50+
4400 104.834 385.255 301.879 366.855 -93.208 398.322 -4.729
51+
4500 104.972 387.612 303.758 377.345 -93.533 409.497 -4.753
52+
4600 105.101 389.921 305.606 387.849 -93.891 420.679 -4.777
53+
4700 105.223 392.182 307.424 398.365 -94.281 431.869 -4.800
54+
4800 105.337 394.399 309.213 408.893 -94.702 443.069 -4.822
55+
4900 105.445 396.572 310.973 419.432 -95.156 454.277 -4.843
56+
5000 105.546 398.703 312.707 429.982 -95.641 465.495 -4.863
57+
5100 105.642 400.794 314.414 440.541 -96.157 476.722 -4.883
58+
5200 105.733 402.847 316.095 451.110 -96.703 487.961 -4.902
59+
5300 105.818 404.861 317.750 461.688 -97.278 499.210 -4.920
60+
5400 105.899 406.840 319.382 472.274 -97.882 510.470 -4.938
61+
5500 105.976 408.784 320.990 482.867 -98.513 521.741 -4.955
62+
5600 106.049 410.694 322.575 493.469 -99.170 533.025 -4.972
63+
5700 106.118 412.572 324.137 504.077 -99.852 544.320 -4.988
64+
5800 106.184 414.418 325.678 514.692 -100.557 555.628 -5.004
65+
5900 106.247 416.234 327.197 525.314 -101.284 566.946 -5.019
66+
6000 106.306 418.020 328.696 535.942 -102.032 578.279 -5.034

0 commit comments

Comments
 (0)