Author:Tonny
转载请注明出处
详细请参考 读取文件
# 数据加载
data = np.loadtxt('load/feature.txt', dtype=np.int32, delimiter=',')
np.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes'
# 数据保存
np.savetxt('save/feature.txt', data, fmt='%d', delimiter=',')
np.savetxt(fname, X, fmt='%.18e', delimiter=',', newline='\n', header='', footer='', comments='# ', encoding=None)
vector = numpy.array([1,2,3,4])
matrix = numpy.array([[1,2,3],[4,5,6]])
vector = numpy.array([1,2,'3',4])
array(['1', '2', '3', '4'],dtype=')
# arrage生成1维的ndarray
np.arange(0, 10, 2)
# ones,zeros,random生成多维的ndarray
np.ones((2, 3, 4), dtype=np.int32)
np.zeros((3, 4))
np.random.random((2, 3))
import numpy as np
print(vector.shape) # (4,)
print(vector.dtype) # dtype('int64')
print(matrix.ndim) # 2
print(vector[:,np.newaxis]) #[[1],[2],[3],[4]]
实现对数据的过滤
import numpy
vector = numpy.array([[5, 10, 15, 20],[1,6,12,18]])
equal_to_ten = (vector > 10)
print(equal_to_ten)
print(vector[equal_to_ten])
equal_to_ten_and_five = (vector > 10) | (vector == 5)
print(equal_to_ten_and_five)
print(vector[equal_to_ten_and_five])
# 结果
[[False False True True]
[False False True True]]
[15 20 12 18]
[[ True False True True]
[False False True True]]
[ 5 15 20 12 18]
import numpy
# list 转换为 ndarray
list = [[5, 10, 15, 20],[1,6,12,18]]
matrix = numpy.array(list)
# ndarray 转换为 list
list_1 = matrix.tolist()
# ndarray 内部数据类型转换
matrix = matrix.astype(str)
print(matrix)
# 结果
[['5' '10' '15' '20']
['1' '6' '12' '18']]
import numpy as np
# sum()函数,全部求和,按行求和,按列求和 举一反三: min() max()
matrix = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
print(matrix.sum(),matrix.sum(1),matrix.sum(0))
# reshape函数,形状的转变
arr = np.arange(15).reshape(3, 5)
ndarray中的元素逐个进行运算
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array(3)
# 减法
print(a - b)
# 幂
print(a**2)
# 根号
print(np.sqrt(a))
# 指数
print(np.exp(a))
# 向下取整
print(np.floor(np.exp(a)))
# 结果
[[-2 -1 0]
[ 1 2 3]]
[[ 1 4 9]
[16 25 36]]
[[1. 1.41421356 1.73205081]
[2. 2.23606798 2.44948974]]
[[ 2.71828183 7.3890561 20.08553692]
[ 54.59815003 148.4131591 403.42879349]]
[[ 2. 7. 20.]
[ 54. 148. 403.]]
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.array([[7,8],[9,10],[11,12]])
D = np.array([[[0,1,2],[3,4,5]],[[6,7,8],[9,10,11]]])
# 矩阵对应位置一次相乘
print(A*B)
# 矩阵对于位置一次相减
print(A-B)
# 矩阵乘法
print(A.dot(C))
# 转置,适用于一,二维数组
print(A.T)
# 转置,适用于多维数组,正常情况为(0,1,2),(1,0,2)为第一,二维转置,(0,2,1)为第二,三维转置
print(D.transpose(1,0,2), D.transpose(0,2,1))
# 结果
[[ 7 16 27]
[40 55 72]]
[[-6 -6 -6]
[-6 -6 -6]]
[[ 58 64]
[139 154]]
[[1 4]
[2 5]
[3 6]]
[[[ 0 1 2]
[ 6 7 8]]
[[ 3 4 5]
[ 9 10 11]]]
[[[ 0 3]
[ 1 4]
[ 2 5]]
[[ 6 9]
[ 7 10]
[ 8 11]]]
矩阵的合并 hstack , vstack, column_stack , row_stack , concatenate , c_ , r_
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
# 水平合并
print(np.hstack([A,B]))
# 垂直合并
print(np.vstack([A,B]))
# 水平分割,平均分割为3部分
print(np.hsplit(A,3))
# 水平分割,在第2,3列后面进行分割
print(np.hsplit(A,(2,3)))
# 垂直分割,平均分割为2部分
print(np.vsplit(A,2))
# 垂直分割,在第2行后面进行分割
print(np.vsplit(A,(1,2)))
# 结果
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[array([[1],
[4]]), array([[2],
[5]]), array([[3],
[6]])]
[array([[1, 2],
[4, 5]]), array([[3],
[6]]), array([], shape=(2, 0), dtype=int64)]
[array([[1, 2, 3]]), array([[4, 5, 6]])]
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([], shape=(0, 3), dtype=int64)]
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
# 简单的赋值不拷贝数组对象或它们的数据。
B = A
# 视图法:不同的数组对象分享同一个数据。创造一个新的数组对象,但指向同一数据。
B = A.view()
# 切片法:不同的数组对象分享同一个数据。创造一个新的数组对象,但指向同一数据。
B = A[:,:2]
# 深复制:创建新的数组对象和数据
B = A.copy()
注: 如果任何疑问,欢迎提问,转载请注明出处.