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])