美文网首页
Python学习笔记(一)——Numpy

Python学习笔记(一)——Numpy

作者: hgt312 | 来源:发表于2017-08-22 00:44 被阅读0次

numpy文档链接:https://docs.scipy.org/doc/

numpy的核心数据结构:ndarray(n-dimension array,即多维数组)

示例:

import numpy as np
vector = np.array([5, 10, 15, 20])
matrix = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])

shape属性:获取矩阵的行数和列数

print(vector.shape)
>>> (4,)
print(matrix.shape)
>>> (2,3)

genfromtxt方法:从csv文件中读取数据

world_alcohol = np.genfromtxt(“world_alcohol.csv”,  
delimiter=”,”,  dtype=”U75″)

numpy中的数据类型有以下几种:

  1. bool,布尔类型,值为 True 或 False
  2. int,包含int16、int32 和 int64
  3. float,包含float16、float32 和 float64
  4. string,包含string 和 unicode

利用dtype属性可以查看array的数据类型,array中的数据是相同的类型。array和python内置的list类型非常相似,这是二者非常不同的一点。

相关文章

网友评论

      本文标题:Python学习笔记(一)——Numpy

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