Python-Numpy学习

读取数据

用numpy 读取txt

#用numpy读取txt
import numpy
#delimiter="," txt中的文件是用,隔开的
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",")
print(type(world_alcohol))
print(help(numpy.genfromtxt))

1. 结果: <class ‘numpy.ndarray’> 是numpy重要的格式
2. 打印帮助文档可以知道用法
3. List item

对数据进行操作

将列表转化为矩阵

#array()函数可以接受一个或多个列表作为输入。当我们输入一个列表,我们得到一个一维数组的结果:
vector = numpy.array([5, 10, 15, 20])
#当我们输入一个列表的列表,我们得到一个矩阵的结果:
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print (vector)
print (matrix)

查看矩阵

#我们可以用ndarray。形状属性来计算数组中有多少元素
vector = numpy.array([1, 2, 3, 4])
print(vector.shape)
#对于矩阵,形状属性包含一个包含2个元素的元组。
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
'''
结果:(4,)
     (2, 3)
'''

如何查看矩阵类型


numbers = numpy.array([1, 2, 3, 4])
print(numbers.dtype)
  • NumPy数组中的每个值必须具有相同的数据类型

  • NumPy将在读取数据或将列表转换为数组时自动找出适当的数据类型。

  • 您可以使用dtype属性检查NumPy数组的数据类型。

取出指定位置的值

#第一步读取文件
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)
#print(world_alcohol)
#第二步读取指定位置的值
uruguay_other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print (uruguay_other_1986)
print (third_country)
  • 索引取值的方法

切片

vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])
#结果:[5, 10, 15]
matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[1:3,0:2])
'''
结果:[[20 25]
     [35 40]]
 '''
  • : 是所有值的意思

与&或|

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print (equal_to_ten_and_five)
#结果:[False False False False]
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]

数据类型的转化-astype

vector = numpy.array(["1", "2", "3"])
print (vector.dtype)
print (vector)
vector = vector.astype(float)
print (vector.dtype)
print (vector)
'''
结果如下:
<U1
['1' '2' '3']
float64
[1. 2. 3.]
'''

求极值、和、均值

vector = numpy.array([5, 10, 15, 20])
vector.sum()
vector.min()
vector.max()
vector.mean()
#有问题就喊Help吧
print(help(numpy.array))
  • 小拓展:按行或者按列进行求和
#轴指示我们在哪个维度上执行操作
#1表示希望对每一行执行操作,0表示on each column
matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=1)
#结果:array([ 30,  75, 120])

reshape

  • 对矩阵进行一个整体的变化
import numpy as np
b= np.arange(15)#先造矩阵
a = np.arange(15).reshape(3, 5)
print(b)
print(a)
print(a.shape)
'''
结果: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]</font>
(3, 5)   
'''

构建一个全为0的矩阵

  • 初始化了一个零矩阵
import numpy as np
np.zeros ((3,4)) 
'''结果:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
'''
  • 初始化了两个为1矩阵
import numpy as np
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]]])
'''

构造一个矩阵
指定了值

import numpy as np
np.arange( 10, 30, 5 )#从10 开始 到30结束,隔5取值(小于30)
#结果:array([10, 15, 20, 25])
np.arange( 0, 2, 0.3 )#从0 开始 到2结束,隔0.3取值(小于2)
#结果array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])
  • 因为同一个矩阵的类型一样所有这里是0.
  • 取值范围:<x<=

构建了一个随机矩阵

import numpy as np0
print(np.random.random((2,3))

构建矩阵引入pi(Π)
linspace

from numpy import pi
np.linspace( 0, 2*pi, 10)
#pi 就是数学上的Π从0开始,终点值2*pi,平均取10个

构建矩阵引入pi(Π)
总结:arrange(A) -> 取0–A-1个数
arrange(A,B,C)->A是起始值可以取到,B是不可取到的极大值,C是个数

矩阵的运算
乘->有两种:1:*;2:.dot()

A = np.array( [[1,1],
               [0,1]] )
B = np.array( [[2,0],
               [3,4]] )
print (A)
print (B)
print (A*B)
print (A.dot(B))
print (np.dot(A, B) )
'''
[[1 1]
 [0 1]]
[[2 0]
 [3 4]]
[[2 0]
 [0 4]]
[[5 4]
 [3 4]]
[[5 4]
 [3 4]]
'''

e/开方->exp(B)/sqrt(B)

import numpy as np
B = np.arange(3)
print (B)
print (np.exp(B))
print (np.sqrt(B))
'''
结果:
[0 1 2]
[1.         2.71828183 7.3890561 ]
[0.         1.         1.41421356]
'''

矩阵.floor &ravel&resize

floor()->向下取整
ravel()->矩阵拉成向量
resize()->重新组合
.T->转置

import numpy as np
a = np.floor(10*np.random.random((3,4)))#floor->向下取整
print(a) 
print (a.ravel())
a.shape = (6, 2)
print (a )
print (a.T)
print (a.resize((2,6)))
print (a)
'''结果:
[[3. 2. 2. 1.]
 [0. 1. 5. 6.]
 [9. 1. 0. 4.]]
[3. 2. 2. 1. 0. 1. 5. 6. 9. 1. 0. 4.]
[[3. 2.]
 [2. 1.]
 [0. 1.]
 [5. 6.]
 [9. 1.]
 [0. 4.]]
[[3. 2. 0. 5. 9. 0.]
 [2. 1. 1. 6. 1. 4.]]
None
[[3. 2. 2. 1. 0. 1.]
 [5. 6. 9. 1. 0. 4.]]
'''

如果在整形操作中给定一个尺寸为-1,则自动计算尺寸

import numpy as np
a.reshape(3,-1)

矩阵的拼接

np.hstack->按列进行拼接
np.vstack->按行进行拼接

a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print (a)
print (b)
print (np.hstack((a,b)))
print (np.vstack((a,b)))
'''
结果:
[[2. 1.]
 [0. 1.]]
[[7. 6.]
 [8. 6.]]
[[2. 1. 7. 6.]
 [0. 1. 8. 6.]]
[[2. 1.]
 [0. 1.]
 [7. 6.]
 [8. 6.]]
'''

矩阵的切分
.hsplit->按列进行切分
np.vsplit->按行进行切分

a = np.floor(10*np.random.random((4,4)))
print (a)
print(np.vsplit(a,2))#2代表要切分为几份
print(np.hsplit(a,2))#2代表要切分为几份
print (np.hsplit(a,(3,4)) )  #在第三列和第四列后面拆分a
'''
结果:
[[7. 2. 4. 1.]
 [8. 9. 8. 6.]
 [5. 1. 4. 0.]
 [3. 2. 0. 3.]]
[array([[7., 2., 4., 1.],
       [8., 9., 8., 6.]]),
 array([[5., 1., 4., 0.],
       [3., 2., 0., 3.]])]
[array([[7., 2.],
       [8., 9.],
       [5., 1.],
       [3., 2.]]),
 array([[4., 1.],
       [8., 6.],
       [4., 0.],
       [0., 3.]])]
[array([[7., 2., 4.],
       [8., 9., 8.],
       [5., 1., 4.],
       [3., 2., 0.]]), 
array([[1.],
       [6.],
       [0.],
       [3.]]), array([], shape=(4, 0), dtype=float64)]
'''

复制
1.通过=进行复制;改变a,就会改变b
2.通过.view()进行复制;

a = np.arange(12)
b = a
#a和b是同一个ndarray对象的两个名称
print(b is a)
b.shape = 3,4
print (a.shape)
print (id(a))
'''结果
True
(3, 4)
6322640
6322640
'''
c = a.view()
c is a
c.shape = 2,6
print (a.shape)
c[0,4] = 1234
print(a)

PS:他们指向的id不同,但是值是一样的
总结:他们两个都是指定相同的值,但是=所对应的id是一样的,而.view指定的id不一样

索引
找出一列的最大值的Id,并且输出最大值

import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)
print (data)
#找出一列的最大值所对应的索引
ind = data.argmax(axis=0)
print (ind)
#找出一列的最大值,通过找索引值,找到最大值
data_max = data[ind, range(data.shape[1])]
print (data_max)
'''
结果:
[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]
''''

利用.tile()进行扩展

a = np.arange(0, 40, 10)
b = np.tile(a, (2, 2)) 
print (a)
print (b)
'''
结果:
[ 0 10 20 30]
[[ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]]
'''

排序

a = np.array([[4, 3, 5], [1, 2, 1]])
print (a)
b = np.sort(a, axis=1)#排序 axis=1从小到大
print(b) 
a.sort(axis=1)
print (a)
a = np.array([4, 3, 1, 2])
j = np.argsort(a)#对最大值的索引值进行排序
print(j) 
print (a[j])#按索引排序后的值

重点:print (a[j])#按索引排序后的值!!!!

–end–

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值