numpy学习笔记 1

# encoding=utf-8

#导入numpy 包
import numpy as np


def pythonsum(n):
        # 获取一个n 个元素的数组
    a = np.arange(n)
    b = np.arange(n)
    c = []
    for i in range(len(a)):
        # a[i]的2次方
        a[i] = i ** 2
        #b[i]的3次方
        b[i] = i ** 3
        c.append(a[i] + b[i])
    return c


def npsum(n): 
        # 数组中的每个元素都进行二次方
    a = np.arange(n) ** 2
        #数组中的每个元素都进行三次方
    b = np.arange(n) ** 3
    c = a + b
    return c


#  效率比较
import sys
from datetime import datetime

size = 1000
start = datetime.now()
c = pythonsum(size)
delta1 = datetime.now() - start
print "The last 2 elements of the sum ", c[-2:]
print "Pythonsum elaposed time in microseconds ", delta1.microseconds

start = datetime.now()
d = npsum(size)
delta2 = datetime.now() - start
print "The last 2 elements of the sum ", d[-2:]
print "Pythonsum elaposed time in microseconds ", delta2.microseconds

# 创建多维数组
# 创建一个两行的二维数组,第一行3个元素,第二行2个元素
m = np.array([np.arange(3), np.arange(2)])
print m
# 获取数组的结构信息
print m.shape
# 数组的数据类型
print m.dtype
# 创建一个10元素全为0的数组
np.zeros(10)
# 创建一个三维的数组,维度为234
np.empty((2, 3, 4))
# 创建一个以数组为元素的二位数组
a = np.array([[1, 2], [3, 4]])
print a
print "In : a[0,1]"
print a[0, 1]
#In [58]: a=np.array([[1,2],[3,4,5]]) 
#In [59]: print a
#[list([1, 2]) list([3, 4, 5])]
#
#numpy 里面的数据类型
print "in:int8(42.0)"
print np.int8(42.0)

print "In:bool(42)"
print np.bool(42)

print "In bool(-42.0)"
print np.bool(-42.0)

print "In bool(0)"
print np.bool(0)

print "In:float(True)"
print np.float(True)
print np.float(False)

print "In:arange(7,dtype=uint16)"
print np.arange(7, dtype=np.int8)
print np.arange(7, dtype=np.uint8)
print " print np.arange(7,dtype=np.float)  "
print np.arange(7, dtype=np.float)
#复数类型数据
fu = 123 - 12j
print fu.real
print fu.imag

print "In :int(42.0+1.j)"
try:
    print np.int(42.0 + 1.j)
    print "test"
except TypeError:
    print "TypeError"
print "In:float42.0+1.j"
# print float(42.0+1.j)
#获取一个指定元素的数组
arr = np.array([1, 2, 3, 4, 5])
arr.dtype
float_arr = arr.astype(np.float64)
float_arr.dtype

arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
print arr
float_arr = arr.astype(np.int32)
print float_arr
#[  3.7  -1.2  -2.6   0.5  12.9  10.1]
#[ 3 -1 -2  0 12 10]


# 指定数据类型
numeric_strings = np.array(['1.25', '9.6', '42'], dtype=np.string_)
print numeric_strings
# 转变成指定类型的数据
numberic_float = numeric_strings.astype(float) 
print numberic_float

#  数据类型对象
a = np.array([[1, 2], [3, 4]])
print a
print a.dtype.byteorder
#  元素个数
print a.dtype.itemsize
#获取指定类型的数组
a = np.arange(7, dtype='f')
print a.dtype
print np.dtype('f')
print np.dtype('d')
print np.dtype('f8')
print np.dtype('Float64')
print np.dtype('f4')
print np.dtype('Float32')

t = np.dtype('Float64')
#查看类型
print t.char
print t.type
print t.str
# 创建自定义数据类型
#由三种数据类型组成
#1. name,str类型,长度40
#2.numitems , int32
#3.price,float32 类型 
t = np.dtype([('name', np.str_, 40), ('numitems', np.int32), ('price', np.float32)])
print t
print t['name']
itemz = np.array([('Meaning of life DVD ', 42, 3.14), ('Butter', 13, 2.72)], dtype=t)

print itemz[0]['name']
print itemz[0]['numitems']
print itemz[0]['price']

#  数组与标量的运算
arr = np.array([[1, 2, 3], [4, 5, 6]])
print arr
print arr * arr
print arr - arr
print arr + arr
print 1/arr
print 'test'
print arr**0.5

# encoding=utf-8
import numpy as np
import sys
from datetime import datetime

b = np.arange(24).reshape(2, 3, 4)
s = slice(None, None, -1)
print b
print b[(s, s, s)]

#  布尔型索引
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
print names
print data
names == 'Bob'
print names == 'Bob'
print data[names == 'Bob']
print data[names == 'Bob', 2:]
print 'data'
print data[names == 'Bob', 0]

print names == 'Bob'
print names != 'Bob'
print 'test'
mask = (names == 'Bob') | (names == 'will')
print mask
print 'test'
print data
print data[data < 0]
print 'test0'
data[names != 'Joe'] < 1
print data
print 'test1'
print names
print data
data[names != 'Joe'] = 7
print data
print 'test2'

# 花式索引
arr = np.empty((8, 4))
print arr
for i in range(8):
    arr[i] = i
print arr

print arr[[4, 3, 0, 6]]
print arr[[-3, -5, -7]]

arr = np.arange(32).reshape((8, 4))
print arr
print 'test3'
print arr[[1, 5, 7, 2]]
print 'test4'
# 获取指定的数据,根据序列
print arr[[1, 5, 7, 2], [0, 3, 1, 2]]
print 'test5'
print arr[[1, 5, 7, 2]]
print arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]
print 'test6'
print arr[[1, 5, 7, 2]]
print arr[[1, 5, 7, 2], [0, 3, 1, 2]]
print 'test7'
print arr
print arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]
print 'test'
data[[1,3]][:,[1,3]]

# encoding=utf-8
import numpy as np

arr = np.arange(32).reshape((8, 4))
print arr
print arr.T

#  改变数组的维度
b = np.arange(24).reshape(2, 3, 4)
print b
#  讲数组转成一维的数组
print b.ravel()
print b.flatten()
#  设置数组的维度参数
b.shape = (6, 4)
print b
print 'test1'
print b.transpose()
print b
b.resize((2, 12))
print b
print 'test'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值