Python_机器学习_Numpy

资源:Github kaggle 各大资源分享点
配置:使用Anaconda下载使用python来使用
网址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

Anadonda里面有一个notebook可以启动网页版编辑
shifter+enter 可以一行一行执行命令
在这里插入图片描述

如何在anaconda中安装各种库 如安装 tensorflow
anaconda search -t conda tensorflow
anaconda show jjhelmus/tensorflow【想安装的版本】
复制显示的链接就可以下载

numpy模块

import numpy
world_data = numpy.genfromtxt("Beam1Dose.txt",delimiter="   ",dtype=str)  #读取txt比较合适的东西  delimiter  分隔符
print(type(world_data))  #numpy.ndarray
print(world_data)  #输出二维数组值
print(help(numpy.genfromtxt))  #打印帮助文档

vector = numpy.array([5,10,15,20])  #array里面必须是相同的数据结构
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45],[50,60,70]]) #4*3 构造三维就是三个中括号

print(vector.shape)  #向量的维度 (4,)
print(matrix.shape)  #二维的话0是行数 1是列数  (4,3)
vector.dtype   #查看数据类型 在网页版中可查

vector = numpy.array([5,10,15,20])  #array里面必须是相同的数据结构,否则是最大兼容模式
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45],[50,60,70]])

print(matrix[1,1]) #index从0开始  第二行第二例
print(vector[0:3])  #包括前面不包括后面,左闭右开
print(matrix[:,1])  #和matlab一样的切片 第二列
print(matrix[1,:])  #第二行
print(matrix[0:2,:]) #取第一行和第二行数据

print(vector == 10)        #bool 每一个数据有没有等于10 布尔值可以当成索引来使用
print(matrix[matrix == 25])  #找到对应的元素
# 与 或   & |

vector = numpy.array(["1","2","3"])
print(vector,vector.dtype)
vector = vector.astype(float)  #类型变换 从str转变成float
print(vector,vector.dtype)
print(vector.max()) #最大最小等等
#print(help(numpy.array))
matrix = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
print(matrix.sum(axis = 1))  #对行求和 
print(matrix.sum(axis = 0))  #对列求和 里面什么都没有是整个二维数列求和

a = numpy.arange(15)
print(a)
b = a.reshape(3,5)
c = a.reshape(3,-1)  #-1表示会自动进行一个计算
print(b)
print(b.ndim)  #维度 2

[ 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]]
 2

 print(b.size)  #数据数量 15
 print(np.zeros((3,4))) #初始化3行4列的0矩阵 元组格式
print(np.ones((2,3,4),dtype=np.int32))  #3行4列2层

data = np.arange(10,30,5)  #第一个数  最后一个数  步长
print(data)

#np中的随机模块  进入np.random模块调用了random函数
data = np.random.random((2,3))  #创造一个2行3列在0-1之间的随机值
print(data)

b = np.linspace(0,2,100)  #第一个数 最后一个数 中间的数量
print(b)

#对应的数组操作
a = np.array([20,30,40,50])
b = np.arange(4)
c = a - b #对应位置相减
print(c)
print(b**2)  #对应位置元素平方

a = np.array([
    [1,1],
    [0,1]
])
b = np.array([
    [2,0],
    [3,4]
])

print(a*b)        #对应位置进行相乘
print(a.dot(b))   #矩阵相乘
print(np.dot(a,b))

print(np.exp(1))
print(np.sqrt(25))

c = np.floor(10*np.random.random((3,4)))  #floor向下取整 3.5转3
print(c)
print(c.T)  #转置

print(c.ravel())  #矩阵拉成一个向量  一行一行连起来

a = np.array([
    [1,1],
    [0,1]
])
b = np.array([
    [2,0],
    [3,4]
])

print(np.hstack((a,b)))  #横着拼接
print(np.vstack((a,b)))   #竖着拼接

a = np.floor(10*np.random.random((2,12)))
print(a)

a1 = np.hsplit(a,3)   #按着行拼接的反方向 断了三刀 也就是列方向分了3段
print(a1)
print(np.hsplit(a,(3,4)))  #指定刀位

#赋值的三种操作
a = np.arange(12)
b = a   #名字不同但是指向区域一样的
print(b is a)
b.shape = 3,4
print(a.shape)

c = a.view()  #浅复制  id指向不同 但是公用的是一套值
print(c is a)
c.shape = 2,6
print(a.shape)
print(a)
c[0,1] = 100
print(a)

d = a.copy()  #深复制  两个都不一样 没有关系的
d[0,0] = 100
print(a)
print(d)

# 排序和索引
#寻找最大值 并根据id进行求值
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])]  #IND 得到行数 shape【1】得出列数
print(data_max)

a = np.arange(0,40,10)
print(a)
b = np.tile(a,(2,3))  #进行扩展 行变成2倍 列变成3倍
print(b)

a = np.array([[4,3,5],[1,2,1]])
print(a)
b = np.sort(a,axis=1)  #按行进行向上排序
print(b)

a = np.array([4,3,1,2])
j = np.argsort(a)     #求出数据从小到大数据的id索引
print(j)
print(a[j]) #得到排序完之后的结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值