1.概述
numpy库是Python语言的一个扩充程序库。支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
2.操作
①对读取文件的操作
import numpy
//genfromtxt是一个对文件操作的一个函数
new_text = numpy.genfromtxt("C:\\Users\\Administrator\\Desktop\\new_text.txt",delimiter=",",dtype=str)
//打印出文件的存储类型
print(type(new_text))
print(new_text)
//help函数是为了查询这个genfromtxt有什么功能,以及有什么参数
print(help(numpy.genfromtxt)
运行结果:
<class 'numpy.ndarray'>
[['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value']
['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
['1986' 'Americas' 'Uruguay' 'Other' '0.5']
...
['1987' 'Africa' 'Malawi' 'Other' '0.75']
['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
Help on function genfromtxt in module numpy:
genfromtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding='bytes')
Load data from a text file, with missing values handled as specified.
Each line past the first `skip_header` lines is split at the `delimiter`
character, and characters following the `comments` character are discarded.
Parameters
----------
fname : file, str, pathlib.Path, list of str, generator
File, filename, list, or generator to read. If the filename
extension is `.gz` or `.bz2`, the file is first decompressed. Note
that generators must return byte strings in Python 3k. The strings
in a list or produced by a generator are treated as lines.
dtype : dtype, optional
Data type of the resulting array.
If None, the dtypes will be determined by the contents of each
column, individually.
②一维数组和二维数组的建立
vector=numpy.array([5,10,15,20])
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector)
print(matrix)
运行结果:[ 5 10 15 20]
[[ 5 10 15]
[20 25 30]
[35 40 45]]
##### ③
shape():一维元素查元素个数,二维数组表示几行几列
vector=numpy.array([5,10,15,20])
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector.shape)
print(matrix.shape)
运行结果:(4,)
(3, 3)
//一个矩阵或者数组里面的元素必须是同种类型,如果有一个优先级高的,所有的元素会向优先级转换
import numpy
numbers=numpy.array([1,2,3,'4'])
print(numbers)
numbers.dtype
运行结果:['1' '2' '3' '4']
⑤数组和矩阵的切片操作
vector=numpy.array([5,10,15,20])
//左闭右开,输出第0个元素到第二个元素
print(vector[0:3])
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
//表示第一列的所有元素
print(matrix[:,1])
//表示第0列到第1列的所有元素
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(matrix[:,0:2])
运行结果:
[ 5 10 15]
[10 25 40]
[[ 5 10]
[20 25]
[35 40]]
⑥判断字符的应用
vector=numpy.array([5,10,15,20])
vector==10
运行结果:array([False, True, False, False])
//bollean类型也可以当索引来用
vector=numpy.array([5,10,15,20])
equal_to_ten=(vector==10)
print (equal_to_ten)
print(vector[equal_to_ten])
运行结果:
[False True False False]
[10]
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
second_column_25=(matrix[:,1]==25)
print (second_column_25)
print(matrix[second_column_25,:])
运行结果:
[False True False]
[[20 25 30]]
⑦矩阵的列求和和行求和
import numpy
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
//代表每一行的和
matrix.sum(axis=1)
运行结果:
array([ 30, 75, 120])
import numpy
matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
//代表每一列的和
matrix.sum(axis=0)
运行结果:
array([60, 75, 90])