import numpy as np #大写的是常量 dir(np) ['ALLOW_THREADS', 'BUFSIZE', 'CLIP', 'DataSource', 'ERR_CALL', 'ERR_DEFAULT', 'ERR_IGNORE', 'ERR_LOG', 'ERR_PRINT', 'ERR_RAISE', 'ERR_WARN', 'FLOATING_POINT_SUPPORT', 'FPE_DIVIDEBYZERO', 'FPE_INVALID', 'FPE_OVERFLOW', 'FPE_UNDERFLOW', 'False_', 'Inf', 'Infinity', 'MAXDIMS', 'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT', 'NAN', 'NINF', 'NZERO', 'NaN', 'PINF', 'PZERO', 'RAISE', 'RankWarning', 'SHIFT_DIVIDEBYZERO', 'SHIFT_INVALID', 'SHIFT_OVERFLOW', 'SHIFT_UNDERFLOW', 'ScalarType', 'True_', 'UFUNC_BUFSIZE_DEFAULT', 'UFUNC_PYVALS_NAME', 'WRAP', '_CopyMode', '_NoValue', '_UFUNC_API', '__NUMPY_SETUP__', '__all__', '__builtins__', '__cached__', '__config__', '__deprecated_attrs__', '__dir__', '__doc__', '__expired_functions__', '__file__', '__former_attrs__', '__future_scalars__', '__getattr__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_add_newdoc_ufunc', '_builtins', '_distributor_init', '_financial_names', '_get_promotion_state', '_globals', '_int_extended_msg', '_mat', '_no_nep50_warning', '_pyinstaller_hooks_dir', '_pytesttester', '_set_promotion_state', '_specific_msg', '_typing', '_using_numpy2_behavior', '_utils', 'abs', 'absolute', 'add', 'add_docstring', 'add_newdoc', 'add_newdoc_ufunc', 'all', 'allclose', 'alltrue', 'amax', 'amin', 'angle', 'any', 'append', 'apply_along_axis', 'apply_over_axes', 'arange', 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'argmax', 'argmin', 'argpartition', 'argsort', 'argwhere', 'around', 'array', 'array2string', 'array_equal', 'array_equiv', 'array_repr', 'array_split', 'array_str', 'asanyarray', 'asarray', 'asarray_chkfinite', 'ascontiguousarray', 'asfarray', 'asfortranarray', 'asmatrix', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'bartlett', 'base_repr', 'binary_repr', 'bincount', 'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman', 'block', 'bmat', 'bool_', 'broadcast', 'broadcast_arrays', 'broadcast_shapes', 'broadcast_to', 'busday_count', 'busday_offset', 'busdaycalendar', 'byte', 'byte_bounds', 'bytes_', 'c_', 'can_cast', 'cast', 'cbrt', 'cdouble', 'ceil', 'cfloat', 'char', 'character', 'chararray', 'choose', 'clip', 'clongdouble', 'clongfloat', 'column_stack', 'common_type', 'compare_chararrays', 'compat', 'complex128', 'complex256', 'complex64', 'complex_', 'complexfloating', 'compress', 'concatenate', 'conj', 'conjugate', 'convolve', 'copy', 'copysign', 'copyto', 'corrcoef', 'correlate', 'cos', 'cosh', 'count_nonzero', 'cov', 'cross', 'csingle', 'ctypeslib', 'cumprod', 'cumproduct', 'cumsum', 'datetime64', 'datetime_as_string', 'datetime_data', 'deg2rad', 'degrees', 'delete', 'deprecate', 'deprecate_with_doc', 'diag', 'diag_indices', 'diag_indices_from', 'diagflat', 'diagonal', 'diff', 'digitize', 'disp', 'divide', 'divmod', 'dot', 'double', 'dsplit', 'dstack', 'dtype', 'dtypes', 'e', 'ediff1d', 'einsum', 'einsum_path', 'emath', 'empty', 'empty_like', 'equal', 'errstate', 'euler_gamma', 'exceptions', 'exp', 'exp2', 'expand_dims', 'expm1', 'extract', 'eye', 'fabs', 'fastCopyAndTranspose', 'fft', 'fill_diagonal', 'find_common_type', 'finfo', 'fix', 'flatiter', 'flatnonzero', 'flexible', 'flip', 'fliplr', 'flipud', 'float128', 'float16', 'float32', 'float64', 'float_', 'float_power', 'floating', 'floor', 'floor_divide', 'fmax', 'fmin', 'fmod', 'format_float_positional', 'format_float_scientific', 'format_parser', 'frexp', 'from_dlpack', 'frombuffer', 'fromfile', 'fromfunction', 'fromiter', 'frompyfunc', 'fromregex', 'fromstring', 'full', 'full_like', 'gcd', 'generic', 'genfromtxt', 'geomspace', 'get_array_wrap', 'get_include', 'get_printoptions', 'getbufsize', 'geterr', 'geterrcall', 'geterrobj', 'gradient', 'greater', 'greater_equal', 'half', 'hamming', 'hanning', 'heaviside', 'histogram', 'histogram2d', 'histogram_bin_edges', 'histogramdd', 'hsplit', 'hstack', 'hypot', 'i0', 'identity', 'iinfo', 'imag', 'in1d', 'index_exp', 'indices', 'inexact', 'inf', 'info', 'infty', 'inner', 'insert', 'int16', 'int32', 'int64', 'int8', 'int_', 'intc', 'integer', 'interp', 'intersect1d', 'intp', 'invert', 'is_busday', 'isclose', 'iscomplex', 'iscomplexobj', 'isfinite', 'isfortran', 'isin', 'isinf', 'isnan', 'isnat', 'isneginf', 'isposinf', 'isreal', 'isrealobj', 'isscalar', 'issctype', 'issubclass_', 'issubdtype', 'issubsctype', 'iterable', 'ix_', 'kaiser', 'kernel_version', 'kron', 'lcm', 'ldexp', 'left_shift', 'less', 'less_equal', 'lexsort', 'lib', 'linalg', 'linspace', 'little_endian', 'load', 'loadtxt', 'log', 'log10', 'log1p', 'log2', 'logaddexp', 'logaddexp2', 'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'logspace', 'longcomplex', 'longdouble', 'longfloat', 'longlong', 'lookfor', 'ma', 'mask_indices', 'mat', 'matmul', 'matrix', 'max', 'maximum', 'maximum_sctype', 'may_share_memory', 'mean', 'median', 'memmap', 'meshgrid', 'mgrid', 'min', 'min_scalar_type', 'minimum', 'mintypecode', 'mod', 'modf', 'moveaxis', 'msort', 'multiply', 'nan', 'nan_to_num', 'nanargmax', 'nanargmin', 'nancumprod', 'nancumsum', 'nanmax', 'nanmean', 'nanmedian', 'nanmin', 'nanpercentile', 'nanprod', 'nanquantile', 'nanstd', 'nansum', 'nanvar', 'nbytes', 'ndarray', 'ndenumerate', 'ndim', 'ndindex', 'nditer', 'negative', 'nested_iters', 'newaxis', 'nextafter', 'nonzero', 'not_equal', 'numarray', 'number', 'obj2sctype', 'object_', 'ogrid', 'oldnumeric', 'ones', 'ones_like', 'outer', 'packbits', 'pad', 'partition', 'percentile', 'pi', 'piecewise', 'place', 'poly', 'poly1d', 'polyadd', 'polyder', 'polydiv', 'polyfit', 'polyint', 'polymul', 'polynomial', 'polysub', 'polyval', 'positive', 'power', 'printoptions', 'prod', 'product', 'promote_types', 'ptp', 'put', 'put_along_axis', 'putmask', 'quantile', 'r_', 'rad2deg', 'radians', 'random', 'ravel', 'ravel_multi_index', 'real', 'real_if_close', 'rec', 'recarray', 'recfromcsv', 'recfromtxt', 'reciprocal', 'record', 'remainder', 'repeat', 'require', 'reshape', 'resize', 'result_type', 'right_shift', 'rint', 'roll', 'rollaxis', 'roots', 'rot90', 'round', 'round_', 'row_stack', 's_', 'safe_eval', 'save', 'savetxt', 'savez', 'savez_compressed', 'sctype2char', 'sctypeDict', 'sctypes', 'searchsorted', 'select', 'set_numeric_ops', 'set_printoptions', 'set_string_function', 'setbufsize', 'setdiff1d', 'seterr', 'seterrcall', 'seterrobj', 'setxor1d', 'shape', 'shares_memory', 'short', 'show_config', 'show_runtime', 'sign', 'signbit', 'signedinteger', 'sin', 'sinc', 'single', 'singlecomplex', 'sinh', 'size', 'sometrue', 'sort', 'sort_complex', 'source', 'spacing', 'split', 'sqrt', 'square', 'squeeze', 'stack', 'std', 'str_', 'string_', 'subtract', 'sum', 'swapaxes', 'take', 'take_along_axis', 'tan', 'tanh', 'tensordot', 'test', 'testing', 'tile', 'timedelta64', 'trace', 'tracemalloc_domain', 'transpose', 'trapz', 'tri', 'tril', 'tril_indices', 'tril_indices_from', 'trim_zeros', 'triu', 'triu_indices', 'triu_indices_from', 'true_divide', 'trunc', 'typecodes', 'typename', 'ubyte', 'ufunc', 'uint', 'uint16', 'uint32', 'uint64', 'uint8', 'uintc', 'uintp', 'ulonglong', 'unicode_', 'union1d', 'unique', 'unpackbits', 'unravel_index', 'unsignedinteger', 'unwrap', 'ushort', 'vander', 'var', 'vdot', 'vectorize', 'version', 'void', 'vsplit', 'vstack', 'where', 'who', 'zeros', 'zeros_like'] len(dir(np)) 594 |
'abs', 'absolute', 'add', 'add_docstring', 'add_newdoc', 'add_newdoc_ufunc', 'all', 'allclose', 'alltrue', 'amax', 'amin', 'angle', 'any', 'append', 'apply_along_axis', 'apply_over_axes', 'arange', 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'argmax', 'argmin', 'argpartition', 'argsort', 'argwhere', 'around', 'array', 'array2string', 'array_equal', 'array_equiv', 'array_repr', 'array_split', 'array_str', 'asanyarray', 'asarray', 'asarray_chkfinite', 'ascontiguousarray', 'asfarray', 'asfortranarray', 'asmatrix', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', |
'bartlett', 'base_repr', 'binary_repr', 'bincount', 'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman', 'block', 'bmat', 'bool_', 'broadcast', 'broadcast_arrays', 'broadcast_shapes', 'broadcast_to', 'busday_count', 'busday_offset', 'busdaycalendar', 'byte', 'byte_bounds', 'bytes_', |
|
|
import numpy as np print(np.ones(3)) # array([1., 1., 1.]) print(np.ones([3]))# array([1., 1., 1.]) print(np.zeros(3)) # array([0., 0., 0.]) print(np.empty([3,5])) # empty的值没有意义,但速度最快,连初始化都不需要 print(np.eye(N=3)) # 单位矩阵E """ array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) """ # 任何矩阵与单位矩阵相乘后还是本身 print(np.linspace(1,15,15).reshape(3,5)@np.eye(5)) """ array([[ 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10.], [11., 12., 13., 14., 15.]]) """
np.unique
from sklearn.datasets import make_classification X,y = make_classification(n_samples=10000,n_features=2000,n_classes=2,) import numpy as np unique,count=np.unique(y,return_counts=True) data_count=dict(zip(unique,count)) print(data_count) {0: 4999, 1: 5001}
python collections.Counter
import collections data_count2=collections.Counter(y) data_count2 Counter({0: 4999, 1: 5001})
python list count
a = [1,2,2,3,3,3] b = set(a) for v in b: print(v,":",a.count(v)) 1 : 1 2 : 2 3 : 3
import numpy as np np.allclose(1e-8,0)
python有range生成一系列的数,numpy有arange,开头的a指np.array
arange([start,] stop[, step,], dtype=None, *, like=None) import numpy as np print(range(3)) # range(0, 3) print(np.array([0,1,2])) # array([0, 1, 2]) print(np.array(range(3))) # array([0, 1, 2]) print(np.arange(3)) # array([0, 1, 2])
#一个参数 默认起点0,步长为1 输出:[0 1 2] a = np.arange(3) #两个参数 默认步长为1 输出[3 4 5 6 7 8] a = np.arange(3, 9) #三个参数 起点为0,终点为3,步长为0.1 输出[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9] a = np.arange(0, 3, 0.1)
指定范围内取N个数,线性分布
## linspace 从1开始,到100结束(包含100),取10个浮点数 - 近似于等差数列 a1 = np.linspace(1, 100, 10) print(type(a1)) print(len(a1)) print(a1) class 'numpy.ndarray' 10 [ 1. 12. 23. 34. 45. 56. 67. 78. 89. 100.] - 不指定数量时,默认生成50个 a1 = np.linspace(1,2) print(type(a1)) print(len(a1)) print(a1) class 'numpy.ndarray' 50 [1. 1.02040816 1.04081633 1.06122449 1.08163265 1.10204082 1.12244898 1.14285714 1.16326531 1.18367347 1.20408163 1.2244898 1.24489796 1.26530612 1.28571429 1.30612245 1.32653061 1.34693878 1.36734694 1.3877551 1.40816327 1.42857143 1.44897959 1.46938776 1.48979592 1.51020408 1.53061224 1.55102041 1.57142857 1.59183673 1.6122449 1.63265306 1.65306122 1.67346939 1.69387755 1.71428571 1.73469388 1.75510204 1.7755102 1.79591837 1.81632653 1.83673469 1.85714286 1.87755102 1.89795918 1.91836735 1.93877551 1.95918367 1.97959184 2. ] import numpy as np ar1 = np.linspace(0.001,0.999,10) print(ar1) [0.001 0.11188889 0.22277778 0.33366667 0.44455556 0.55544444 0.66633333 0.77722222 0.88811111 0.999 ]
指定范围内取N个数,均匀分布,随机
uniform(low=0.0, high=1.0, size=None) [low, high) (includes low, but excludes high) 从半开区间,均匀分布的数据中,随机取出size个数据 np.random.uniform(low=-1,high=1,size=10) array([-0.60967645, -0.40023626, 0.83646232, 0.93581929, 0.50901503, -0.86735182, 0.64710941, 0.47334668, 0.77855487, -0.86533817])
round(n):保留n位小数
import numpy as np a=np.array([1.001,1.123]) a=a.mean() # 1.0619999999999998 numpy.float64 a.round(3) # 1.062
np.set_printoptions(precision=None, threshold=None, linewidth=None, suppress=None, formatter=None) np.set_printoptions() 用于控制Python中小数的显示精度。 np.set_printoptions(precision=None, threshold=None, linewidth=None, suppress=None, formatter=None) 1.precision:控制输出结果的精度(即小数点后的位数),默认值为8 2.threshold:当数组元素总数过大时,设置显示的数字位数,其余用省略号代替(当数组元素总数大于设置值,控制输出值得个数为6个,当数组元素小于或者等于设置值得时候,全部显示),当设置值为sys.maxsize(需要导入sys库),则会输出所有元素 3.linewidth:每行字符的数目,其余的数值会换到下一行 4.suppress:小数是否需要以科学计数法的形式输出 5.formatter:自定义输出规则 |
import numpy as np np.set_printoptions(precision=4) val=[1.2345678] np.array(val) array([1.2346]) 注意四位有效小数,最后一位是6,6进5+1,相当于四舍五入了 |
import numpy as np np.set_printoptions(precision=5,suppress=False) val=[1.2345678] np.array(val) array([1.23457]) |
threshold默认不低于3 import numpy as np np.set_printoptions(precision=3,suppress=False,threshold=2) val1=[1,2,3,4,5,6,7,8,9,0] np.array(val1) array([1, 2, 3, ..., 8, 9, 0]) np.random.randn(32,12) array([[-0.4794, 1.7589, 0.3096, ..., 1.889 , -0.1393, -0.7241], [-0.0544, -0.484 , -1.5246, ..., -0.8761, 1.2215, 1.8836], [ 0.8409, -1.1877, -1.886 , ..., -0.14 , -0.7232, -1.0742], ..., [ 1.3005, -1.4814, 1.0046, ..., 1.7119, 0.3857, -1.099 ], [ 1.1772, -1.6911, 0.5059, ..., 0.4867, -0.1444, 0.3945], [-0.1769, -1.4905, 0.025 , ..., 0.267 , 0.2176, -1.0928]]) 如果不设置threshold import numpy as np # np.set_printoptions(precision=4,suppress=False,threshold=2) val1=[1,2,3,4,5,6,7,8,9,0] np.array(val1) array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) np.random.randn(32,12) array([[ 1.31096198, 0.78054052, -0.40963379, 0.23273274, -0.10150605, 1.88460194, 0.35433371, 0.58183019, -0.57228141, -0.77110854, 1.03263305, -0.89838403], [ 0.84391413, 0.78073747, 1.04194755, -0.44456158, 2.12592094, -0.75856683, 0.24688149, -1.25829492, 0.77219029, -1.85642349, 1.01285344, 2.06686249], [-0.47637921, -0.21378507, 1.47798267, 0.40207861, -0.9119715 , -0.33286264, 0.12025972, 0.97894507, -0.73496818, -0.23886736, 0.25050232, 0.50630583], [ 1.15780458, -0.08534266, 0.28685981, -0.30310841, -1.14923578, 0.45098689, -0.801712 , 0.73749301, 0.95536514, -1.3927399 , -0.56234157, -0.11920498], [ 0.77520323, 0.20221095, 0.77120729, -0.0667725 , -0.15135993, 0.2243866 , -1.71608205, 2.16473421, -1.20444339, -0.61015631, 0.22587885, -0.71552811], [-1.44029834, 0.8743934 , 1.2970308 , 0.92969892, 0.82170051, -1.82683443, -0.97449604, -0.56134529, -1.1953847 , 0.09939801, 0.04745533, -0.33863088], [-1.98972731, -0.55352395, -0.03600633, 0.88468785, -0.67922446, -0.28280658, -0.21982757, 0.81073329, -0.437203 , 0.01027904, 0.38735405, 1.18708735], [ 0.8603094 , -0.08396988, -0.73883093, -0.79855211, 1.00562191, 0.31154704, 0.03984569, 0.91169203, -2.22721951, -0.67505607, 0.12693552, 1.02194256], [-0.21013117, 0.19916637, 0.29599217, 1.18469863, -0.51609669, -0.80919176, -0.18930233, -0.57996685, -0.75708229, 0.57157711, 0.98432768, 1.75348857], [ 0.41805862, 0.05898554, -1.58387403, -0.21579417, -1.22139546, 0.91596961, -0.81760997, 1.46059549, -0.1663579 , 0.18273167, -0.90985894, 0.76214167], [ 0.76408739, -0.79867742, 1.61184349, -0.50377978, -0.55804087, 0.57939787, 0.87574714, 1.29942416, -1.97320811, -0.49030193, 0.53141084, 0.35189542], [-0.37105578, 0.10168585, -0.16420312, 0.96103647, -0.59469692, -0.49589185, 1.58945027, 0.92076437, 0.92450181, -1.21256344, -2.03462813, 1.08089276], [-0.93407269, -0.47150967, -0.87131997, -1.91801268, -1.14335032, 0.98411407, -0.35696701, 0.20963831, 1.81055827, -0.38785915, -0.25418608, 2.07452367], [-0.49608547, 0.33450767, -1.3014056 , -1.52946272, 1.32558334, 1.41568975, 1.41339455, 0.80110783, 0.4616043 , 0.64017306, -0.31558792, 1.78494037], [ 0.67592165, 0.90067825, 0.07187182, -1.20817453, 0.50172272, 1.18440157, 0.80802945, -0.70223411, 0.15666584, 0.67119167, 0.25968969, 0.08471809], [ 1.55923875, -2.03317548, -1.62797668, 0.32402003, 0.70732674, -0.44145688, -0.26678467, 0.60273043, -0.14171507, 1.47339477, 1.29916823, 0.41381557], [ 0.67091644, -1.13449214, 0.88084109, -0.86644771, 0.9574995 , -0.49179203, 0.59506458, 1.68567831, 1.45062971, 1.12630867, -0.49457516, 0.09114502], [ 0.3225183 , 0.47806111, -2.36724118, 0.93357899, -0.89987033, -0.06345555, -1.83587324, 1.12663143, -1.47788753, 0.7524638 , 1.07597296, 0.45719949], [-2.06272979, 0.77196334, 0.09220272, 0.28972571, 0.15619067, -1.07744337, 0.41897236, 0.29222976, 2.94242355, -0.61419158, -0.19096359, -1.36344336], [ 0.97305062, 1.10512088, -1.1480367 , 0.71308534, 0.33248084, -0.9166076 , -0.29528799, -1.42392493, -0.04919407, -0.01888874, 0.08178188, -0.28983312], [ 1.22496706, -0.47912479, 0.44151834, -1.01027493, 1.05074605, -1.73803353, -2.9378151 , -1.03139439, -0.91806935, -1.58093067, -0.12704403, 0.64359906], [ 0.18644383, 0.35878495, 0.46146176, -1.75039558, 1.15147572, -0.79001927, 0.06842818, -0.92784629, 0.53966766, -1.15624849, -0.56785447, -0.09107513], [ 1.64521859, -0.35208162, -0.24719867, 0.73485631, -0.7193255 , 0.18748171, 0.40802318, 0.15651978, -1.7070667 , -0.19710219, -0.52689884, 0.12777919], [-0.00589913, 0.11645322, -0.69165855, -1.3728932 , -1.72926142, 0.0853667 , 0.078379 , -0.21203821, -0.33286296, 2.36753064, -1.12380217, -0.03725729], [ 1.63224637, 0.8916754 , 1.02162072, -0.77177397, 0.10909745, 0.93361119, 0.49809446, -0.36998432, -0.15940681, -1.2318212 , 0.54767701, -1.03294341], [-0.71157578, -0.40025839, 0.18814183, 1.61617908, 0.67473142, 0.76452083, -1.45217186, 0.73087727, 0.5605336 , 0.77584341, -2.04797342, 0.72173009], [-0.75710551, -0.48514138, 1.18142324, 1.45099828, 0.61900149, -1.50008651, 0.01960995, -0.25247218, 0.16883396, 0.36491727, -0.19654314, -0.1775503 ], [-0.05020209, 0.10460235, 0.40680731, 1.37479371, -0.29574042, -0.01259258, -1.99811303, 1.91410299, -0.23692018, 0.11205839, 0.59292661, 0.44417237], [-0.02894275, -0.65325663, 0.67701216, 0.27121676, 0.14872388, -0.58924875, -0.54020538, -1.21573666, -0.61309256, -0.23269741, 0.04588593, 0.56945742], [ 0.28749359, 0.56990275, 1.37091707, -1.08851728, -0.13620002, -0.40595672, -0.1140739 , 0.41187812, -1.11993698, 0.68289479, 0.42379275, -0.64094676], [-1.60017974, -0.84723902, -0.02600453, -0.38100938, 1.34531363, -1.00090349, -2.28562568, 0.13931103, -0.25101233, 1.36768321, -0.40857943, -0.04582211], [ 0.7461974 , 0.11564297, 0.20692693, 0.15816297, -1.29618134, 0.05000082, 0.79307865, -1.80586943, 0.52601396, 0.74423465, 0.30464028, -0.59836129]]) |
|
数组排序
import numpy as np np.random.seed(73) a=np.random.randint(low=1,high=10,size=5) print(a) # [7 3 1 9 4] a.sort() print(a) # [1 3 4 7 9]
argsort索引排序
import numpy as np a=np.arange(start=2,stop=-1,step=-1) print(a) # array([2, 1, 0]) # 索引排序,默认按值的升序排列 print(a.argsort()) # array([2, 1, 0]) print(a[[2, 1, 0]]) # array([0, 1, 2])
aa=np.array([2,1,3]) aa[::-1] array([3, 1, 2]) aa[aa.argsort()] array([1, 2, 3]) aa[aa.argsort()][::-1] array([3, 2, 1])
- numpy.concatenate((a1,a2,...), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,...是数组类型的参数 - axis=0行方向上拼接 - axis=1列方向上拼接 - concatenate()效率更高,适合大规模的数据拼接
### a = np.unique(A) - 对于一维数组或者列表,unique函数去除其中重复的元素,并按元素由小到大返回一个新的无元素重复的元组或者列表 import numpy as np A = [1, 2, 2, 5, 3, 4, 3] a = np.unique(A) B= (1, 2, 2, 5, 3, 4, 3) b= np.unique(B) C= ['fgfh','asd','fgfh','asdfds','wrh'] c= np.unique(C) print(a) print(b) print(c) [1 2 3 4 5] [1 2 3 4 5] ['asd' 'asdfds' 'fgfh' 'wrh'] ### return_index - return_index=True - 表示返回新列表元素在旧列表中首次出现的位置,并以列表形式储存在s中。 import numpy as np A = [1, 2, 2, 5, 3, 4, 3] a, s= np.unique(A, return_index=True) print(a) print(s) [1 2 3 4 5] [0 1 4 5 3] ### return_inverse - a, s,p = np.unique(A, return_index=True, return_inverse=True) - a 取出了不同的类别,并做了排序 - s 是索引列表,其索引元素是A的,只不过是首次出现 - return_inverse=True 返回与旧列表元素个数相同的一个索引列表,其元素为a中元素的索引 - 从A到p完成了索引编码 - 本质上取出原标签的所有类别,并从0,1,2,...,n进行编码 a, s,p = np.unique(A, return_index=True, return_inverse=True) print(a) print(s) print(p) - a: 从小到大排列的不重复的元素列表 - s: a列表中的元素,首次在原始列表A中出现的索引位置 - p: 索引编码,原始列表的中元素在不重复列表a中的索引形成的列表 [1 2 3 4 5] [0 1 4 5 3] [0 1 1 4 2 3 2]
返回符合条件的索引
import numpy as np a = np.array([0,1,2,3]) #一维矩阵 b = np.where(a > 1) print(b) # (array([2, 3]),)
真假条件转换
import numpy as np a = np.linspace(start=-1,stop=1,num=10) """ array([-1. , -0.77777778, -0.55555556, -0.33333333, -0.11111111, 0.11111111, 0.33333333, 0.55555556, 0.77777778, 1. ]) """ b = np.where(a > 0, 1, 0) # [0 0 0 0 0 1 1 1 1 1] print(b)
reshape
### reshape - 将原数组中的所有元素串起来,按新的维度进行组合 ```python import numpy as np a = np.array( [ [ [[1],[255]], [[0],[100] ], ] ]) print("1",a) print("2",a.shape) b = a.reshape(2,2) print("3",b) ``` ``` 1 [[[[ 1] [255]] [[ 0] [100]]]] 2 (1, 2, 2, 1) 3 [[ 1 255] [ 0 100]] ```
reshape(-1, 1)
### reshape(-1, 1) - 这里的-1被理解为unspecified value,意思是未指定,未给定的。 - 为-1时,该维度由程序自动决定 - 即将矩阵的shape转换为n行一列 ```python x = np.array(range(1, 10)).reshape(-1, 1) print(x) ``` ``` [[1] [2] [3] [4] [5] [6] [7] [8] [9]] ``` ```python x = np.array(range(2, 10)).reshape(2, -1) print(x) ``` ``` [[2 3 4 5] [6 7 8 9]] ``` ```python # 原始数据的标签,是1维数据,每个样本向量对应一个标量 # 将标签变成列向量 y_train = y_train.reshape((-1,1)) y_test = y_test.reshape((-1,1)) ```
X.reshape(-1)
X.reshape(-1) 将n维矩阵转1维矩阵,即向量
np.newaxis
将np.newaxis放在哪一维表示在哪一维上增加一个维度 import numpy as np arr=np.random.randn(2,3) print(arr.shape) # (2, 3) a=arr[np.newaxis,:] print(a.shape) # (1, 2, 3) a=arr[:,np.newaxis] print(a.shape) # (2, 1, 3) a=arr[:,:,np.newaxis] print(a.shape) # (2, 3, 1)
reshape
原来的shape为(n1,n2,n3) 新加一个维度,reshape(n1,1,n2,n3) 这个1写在哪个位置就表示新增维度放在哪里
- 矩阵转置,按相同的顺序交换两个序列数据的维度 - 参数为原数组shape的索引下标 import numpy as np a = np.arange(15).reshape(3,5) print(a) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] import numpy as np a = np.arange(15).reshape(3,5) print(a.transpose(1, 0)) [[ 0 5 10] [ 1 6 11] [ 2 7 12] [ 3 8 13] [ 4 9 14]]
import numpy as np print(np.log(np.e)) # 1.0 print(np.log2(8)) # 3.0 # 感受一下对数与十进制的变化速度 import numpy as np a=np.array([1,10,100,1000,10000,100000,1000000,10000000]) # 个,十,百,千,万,十万,百万,千万 print(np.log(a).astype(np.float32)) """ array([ 0. , 2.3025851, 4.6051702, 6.9077554, 9.2103405, 11.512925 , 13.815511 , 16.118095 ], dtype=float32) """ print(np.log2(a).astype(np.float32)) """ array([ 0. , 3.321928, 6.643856, 9.965784, 13.287712, 16.60964 , 19.931568, 23.253496], dtype=float32) """
numpy数组拼接方法介绍(concatenate)