Numpy初级学习笔记

https://www.machinelearningplus.com/numpy-tutorial-part1-array-python-examples/

1.list是基本操作单元,array是向量化操作,可以将list转成arry,也可以将array转成list

array1=np.array(list1)
list1=arr1d_obj.tolist()
print(type(array1))

array1是属于numpy.ndarray,而其中的元素的数据类型是

print('datetype:',array1.dtype)

其实array就相当于矩阵,对矩阵的各种处理在array中都有对应的操作

例如:取出特定位置的元素(取出前两行前两列):

array[:2,:2]

而用list无法得到。

print("Column wise minimum: ", np.amin(arr2d_f, axis=0))
print("Row wise minimum: ", np.amin(arr2d_f, axis=1))

得到每行、每列中的最小值

2.可以将数据类型转换

arr2d_f=arr2d_f.astype('int').astype('str')

而且array中所有的数据必须是同一类型

当不知道元素到底是什么类型时,可以用object

arr1d_obj = np.array([1, 'a'], dtype='object')

3.假设得到一个未知向量,要知道哪些信息

4.最好使用copy得到原array的元素,否则,对new array的修改会波及array中的元素

arr2b = arr2d_f[:2, :2].copy()

拷贝arr2d_f的前两行两列给arr2b,并且此时改变arr2b的值不影响原数组的值

5.对于要做的规律数组,不用手动计算间隔值

print(np.linspace(start=1, stop=50, num=10, dtype=int))

起始为1,终止50,中间有10个数,数据类型为int,自动输出

zeros和ones输出全是01的矩阵:

print(np.zeros([2,2]))

6.生成随机数:

If you want to repeat the same set of random numbers every time, you need to set the seed or the random state. The see can be any value. The only requirement is you must set the seed to the same value every time you want to generate the same set of random numbers.

Once np.random.RandomState is created, all the functions of the np.random module becomes available to the created randomstate object.

如果你想每次重复同一组随机数,你需要设置种子或随机状态。 看到的可以是任何值。 唯一的要求是您必须在每次要生成相同的一组随机数时将种子设置为相同的值。

rn = np.random.RandomState(100)

# Create random numbers between [0,1) of shape 2,2
print(rn.rand(2,2))
np.random.seed(100)
print(np.random.rand(2,2))

这个学到了,今天一下午就没白费

7.对array中的数组进行计数,第一个uniqs是元素,counts是元素出现的个数,这也是很重要的啊,最开始做的查看是否重复也可以这么做

uniqs, counts = np.unique(array, return_counts=True)
print("Unique items : ", uniqs)
print("Counts       : ", counts)

如果对你有所帮助,谢谢您的鼓励^_^

红包还不收?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值