python 数据处理 - numpy 模块
1.np.array()
Parameters
- object: array_like
- dtype: data-type, optional
- copy: bool, optional
- order{‘K’, ‘A’, ‘C’, ‘F’}, optional
# 示例1
data = np.array(data,dtype=np.float)
2.np.where()
numpy.``where
(condition[****, x**,** y**]****)**
Description:
Return elements chosen from x or y depending on condition.
Parameters:
Parameters
-
condition: array_like, bool
Where True, yield x, otherwise yield y.
-
x, y: array_like
Values from which to choose. x, y and condition need to be broadcastable to some shape.
# 示例1
data.loc[i, 'total_current'] = np.average(temp1,weights=temp2)
3.np.average
Parameters
-
a: array_like
-
axis: None or int or tuple of ints, optional
Axis or axes along which to average a.
-
weights: array_like, optional
-
returnedbool, optional
Default is False. If True, the tuple (
average
, sum_of_weights) is returned, otherwise only the average is returned. If weights=None, sum_of_weights is equivalent to the number of elements over which the average is taken.
# 示例1
data.loc[i, 'total_current'] = np.average(temp1,weights=temp2)
4. np.random.choice
import numpy as np
# 参数意思分别 是从a 中以概率P,随机选择3个, p没有指定的时候相当于是一致的分布
a1 = np.random.choice(a=5, size=3, replace=False, p=None)
print(a1)
# 非一致的分布,会以多少的概率提出来
a2 = np.random.choice(a=5, size=3, replace=False, p=[0.2, 0.1, 0.3, 0.4, 0.0])
print(a2)
# replacement 代表的意思是抽样之后还放不放回去,如果是False的话,那么出来的三个数都不一样,如果是