python之numpy的使用
前言numpy是Python的一种开源的数值计算扩展工具,这种工具可用来存储和处理大型矩阵使用安装pip install numpy基准示例list(列表)转换成array(数组)>>> import numpy as np>>> getAllData = [['retailer', 'price', 'weight', 'totalValue'], ['liy
·
前言
numpy是Python的一种开源的数值计算扩展工具,这种工具可用来存储和处理大型矩阵
使用
安装
pip install numpy
基准示例
- list(列表)转换成array(数组)
>>> import numpy as np
>>> getAllData = [['retailer', 'price', 'weight', 'totalValue'], ['liyi', 100.0, 100.0, 10000.0], ['daier', 99.0, 99.0, 9801.0], ['shangsan', 98.0, 98.0, 9604.0], ['qinsi', 97.0, 97.0, 9409.0], ['tangwu', 96.0, 96.0, 9216.0]]
>>> data = np.asarray(getAllData)
>>> data
array([['retailer', 'price', 'weight', 'totalValue'],
['liyi', '100.0', '100.0', '10000.0'],
['daier', '99.0', '99.0', '9801.0'],
['shangsan', '98.0', '98.0', '9604.0'],
['qinsi', '97.0', '97.0', '9409.0'],
['tangwu', '96.0', '96.0', '9216.0']], dtype='<U10')
>>> type(data)
<class 'numpy.ndarray'>
获取列表中第一行的值
>>> data[1, :]
array(['liyi', '100.0', '100.0', '10000.0'], dtype='<U10')
获取数组中第一列的值
>>> data[:, 0]
array(['retailer', 'liyi', 'daier', 'shangsan', 'qinsi', 'tangwu'],
dtype='<U10')
结语
详参Numpy
更多推荐
已为社区贡献5条内容
所有评论(0)