NumPy学习心得
Posted Mellenfu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NumPy学习心得相关的知识,希望对你有一定的参考价值。
数据的csv文件存取
csv→一种常见文件格式。作用:用来存储批量数据。
写入csv的函数:
![](https://image.cha138.com/20230201/03d819907a374a76b4068fb88b17a71a.jpg)
代码展示:
![](https://image.cha138.com/20230201/95fc4feeb1674cc09b2cb8bd4ce510c6.jpg)
将整型转化为浮点型
![](https://image.cha138.com/20230201/a7fe9f7581cc456996d149bb3e0ee416.jpg)
![](https://image.cha138.com/20230201/07d15e8e152e4ced97d39dbfcbf67dac.jpg)
录取csv文件
![](https://image.cha138.com/20230201/1b8a685621c1481ea606659d06d9fd76.jpg)
数据类型转化
![](https://image.cha138.com/20230201/32bd882f27414d02baf293ac64ea77a3.jpg)
局限性:只能有效存储一维和二维数组。
np.loadtxt()和np.savetxt()这两个函数只能有效存储和读取一维和二维数组。
多维度读取
利用的函数
三参:a.tpfile(frame,sep=",format=‘%s“)
四参:np.formfile(frame,dtype=float,count=-1,sep=”)
frame:文件,字符串。
sep=“:数据分割字符串,如果是空窜,写入文件为二进制。
format=‘%s“:写入数据的格式。
dtype=float:表示读取数据的类型。eg:dtype=np.int64
count=-1:表示读入整个文件
a.tofile()函数代码展示:
![](https://image.cha138.com/20230201/b1ef998ffb47477bbbb25f8e49d440f2.jpg)
np.fromfile() 函数代码展示:
![](https://image.cha138.com/20230201/66a536f472754f3f8e32fa420dd9b1ea.jpg)
NumPy的便捷文件存取
函数的利用:np.save(frame,array)或np.savez(fname,array)或np.load(fname)
frame:文件名,以.npy为扩展名,压缩扩展名为.npz
代码展示:
![](https://image.cha138.com/20230201/ade592cf811c42059f5bd1dcd88e5952.jpg)
NumPy随机数函数
![](https://image.cha138.com/20230201/6c5cf14e99bc4e44bc67a816ef62ed7e.jpg)
np.random.rand()函数代码展示:
![](https://image.cha138.com/20230201/ec3acb8685c44c5a90324ebe7ef0fe8d.jpg)
np.random.randn()函数代码展示:
![](https://image.cha138.com/20230201/3dd96c16635e4edc8c85a5de110378ec.jpg)
np.random.randint()代码展示:
![](https://image.cha138.com/20230201/2da0a3e2126c4db898940691ce275450.jpg)
np.random.seed()函数代码展示:
![](https://image.cha138.com/20230201/562874abaef04e5ca545cb553d582bdb.jpg)
![](https://image.cha138.com/20230201/cb8bb087725b4ae3ad623489b940558e.jpg)
np.random.shuffle()函数代码展示:
![](https://image.cha138.com/20230201/9f9e9813f6044f7baa394df2ebe0a388.jpg)
np.random.permutation()函数代码展示:
![](https://image.cha138.com/20230201/0bd7dcd21360478c8d4a34d71791f0d6.jpg)
choice函数的利用
代码展示:
![](https://image.cha138.com/20230201/a8cb8d329db9451f83fd82cede11a170.jpg)
![](https://image.cha138.com/20230201/e494c1565378478c92f80c80d8e7ea20.jpg)
代码展示:
![](https://image.cha138.com/20230201/4a6c93584a50404182e87ef4c37a7acb.jpg)
![](https://image.cha138.com/20230201/a1c0ac71d5a7480baf523aeb3b96e3b6.jpg)
sum求和函数代码展示:
![](https://image.cha138.com/20230201/41b47342fa67412f9f1eb2ca311d692c.jpg)
mean()求平均值函数代码展示:
![](https://image.cha138.com/20230201/97543ed5c6f54f0f9f2fa7d18eb1fb4a.jpg)
average()函数平均函数的代码展示:
import numpy as np
a = np.arange(20).reshape(4,5)
print(a)
e = np.average(a,axis=0,weights=[10,5,1,2]) #加权平均
h = np.average(a)
print(h)
print(e)
std()函数求标准差函数代码展示:
![](https://image.cha138.com/20230201/cef32659561147c38bdcedd79febbc6e.jpg)
var()函数求方差,代码展示:
![](https://image.cha138.com/20230201/585a576798a249b6a68fe5acb894ef3e.jpg)
![](https://image.cha138.com/20230201/6a21a317a5be4937b10c050851e993e0.jpg)
代码展示:
![](https://image.cha138.com/20230201/dd49098361d14a829b24fd1d10ffe4f3.jpg)
结果展示:
![](https://image.cha138.com/20230201/43ec8f1adfbc426681a516602ed2639e.jpg)
np.random的梯度函数
只有函数np.gradient()表示计算数组中元素的梯度,当数组为多维时,返回每个梯度维度。
梯度:连续值之间的变化率,即斜率。
一维数组代码展示:
![](https://image.cha138.com/20230201/af7d217b3c314b7bbaf9c13b3eb63c5d.jpg)
多维数组代码展示:
![](https://image.cha138.com/20230201/48245f2531c74f9f9553dbc4cb53be1f.jpg)
存入csv文件的方法
# 存入csv文件
list = [a ,b ,c]
f = open('文件名.csv','w',encoding='utf-8')
csv_writer = csv.writer(f)
csv_writer.writerow(list)
以上是关于NumPy学习心得的主要内容,如果未能解决你的问题,请参考以下文章