import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str)#源地址、分隔符、数据类型
print(type(world_alcohol))
numbers = numpy.array([1, 2, 3, 4])
numbers.dtype#dtype('int32')
numbers = numpy.array([1, 2, 3, '4'])
numbers.dtype#dtype('<U11')元素类型不同,会发生类型转换
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)#忽略的行数
print(world_alcohol)
vector = numpy.array([5, 10, 15, 20])
print(vector[0:3]) #切片操作ok注意切片右侧取不到
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,1])#[10 25 40]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,0:2])
'''
[[ 5 10]
[20 25]
[35 40]]
'''
#Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print equal_to_ten#结果是[False True False False]
print(vector[equal_to_ten])#equal_to_ten当成了索引传入vector
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25)#[False True False]
print(matrix[second_column_25])#[20 25 30]]
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print equal_to_ten_or_five#[ True True False False]
#We can convert the data type of an array with the ndarray.astype() method.改变类型
vector = numpy.array(["1", "2", "3"])
print (vector.dtype)#<U1
print (vector)#['1' '2' '3']
vector = vector.astype(float)
print (vector.dtype)#float64
print (vector)#[1. 2. 3.]
vector = numpy.array([5, 10, 15, 20])
vector.sum()#求和
numpy.sum(vector)#求和
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=1) #array([ 30, 75, 120])按行求和
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(numpy.sum(matrix))#225, 求和函数
matrix.sum(axis=0)#array([60, 75, 90])按列求和
import numpy as np
a = np.arange(15).reshape(3, 5)#搞15个数据,弄成三行5列
a
#array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
#[10, 11, 12, 13, 14]])
import numpy as np
np.zeros ((3,4)) #注意括号的个数,少些就报错
#array([[ 0., 0., 0., 0.],
# [ 0., 0., 0., 0.],
#[ 0., 0., 0., 0.]])
np.ones( (2,3,4), dtype=np.int32 )#生成三维矩阵
'''
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
'''
#To create sequences of numbers
np.arange( 10, 30, 5 )#array([10, 15, 20, 25])arrange也可有步长
np.random.random((2,3))#array([[0.37689333, 0.28095466, 0.12736144],[0.23358768, 0.89663512, 0.18455733]]) 随机数0-1之间的
from numpy import pi
np.linspace( 0, 2*pi, 100 )#生成100个数,还是平均取得
np.zeros ((3,4)) #全0矩阵,注意参数(3,4)是元祖
'''array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
'''
np.ones( (2,3,4), dtype=np.int32 )#全1矩阵
#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )
# print(A)
# print(B)
print(A*B) #矩阵点乘,需要行列一样
'''
[[2 0]
[0 4]]
'''
print(A.dot(B))#矩阵的×乘,需要左边矩阵行乘矩阵右侧列,
print(np.dot(A, B) )#同上
'''
[[5 4]
[3 4]]
'''
#常用函数与操作
import numpy as np
B = np.arange(3)
print(B)#[0 1 2]
print(np.exp(B))#np. exp(x) e的x幂次方 [1. 2.71828183 7.3890561 ]
print (np.sqrt(B))#[0. 1. 1.41421356] 开方
a = np.floor(10*np.random.random((3,4)))#生成三行四列随机数
'''
[[9. 1. 5. 1.]
[1. 3. 4. 2.]
[1. 2. 4. 8.]]
'''
## flatten the array#拉平数组
print(a.ravel())#[9. 1. 5. 1. 1. 3. 4. 2. 1. 2. 4. 8.]
#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
a.reshape(3,-1)#-1默认计算列数
'''
array([[7., 0., 4., 4.],
[6., 3., 8., 0.],
[4., 4., 9., 1.]])
'''
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print a
print '---'
print b
print '---'
print np.hstack((a,b)) #拼接矩阵,横向
print np.vstack((a,b)) #纵向拼接矩阵
'''
[[ 5. 6.]
[ 1. 5.]]
---
[[ 8. 6.]
[ 9. 0.]]
---
[[ 5. 6. 8. 6.]
[ 1. 5. 9. 0.]]
[[ 5. 6.]
[ 1. 5.]
[ 8. 6.]
[ 9. 0.]]
'''
#切开矩阵,横向和纵向切割矩阵
a = np.floor(10*np.random.random((2,12)))
print(a)
print (np.hsplit(a,3))#把矩阵a切成三份,注意h,是横向切
'''
[[6. 4. 9. 1. 4. 4. 2. 1. 1. 4. 2. 7.]
[8. 9. 1. 6. 1. 0. 2. 4. 8. 2. 3. 1.]]
[array([[6., 4., 9., 1.],
[8., 9., 1., 6.]]), array([[4., 4., 2., 1.],
[1., 0., 2., 4.]]), array([[1., 4., 2., 7.],
[8., 2., 3., 1.]])]
'''
#指定切割的“刀”位,如,传入元祖(3,4)表示在第三列后切一刀,在第4列后切一刀
print (np.hsplit(a,(3,4))) # Split a after the third and the fourth column
'''
[array([[6., 4., 9.],
[8., 9., 1.]]), array([[1.],
[6.]]), array([[4., 4., 2., 1., 1., 4., 2., 7.],
[1., 0., 2., 4., 8., 2., 3., 1.]])]
'''
numpy库常用操作
于 2022-02-23 18:34:04 首次发布