numpy

作者: chliar | 来源:发表于2018-04-14 23:06 被阅读0次
import numpy as np

t = np.arange(12).reshape(2, 6)
t1 = np.arange(2).reshape(2, 1)

np.aranger(12)  #相当于range(12),生成[ 0  1  2  3  4  5  6  7  8  9 10 11]
np.reshape( 块,行 ,列 )  #  分割成多维数组这个是三维数组,二维:np.reshape( 行 ,列 ) 

数组相加减乘除:
t1 = np.arange(12).reshape((2,6))  #(2,6)
t22 = np.arange(6)   #(6,)
t2 = np.arange(4,6).reshape((2,1))  #(2,1)

print(t1)
print(t2)
print(t22)

t1 >> [[ 0  1  2  3  4  5]
         [ 6  7  8  9 10 11]]

t2>>
      [[4]
       [5]]

t22>>
      [0 1 2 3 4 5]

print(t1*t2)>>    [[ 0  4  8 12 16 20]
                  [30 35 40 45 50 55]]
     
print(t1*t22)>>    [[ 0  1  4  9 16 25]
                    [ 0  7 16 27 40 55]]

计算平均值:
t10 = np.arange(0,10).reshape((10))
>>  [0 1 2 3 4 5 6 7 8 9]

#计算平均数
t_mean = t10.mean(axis=0)
print  t_mean
print  t10-t_mean
>>  4.5
>> [-4.5 -3.5 -2.5 -1.5 -0.5  0.5  1.5  2.5  3.5  4.5]

#读取csv文件(csv文件数据的格式是以逗号隔开)
us_file_path= "./youtube_video_data/US_video_data_numbers.csv"
uk_file_path= "./youtube_video_data/GB_video_data_numbers.csv"

#np.loadtxt()读取文件
#参数说明(us_file_path :文件路径; delimiter=",":读取的数据以逗号分隔;dtype=int 读取的数据指定类型)
us_data = np.loadtxt(us_file_path,delimiter=",",dtype=int)

#unpack=1转置,对角线旋转不是很理解
uk_data = np.loadtxt(uk_file_path,delimiter=",",dtype=int,unpack=1)
print(uk_data.shape)
uk_data = np.loadtxt(uk_file_path,delimiter=",",dtype=int)
print(uk_data.shape)


numpy取值操作:
t = np.arange(24).reshape((4,6))
>> [[ 0  1  2  3  4  5]
   [ 6  7  8  9 10 11]
   [12 13 14 15 16 17]
   [18 19 20 21 22 23]]

#t(,)逗号隔开,前面是对行进行操作,后面是对列进行操作
#选择某几行的某几个列
print  t[0:2, 3:6] # 0~2 )行的第3~6 列 
>> [[ 3  4  5]
   [ 9 10 11]]

#选择两个列表中对应位置的值
print(t[[1,2],[2,3]])  #相当于坐标(1,2)和(2,3)
>>  [ 8 15]

#选择某一列(结构也是一维数组)
print(t[:,2])
>> [ 2  8 14 20]
  
#选择一个值
print(t[2,3])

#修改值:
t1 = np.arange(24,dtype=float).reshape((6,4))
t1[[1,2],[3,1]] = np.nan
print t1
>> [[ 0.  1.  2.  3.]
   [ 4.  5.  6. nan]
   [ 8. nan 10. 11.]
   [12. 13. 14. 15.]
   [16. 17. 18. 19.]
   [20. 21. 22. 23.]]




相关文章

  • 科学计算库numpy的执行示例

    numpy1 numpy2 numpy3 numpy4

  • numpy中的常量

    Constants 正无穷 numpy.inf numpy.Inf numpy.Infinity numpy.in...

  • NumPy学习资料

    Numpy 中文资料 NumPy 中文文档 NumPy 中文用户指南 NumPy 中文参考手册

  • Numpy基础

    安装Numpy Numpy Numpy属性 ndim:纬度 shape:行数和列数 size:元素个数 Numpy...

  • Numpy和Pandas基本操作速查

    """ numpy 基本操作 """'''安装 Numpy 的方法:pip install numpy''''''...

  • numpy 基础

    numpy 基础 导入numpy 版本 np常用方法 numpy.array 的基本属性 numpy.array ...

  • Numpy入门

    1、熟悉 numpy 的基础属性 2、numpy 创建 array 3、numpy的基础运算 4、numpy索引 ...

  • 学习:biopython的安装

    安装Numpy 因为使用biopython需要numpy的支持,所以需要先安装numpy。安装numpy过程如下:...

  • Numpy

    Numpy中文文档 # 基本语法 ``` import numpy myText = numpy.genfromt...

  • numpy运算

    numpy的与运算 numpy 中 argsort() numpy 中的布尔索引

网友评论

    本文标题:numpy

    本文链接:https://www.haomeiwen.com/subject/kpqrkftx.html