【python数据分析】numpy的基本使用方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN_fzs/article/details/81176806
import numpy as np
c=[]
def numpysum(n):       #0到(n-1)的列表
    a=np.arange(n)**2  #生成一个0到(n-1)的列表,每个项再平方
    b=np.arange(n)**3  #生成一个0到(n-1)的列表,每个项再进行3次方
    c=a+b             #两个列表相加
    return c

c=numpysum(5)
print(c)
print(c[0:2])      #切片输出
c[0]=10            #将第1个数改为10
print(c)
print(c.dtype)      #查看数据类型
print(c.shape)      #查看维度
[ 0  2 12 36 80]
[0 2]
[10  2 12 36 80]
int32
(5,)
np.zeros(10)    #都是0的数据
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
np.zeros((2,5))
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
np.empty((2,3,5)) 
array([[[  1.13899312e-311,   9.58487353e-322,   0.00000000e+000,   0.00000000e+000,   1.13738974e-311],
        [  5.02034658e+175,   7.03899687e-033,   3.06994030e-057,   1.09044515e-071,   7.82430736e+020],
        [  1.47763641e+248,   1.16096346e-028,   7.69165785e+218,   1.35617292e+248,   6.21063611e-038]],

       [[  3.06189175e-057,   1.58571427e+006,   3.04884885e+025,   4.26032557e-096,   6.32299154e+233],
        [  6.48224638e+170,   5.22411352e+257,   5.74020278e+180,   8.37174974e-144,   1.41529402e+161],
        [  9.16651763e-072,   4.10024123e+097,   1.74686353e-076,   2.74549255e+126,   4.06321267e-317]]])
a = np.array([[1,2],[3,4]])
print(a)
print(a[0,0])
print(a[0,1])
print(a[1,0])
print(a[1,1])
[[1 2]
 [3 4]]
1
2
3
4
#numpy数据类型
#类型               #类型代码         #说明
#int8、uint8        i1、u1            有符号、无符号的8位类型
#int16、uint16      i2、u2            有符号、无符号的16位类型
#int32、uint32      i4、u4            有符号、无符号的32位类型
#int64、uint64      i8、u8            有符号、无符号的64位类型

#float16            f2                半精度浮点数
#float32            f4或f             标准单精度浮点数
#float64            f8或d             标准双精度浮点数 
#float128           f16或g            拓展精度浮点数

#bool               ?                 存储true和false类型,0为false,非0为true
#string             s                 固定长度的字符串类型。比如创建长度为10的字符串:S10
#object             o                 python对象类型
#unicode            u                 固定长度的unicode
print('int8 : 10.0')
print(np.int8(10.0))

print('float16 : 10')
print(np.float16(10))
int8 : 10.0
10

float16 : 10
10.0
print('bool : 15')
print(np.bool(15))
print(np.bool(-1))
print(np.bool(0))
bool : 15
True
True
False
print(np.arange(7,dtype=np.float16))     #创建数据的时候,定义数据类型
print(np.arange(5,dtype='i1'))
print(np.arange(5,dtype='f'))
[ 0.  1.  2.  3.  4.  5.  6.]
[0 1 2 3 4]
[ 0.  1.  2.  3.  4.]
#数据类型的转换
arr = np.array([1,2,3,4,5])
print(arr.dtype)
float_arr = arr.astype(np.float64)
print(float_arr.dtype)
int32
float64
#创建自定义数据类型   商品名称(40个长度的字符串),商品数量(int32),价格(float32)
t = np.dtype([('name',np.str_,40),('numitems',np.int32),('price',np.float32)])
print(t)
print(t['name'])
itemz = np.array([('苹果',25,2.6),('西瓜',30,25)],dtype=t)
print(itemz)
[('name', '<U40'), ('numitems', '<i4'), ('price', '<f4')]
<U40
[('苹果', 25,   2.5999999) ('西瓜', 30,  25.       )]
#数组与标量之间的运算
arr = np.array([[1,2,3],[4,5,6]])
print(arr*arr)
print(1/arr)
print(arr*0.5)
[[ 1  4  9]
 [16 25 36]]

[[ 1.          0.5         0.33333333]
 [ 0.25        0.2         0.16666667]]

[[ 0.5  1.   1.5]
 [ 2.   2.5  3. ]]
#一维数组的索引与切片
a = np.arange(9)
print(a[0:9:2])    #0到9,每隔一个数输出
print(a[:-1])

s = slice(3,7,2)   #3到7,每隔一个数输出
print(a[s])
[0 2 4 6 8]
[0 1 2 3 4 5 6 7]
[3 5]
#多维数组的索引与切片
b = np.arange(24).reshape(2,3,4)   #2个数组(2维),3行,4列
print(b)
print('----------------------------------------------------------1------')
print(b[0,0,0])
print('----------------------------------------------------------2------')
print(b[1,2,1])  #第2个数组,第3行,第2列
print('----------------------------------------------------------3------')
print(b[:,0,0])
print('----------------------------------------------------------4------')
print(b[:,1:2])
print('----------------------------------------------------------5------')
print(b[:,1,1:3])     #第1、2维,第2行,第2第3个数据  5 6 17 18
print('----------------------------------------------------------6------')
print(b[:,1])         #第1、2维,第2行 
print('----------------------------------------------------------7------')
print(b[:,:,3])       #第1、2维,第4列 
print('----------------------------------------------------------8------')
print(b[::-1])        #把第1和第2维的位置互换
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
----------------------------------------------------------1------
0
----------------------------------------------------------2------
21
----------------------------------------------------------3------
[ 0 12]
----------------------------------------------------------4------
[[[ 4  5  6  7]]

 [[16 17 18 19]]]
----------------------------------------------------------5------
[[ 5  6]
 [17 18]]
----------------------------------------------------------6------
[[ 4  5  6  7]
 [16 17 18 19]]
----------------------------------------------------------7------
[[ 3  7 11]
 [15 19 23]]
----------------------------------------------------------8------
[[[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]]

猜你喜欢

转载自blog.csdn.net/CSDN_fzs/article/details/81176806