###################################################################
‘‘‘
当遇到不会的函数时,善用帮助文档。一般来说帮助文档中会有例子,方便学习,可以直接套用例子。
!!!重要!!! !!!重要!!! !!!重要!!!
print(help(numpy.genfromtxt))#官方帮助文档,多查询文档善用!!
‘‘‘
一些数据读取操作代码
import numpy
?
###################################################################
#读取txt文件。。。。numpy很少用来读取数据,,一般使用pandas!!!
print(help(numpy.genfromtxt))#官方帮助文档,多查询文档善用!!
?
world_txt=numpy.genfromtxt(""test.txt,delimiter=".",dtype=str)#读出的数据i是从0开始,区别matlab
file0_1=world_txt[0,1]#第0行第一列
file02_1=world_txt[0:2,1]#第1列第0-2行的数据,联系matlab
file02_1=world_txt[0:2,0-3]#第0-3列第0-2行的数据,联系matlab
?
#运行结果暂略.....
?
?
###################################################################
#数据结构-数组、向量,存的元素性质要相同数字或者字符
vector = numpy.array([10, 18, 10, 90])#input a list ,创建一个行向量
matrix = numpy.matrix([[1, 2, 32], [3, 4, 23]]) #创建一个矩阵
print (vector)
print (matrix)
?
#运行结果
‘‘‘
[10 18 10 90]
[[ 1 2 32]
[ 3 4 23]]
‘‘‘
?
###################################################################
#文件的属性 _.shape
vector = numpy.array([10, 18, 10, 90])
print (vector.shape)#数据中元素的个数
?
matrix = numpy.matrix([[1, 2, 32], [3, 4, 23]])
print (matrix.shape)#矩阵有几行几列
?
#运行结果
‘‘‘
(4,)
(2, 3)
‘‘‘
?
?
import numpy
?
###################################################################
#逻辑判断
vector = numpy.array([10, 18, 10, 90])
indexs = (vector == 10)#创建一个索引值
num = vector[indexs]#1.注意逻辑判断是两个等号“==”,2.它是对每个数组的每个数要做一次判断
print (indexs)
print (num)
?
‘‘‘
[ True False True False]
[10 10]
‘‘‘
?
###################################################################
#逻辑关系 与或关系 "& |"
vector = numpy.array([10, 18, 10, 90])
judges = (vector == 10) & (vector == 5)
print (judges)
?
#输出
‘‘‘
[False False False False]
‘‘‘
judge2 = (vector == 10) | (vector == 5)
print (judge2)
?
#输出
‘‘‘
[ True False True False]
‘‘‘
?
?
###################################################################
#类型转换
vector = numpy.array([‘10‘, ‘18‘, ‘10‘, ‘90‘])
print (vector.dtype)
print (vector)
?
vector = vector.astype(float)
print (vector.dtype)
print (vector)
?
#输出
‘‘‘
<U2
[‘10‘ ‘18‘ ‘10‘ ‘90‘]
float64
[10. 18. 10. 90.]
‘‘‘
?
?
###################################################################
#求最值
vector = numpy.array([10, 18, 10, 90])
print (vector)
print (["min"] + [vector.min()])
print (["max"] + [vector.max()])
?
#输出
‘‘‘[10 18 10 90]
[‘min‘, 10]
[‘max‘, 90]
‘‘‘
?
?
###################################################################
#按行按列求和
matrix = numpy.array([[10, 18, 10, 90], [34, 45, 12, 21], [12, 21, 45, 78]])
print (matrix)
sumhang = matrix.sum(axis=1)
sumlie = matrix.sum(axis=0)
print (["各行的和"] + [sumhang])
print (["各列的和"] + [sumlie])
?
#输出
‘‘‘