首页 > 其他 > 详细

1.numpy的用法

时间:2019-10-29 13:33:15      阅读:85      评论:0      收藏:0      [点我收藏+]

numpy创建ndarray对象的三种方法

1.1.list转化

In [8]: import numpy as np

In [9]: a = [1,2,3,4]

In [10]: x1 = np.array(a)

In [11]: x1
Out[11]: array([1, 2, 3, 4])

In [12]: type(x1)
Out[12]: numpy.ndarray

1.2.numpy内的函数生存

In [13]: x2 = np.arange(11)

In [14]: x2
Out[14]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

1.3.文件生存

01.csv文件如下

技术分享图片

使用numpy的loadtxt方法打开

  • 第一个参数:文件名
  • delimiter:以什么分隔
  • skiprows:跳过的行
  • usecols:使用哪几列
  • unpack:默认False,表示是否把列分开
x = np.loadtxt(01.csv,delimiter=,,skiprows=1,usecols=(1,4,6),unpack=False)

显示结果

技术分享图片

In [18]: x.shape
Out[18]: (242, 3)

把每列分开保存

In [24]: open,close,volume = np.loadtxt(01.csv,delimiter=,,skiprows=1,usecols=(1,4,6),unpack=True)

结果:

技术分享图片

In [26]: open.shape
Out[26]: (242,)

 

1.4.numpy的常用函数

In [36]: c = np.random.randint(1,100,10)

In [37]: c
Out[37]: array([44, 26, 40, 87, 32, 82, 20, 70, 62, 14])

In [38]: c.min()
Out[38]: 14

In [39]: c.max()
Out[39]: 87

In [40]: c.mean()
Out[40]: 47.7

In [43]: y = np.sort(c)

In [44]: y
Out[44]: array([14, 20, 26, 32, 40, 44, 62, 70, 82, 87])

1.numpy的用法

原文:https://www.cnblogs.com/gaidy/p/11757391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!