-
读取txt
import numpy world_lcohol=numpy.genfromtxt('world_alcohol.txt',delimiter=',',dtype=str) print(type(world_lcohol)) print(world_lcohol) print(help(numpy.genfromtxt))
运行结果:
-
numpy.array
vector=([5,10,15,20]) matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]]) print (vector) print (matrix)
运行代码:
-
**.shape
vector=numpy.array([1,2,3,4]) print(vector.shape) matrix=numpy.array([[5,10,15],[20,25,30]]) print(matrix.shape)
运行结果:
-
dtype
import numpy numbers=numpy.array([1,2,3,4.0]) print(numbers) numbers.dtype
运行结果:
-
跳过标题栏
world_lcohol=numpy.genfromtxt('world_alcohol.txt',delimiter=',',dtype=str,skip_header=1) print(world_lcohol)
运行结果:
-
取值
uruguay_other_1986=world_lcohol[1,4] third_country=world_lcohol[2,2] print(uruguay_other_1986) print(third_country)
-
切片
vector=numpy.array([5,10,15,20]) print(vector[0:3])
运行结果:
-
取一列
matrix=numpy.array([[5,10,15], [20,25,30], [35,40,45] ]) print(matrix[:,1])
运行结果:
-
两列
print(matrix[:,0:2])
运行结果:
-
指定行列
print(matrix[1:3,0:2])
运行结果:
-
查找
import numpy vector=numpy.array([5,10,15,20]) vector==10
运行结果:
-
数组查找
matrix=numpy.array=([[5,10,15], [20,25,30], [35,40,45]]) matrix==25
运行结果:
-
返回值
equal_to_ten=(vector==10) print (equal_to_ten) print(vector[equal_to_ten])
运行结果:
-
二维数组
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])
运行结果:
科学计算库Numpy
最新推荐文章于 2023-04-22 13:14:35 发布