机器学习 番外篇 02 numpy.array基本操作

numpy.array基本操作

import numpy as np
x=np.arange(10)
x
>>>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x=np.arange(15).reshape((3,5))
x
>>>array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
# 基本属性
x.ndim# 维数
>>>2
x.shape
>>>(3, 5)
xx=np.arange(10)
xx
>>>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
xx[::-1]
>>>array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
x[:2,:3]
>>>array([[0, 1, 2],
       [5, 6, 7]])
x[:,0] # 获取第一列
>>>array([ 0,  5, 10])
subx=x[:2,:3].copy() # 没有copy,则原来的数组的改变会影响现在数组
subx
>>>array([[0, 1, 2],
       [5, 6, 7]])
xx.shape
>>>(10,)
xx.reshape(2,5)
>>>array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
xx
>>>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
xx.reshape(10,-1)
>>>array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])
xx.reshape(2,-1)
>>>array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

猜你喜欢

转载自blog.csdn.net/lihaogn/article/details/81295863