Python学习笔记1环境搭建+Numpy

Python環境安裝

系統環境path裡面配置安裝路徑打開cmd輸入py查看是否安裝路徑:D:/sofeware/python3.8 配置系統環境變量(可以安裝時選擇本身就配置)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b39NVfVZ-1595218155027)( https://img-blog.csdnimg.cn/20200718103604428.png )]
打開cmd命令框輸入python出現》》》環境搭建結束

庫的安裝

pip或者anaconda
anaconda是一个一款专业使用的集成型Python环境,安装后默认安装Python,IPython,集成开发环境Spyder和众多的包和模块,一键安装,装好即用。
官网下载速度过慢 可以在其他地方下载 这里选择的清华大学镜像
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archieve/
Anaconda Prompt 相当于win里面的cmd
conda list 查看安装的库
conda install numpty 安装对应的库
anaconda search -t conda tensorflow 查找到适合win64的便show一下anaconda show paulyim/tensorflow 会有安装的提示命令在这里插入图片描述

科学计算库Numpy

首先在anaconda prompt 使用conda list 查看是否含有numpy(默认是安装了的)
numpy路径 D:\SofeWare\Anaconda3\lib-packages\numpy\lib\npyio.py
命令行cmd 打开 jupyter notebook
不知道某一个怎么使用可以用help查看文件怎么使用

vector=numpy.array([1,2,3,4]) print(vector.shape) (4,)一维四个元素 可以通过这个调试
matrix = numpy.array([[1,2,3],[4,5,6]])print(matrix.shape) (2,3)两行三列
注意:numpy.array里面创造的东西必须是相同结构(同int,float)
matrix.dtype 能够得到当前的类型是什么类型

读取了某一个文件后test(n*m格式)
获取数据跟二维数组相似 test[1,2]获取第二行第三个数字(默认0开始)

print(vector[0:3])//获取数组 0 1 2 元素的值
print(matrix[:,1]) //获得 2 5  取出第二列的所有元素
print(matrix[:,0:n]) //获得 从0列到n-1列

需要进行判断比较
vector==2  //array([Flase,True,Flase,Flase],dtype=bool) 对每一个元素都做了这个判断,不用写for循环
print(vector[vector==2])  //2  可以把判断结果(bool类型)作为索引
print((vector==10)&(vector==5)) //与的判断
print((vector==1)|(vector==5)) //或的判断,之所以用()因为用的python3版本 先进行判断5再进行判断1
vector =  vector.astype(float) //转换类型全部变成 flat类型
vector.min() //查看最小值
print(help(numpy.array)) //查看帮助文档
axis means that we want to perform the operation on each row,and 0 means on each colunm /按行或者按列求和用axis来确定维度
matrix = numpy.array([
					[1,2,3],
					[4,5,6]])
matrix.sum(axis=1) //array([6,15]) 输出的是每一行加的值
matrix.sum(axis=0) //array([5,7,9]) 输出的是每一列加的值

进行矩阵变换

import numpy as np 
print(np.arange(15))
a = np.arange(15).reshape(3,5)
a
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
Out[14]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
a.ndim  // 2  the number of axes(dimensions) of array
a.size  //15  the total of elements of the array
np.zeros((3,4)) //初始化一个3*4的矩阵 m默认是float类型
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
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]]])
np.arange(10,30,5) to create sequences of numbers
array([10, 15, 20, 25])

numpy中的随机模块 (用的也是比较多的)

np.random.random((2,3)) //进入random模块调用random函数,产生一个2*3的矩阵
array([[0.04749864, 0.38805347, 0.96410084],
       [0.23405126, 0.45344106, 0.0927227 ]])
np.linspace(0,n,m) //从0-n 均匀去取m个值

numpy中进行简单的数学运算

a=np.array([20,30,20,50])
b=np.arange(4)
print(a)
print(b)
c=a-b
print(c)
print(c*a) c**2 平方
print(a<35)
[20 30 20 50]
[0 1 2 3]
[20 29 18 47]
[ 400  870  360 2350]
[ True  True  True False]
The matrix can be performed using the dot funciton or method 
import numpy as np 
A=np.array([[1,1],
            [0,1]])
B=np.array([[2,0],
            [3,4]])
print(A)
print("----------")
print(B)
print("--A*B---")  //对应位置相乘
print(A*B)
print("--A.dot(B)---")  // 矩阵的乘法
print(A.dot(B))
[[1 1]
 [0 1]]
----------
[[2 0]
 [3 4]]
--A*B---
[[2 0]
 [0 4]]
--A.dot(B)---
[[5 4]     1*3+2*3   1*0+1*4
 [3 4]]    0*2+1*3   0*0+1*4
 print(np.exp(A)) //e的多少次幂
 print(np.sqrt(A)) //开根号
 a=np.arange(4)
print(a)
print(np.exp(a))
[0 1 2 3]
[ 1.          2.71828183  7.3890561  20.08553692] //默认保留八位小数

矩阵操作
return the floor of the input 向下取整

 print ( 10*np.random.random (( 3 , 4 )) )
 a = np.floor ( 10*np.random.random (( 3 , 4 )) )
 print ( "--floor-- " )   //向下取整
print ( a )
 print ( "--flatten the array--" )    //變為向量
print ( a.ravel ( ))
 a.shape = ( 6,2 )          //改變shape 
print ( "--shape--")
 print ( a )
 print ( "--T--" )       //轉置行列變換一下
print ( a.T )
 [ [ 2.87655236 9.95925534 2.47549295 7.57271023 ] [ 8.54415777 9.03817198 8.97318608 5.28231893 ] [ 8.43418672 2.51880521 9.09227875 9.00964137 ] ] --floor- - [ [ 9. 4. 1. 9. ] [ 2. 0. 8. 1. ] [ 7. 3. 8. 4. ] ] --flatten the array-- [ 9. 4. 1. 9. 2 . 0. 8. 1. 7. 3. 8. 4. ]
  --shape--
[ [ 9. 4. ] [ 1. 9. ] [ 2. 0. ] [ 8. 1. ] [ 7. 3. ] [ 8. 4. ] ] --T-- [ [ 9. 1. 2 . 8. 7. 8. ] [ 4. 9. 0. 1. 3. 4. ] ] 
a.reshape ( 3,-1 )   //-1表示他自己去計算```

矩陣拼接

= np.floor ( 10 * np.random.random ((2 ,2 )) )
 b = np.floor ( 10 * np.random.random ((2 ,2 )) )
的打印(“ - -a--“ )
打印( a )
打印(” --b--“ )
打印( b )
打印(”-橫著拼接hstack--“ )
( np.hstack (( a , b )))  //注意看這裡括號
print ( "--豎著拼接vstack--" )
 print ( np.vstack (( a , b )) )
 --a-- [ [ 8. 1. ] [ 2. 5. ] ] --b-- [ [ 8. 3. ] [ 4. 8. ] ]
  --橫著拼接hstack-- 
  [ [ 8. 1. 8. 3. ] [ 2. 5. 4. 8. ] ] 
  - -豎著拼接vstack-- 
  - [ [ 8. 1. ] [ 2. 5. ] [ 8. 3. ] [ 4. 8. ] ] ```

矩陣的切分`

 print ("--橫著切分hsplit--") 
 print ( np.hsplit ( a,3 ))# 12/3每一個都含有4列 
 print ( np.hsplit ( a, ( 3,4 )))#slipt a after the third and the fourth column (3,4)是一個元組 
 print ("--豎著拼接vsplit--") 
 np.vsplit ( b,3 )

`賦值操作

1.a and b are two names for the sanme ndarray object
a=b直接等于 是指向同一个地址的id,不论对谁操作,两个都会同时改变
2.The view method creates a new array object that looks at the same data
c=a.view()
print(c is a) # false   不完全相等 可以通过 a.shape改变来查看 虽然a c 指向不同的地址但是其中一个改变,两个同时会改变
print(id(a)) # 输出两个是不一样
3.The copy method makes a complete copy of the array and its data
d=a.copy()
d is a  # false 两者没有任何关系,其中一个改变另一个没有变化

时间:2020.7.20 14.04

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值