1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Python学习笔记1环境搭建+Numpy

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

时间:2023-01-25 01:32:38

相关推荐

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

Python環境安裝

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b39NVfVZ-1595218155027)( https://img-/103604428.png )]

打開cmd命令框輸入python出現》》》環境搭建結束

庫的安裝

pip或者anaconda

anaconda是一个一款专业使用的集成型Python环境,安装后默认安装Python,IPython,集成开发环境Spyder和众多的包和模块,一键安装,装好即用。

官网下载速度过慢 可以在其他地方下载 这里选择的清华大学镜像

https://mirrors.tuna./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再进行判断1vector = 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 arraya.size //15 the total of elements of the arraynp.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 numbersarray([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-bprint(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*4print(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 objecta=b直接等于 是指向同一个地址的id,不论对谁操作,两个都会同时改变2.The view method creates a new array object that looks at the same datac=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 datad=a.copy()d is a # false 两者没有任何关系,其中一个改变另一个没有变化

时间:.7.20 14.04

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。