Python 科学工具笔记
numpy
- a = numpy.array([1,2,3,4]);// 创建一个numpy的数组对象
此时a.shape显示的值为(4,);
由此得出结论在一维的数组中, 数组的是列优先的
- numpy.random.uniform(low, high):
产生在low和high之间的随机数
- numpy.vdot(arrA, arrB):
计算arrA与arrB的数量积
- numpy.max(), .min(), .sum(), .average()
- numpy.random.randn():
Scipy
- scipy.integrate.quad(funcname, low, high): funcname函数的仅仅返回一个需要求解积分的式子
low: 积分下线
high: 积分上限
matplotlib.pyplot
import matplotlib.pyplot as plt
plt.hist()
plt.plot()
plt.pie()
plt.bar()
plt.show()
plt.scatter()
numpy库使用
- genfromtxt(filename, delimiter, dtype, skip_header=1/2)
filename: 文件名
delimiter: 分隔符, 用于分隔文件中每行的内容放入到矩阵中
dtype: 矩阵中的类型, ATTENTION: numpy矩阵中的所有元素是同一个类型
skip_header: 是否跳过首行
- numpy.array(list): 根据list返回一个numpy的矩阵
- obj.shape: 返回维度信息
- obj.dtype: 返回numpy的dtype类型对象, 显示矩阵中的元素类型
- [2], [2:3]: 对以为矩阵, 获取指定的元素
对于二维矩阵元素的获取:
[2:3, 3:4]: 逗号左侧表示对行的切片, 逗号右边表示对列的切片
pandas库使用(pandas是对numpy的封装, 随意可以混合使用)
- pandas.read_csv(filename): 读取文件返回DataFrame对象(df), 只要数据是以逗号分隔的都可使用read_csv读取
- df.dtypes返回类型
- df.head(3): 查看前3行
- df.tail(3): 查看后3行
- df.columns: 列名
- df.shape: 维度信息, 表格的行和列信息
- df.loc[index]: 返回指定行的信息
- df['string']: 返回指定的名称的列信息, 返回Series, DataFrame就是有很多个Series中组成的
- 在pandas中的python内置的str类型为object类型(dtype中显示的)
- obj.dropna(): 去掉有空项的行
- Series对象
series.index返回index序列
series.sortindex排序index
series.sortvalues排序values
series[0]|series['str']: 返回索引对应的value
matplotlib.pyplot库使用
import matplotlib.pyplot as plt
x_values = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
y_values = [100, 200, 300, 120, 12, 213, 123]
x_label = 'Days'
y_label = 'Rain flow'
line_color = 'red'
legend_string = 'Trend'
plt.plot(x_label, y_label, c=line_color, label=legend_string) # plot函数作用: 画出图或者线(指的仅仅是折线, 柱形图的, 并不包含坐标的绘画)
plot函数参数介绍:
1: x轴数据序列
2: y轴数据序列
3: c: 折线的颜色
4: label: 折线对应的名称
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title('Demo')
plt.ledend(loc='best') # 显示折线对应的label, 就是图例
plt.xticks(rotation=45) # 设置x轴显示的数据的倾斜度为45, 便于显示标签
plt.show() # 显示图形