import numpy
#delimiter分隔符,dtype資料格式
word_alcho = numpy.genfromtxt("d:\qiujiahao4.txt",delimiter=",",dtype="str")
#print (type(word_alcho))
print (word_alcho[0])
['1' '2' '3' '4' 'qiu']
print (word_alcho[0][1])
2
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15,20],[1,2,3,4]])
print (vector)
print (matrix)
print (word_alcho)
[ 5 10 15 20]
[[ 5 10 15 20]
[ 1 2 3 4]]
[['1' '2' '3' '4' 'qiu']
['1' '2' '3' '4' 'qiu']]
equal = (vector==10)&(vector==5) #陣列中既等於10又等於5的
print (equal)
equal = (vector==10)|(vector==5)
vector[equal]=1
#將陣列中等於10和5的值換成1
print (vector)
print(matrix)
equal = matrix[:,1]==10
#哪一行的第一列等於10
print (equal)
matrix[equal,1]=1
#將這一行的第一列替換成1
print(matrix)
[false false false false]
[ 1 1 15 20]
[[ 5 10 15 20]
[ 1 2 3 4]]
[ true false]
[[ 5 1 15 20]
[ 1 2 3 4]]
print (matrix.dtype)
print (vector[0:3])
#冒號代表所有行
print (matrix[:,1])
print (matrix[:,0:2])
int32
[ 1 1 15]
[1 2]
[[5 1]
[1 2]]
#u75代表utf型別
word_alcho = numpy.genfromtxt("d:\qiujiahao5.txt",delimiter=" ",dtype="u75",skip_header=1)
print (word_alcho)
[['qiu' 'man']
['jia' 'woman']]
vector = numpy.array(["1","2","3"])
print (vector.dtype)
print (vector)
vector = vector.astype(float) #將字串轉化成浮點型
print (vector.dtype)
print (vector)
print (vector.sum())
print (vector.mean()) #平均值
12,34,56,78],[11,22,33,44]])
sum = matrix.sum(axis=1) #指定維度,1代表行,0代表列
print(sum)
[180 110]
word_alcho = numpy.genfromtxt("d:\qiujiahao5.txt",delimiter=" ")
print (word_alcho)#nan代表無法識別的
index = numpy.isnan(word_alcho[:,1])#判斷是否是nan
word_alcho[index,0:2]=0
print (word_alcho)
print (word_alcho.sum())
print (word_alcho.mean())
[[ nan nan 123.]
[ nan nan 456.]]
[[ 0. 0. 123.]
[ 0. 0. 456.]]
579.0
96.5
Numpy矩陣基礎操作命令
1 arange reshape,arange 按照指定步長遞增生成 mat np.arange 0,18,2 mat mat.reshape 3,3 0 2 4 6 8 10 12 14 16 2 array reshape mat np.array 1,3,4,6 mat mat.reshape...
Numpy矩陣基礎
1 與運算 vector numpy.array 5,10,15,20 equal to ten and five vector 5 vector 10 print equal to ten and five false false false false 與運算比較嚴格 乙個數不可能即等於5又等於...
numpy基礎操作
猴急先導入包 import numpy as npvct row np.array 1 2,3 行向量 vct col np.array 4 5 6 列向量matrix np.array 1,2,3 4,5,6 7,8,9 mtx np.mat 1,2,3 4,5,6 7,8,9 from scip...