Numpy

1 Numpy 数据类型must be the same type

2 创建ndarray

     (1)传入一个list

import numpy as np
data1=[1,2,3,4]
arr1=np.array(data1)
data2=[[1,2,3],[2.1,3.5,4]]
arr2=np.array(data2)

arr2.ndim   #结果为2  arr2.ndim=len(arr2.shape) 即指维数
arr2.shape  #结果为(2,3)
arr2.dtype  #结果为'float64'

      (2)特殊的ndarray

np.zeros(10)

np.zeros((3,6))   #还有 np.zero_like()  np.ones()  np.ones_like

np.empty((2,3,4))  #creats an array without initializing its values to any particular value

np.arange(4) #output [0,1,2,3]

np.eye() #create a square N*N identity matrix(1's on the diagonal and 0's selsewhere)

    (3)转换为指定格式的array

float_arr=arr1.astype(np.float64)

3 运算

    运算是针对每一个元素进行的

4 索引与切片  (在源数据上改变,不会复制,除非说明  后加.copy())

     (1) one-dimensional

arr=np.arange(10)
# output array([0,1,2,3,4,5,6,7,8,9])

arr[5]   #索引从0开始
5

arr[2:4]   #不包括4
2,3

arr[5:8]=12  #将5,6,7赋值为12

arr[:]   #全部arr的元素都选中

     (2)highter dimensional

arr2d=np.array([1,2,3],[4,5,6],[7,8,9])

arr2d[2]
[7,8,9]

arr2d[0][2]
3
原文地址:https://www.cnblogs.com/qicaide/p/5897730.html