python numpy 2

import numpy as np

# Create a new array from which we will select elements
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])

print(a)  # prints "array([[ 1,  2,  3],
          #                [ 4,  5,  6],
          #                [ 7,  8,  9],
          #                [10, 11, 12]])"

# Create an array of indices
b = np.array([0, 2, 0, 1])
print(b)

# Select one element from each row of a using the indices in b
print(a[np.arange(4), b])  # Prints "[ 1  6  7 11]"

# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10

print(a)  # prints "array([[11,  2,  3],


#######################################################################################
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a > 2)   
print(bool_idx) 
print(a[bool_idx])  # print true
print(a[a > 2])     # Prints "[3 4 5 6]"

##############################Datatypes########################################
print()
x = np.array([1, 2])   # Let numpy choose the datatype
print(x.dtype)         # Prints "int32"

x = np.array([1.0, 2.0])   # Let numpy choose the datatype
print(x.dtype)             # Prints "float64"

x = np.array([1, 2], dtype=np.int64)   # Force a particular datatype
print(x.dtype)       

print("***********************************************")
x1 = np.linspace(1,10)
x2 = np.linspace(1,10,10)
print(x1)
print("len(x1)=", len(x1))
print(x2)
print("len(x2)=", len(x2))
#np.random.rand()#生成生成[0,1)之间随机浮点数
print(np.random.rand())
print(np.random.rand(1))
print(np.random.rand(2, 3))
print(np.random.rand(5))

print()
for i in range(0, 10):
	print(np.random.randn())


# 标准正态分布随机样本
print(np.random.standard_normal(2))
print(np.random.standard_normal((2,3)))	
print(np.random.standard_normal([2,3]))
print(np.random.standard_normal([2,3]).shape)

#随机整数
print(np.random.randint(2))
print(np.random.randint(2,size=5))
print(np.random.randint(2,6))
print(np.random.randint(2,6,size=15))
print(np.random.randint(2,size=(2,3)))
print(np.random.randint(2,6,(2,3)))

print()
list1 = [1,2,3,4,5]
np.random.shuffle(list1)
print(list1)
arr = np.arange(9).reshape(3,3)
print(arr)
print(np.random.shuffle(arr))
print(arr)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值