numpy学习(三)

拥有回忆 提交于 2020-12-14 04:51:55

练习篇(Part 3)

31. 略

32. Is the following expressions true? (★☆☆)

1 np.sqrt(-1) == np.emath.sqrt(-1)
1 print(np.sqrt(-1) == np.emath.sqrt(-1))

运行结果:False

33. How to get the dates of yesterday, today and tomorrow? (★☆☆)

1 yesterday = np.datetime64('today','D') - np.timedelta64(1,'D')
2 today = np.datetime64('today','D')
3 tomorrow = np.datetime64('today','D') + np.timedelta64(1,'D')
4 print("yesterday:"+str(yesterday))
5 print("today:"+str(today))
6 print("tomorrow:"+str(tomorrow))

运行结果:

yesterday:2019-09-24
today:2019-09-25
tomorrow:2019-09-26

34. How to get all the dates corresponding to the month of July 2016? (★★☆)

1 arr = np.arange('2016-07','2016-08',dtype='datetime64[D]')
2 print(arr)

运行结果:

['2016-07-01' '2016-07-02' '2016-07-03' '2016-07-04' '2016-07-05'
'2016-07-06' '2016-07-07' '2016-07-08' '2016-07-09' '2016-07-10'
'2016-07-11' '2016-07-12' '2016-07-13' '2016-07-14' '2016-07-15'
'2016-07-16' '2016-07-17' '2016-07-18' '2016-07-19' '2016-07-20'
'2016-07-21' '2016-07-22' '2016-07-23' '2016-07-24' '2016-07-25'
'2016-07-26' '2016-07-27' '2016-07-28' '2016-07-29' '2016-07-30'
'2016-07-31']

35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)

1 arr1 = np.random.random((3,3))
2 arr2 = np.random.random((3,3))
3 print(arr1)
4 print(arr2)
5 arr3 = np.multiply(np.add(arr1,arr2),np.negative(np.divide(arr1,2)))
6 print(arr3)

运行结果:

[[0.93844098 0.64468962 0.39723495]
[0.40210752 0.55750482 0.00350184]
[0.09511603 0.95997034 0.77923869]]
[[0.94571561 0.30103345 0.4198415 ]
[0.88062036 0.38437861 0.28678044]
[0.57298281 0.24126303 0.89882227]]
[[-8.84084874e-01 -3.04848926e-01 -1.62285662e-01]
[-2.57897263e-01 -2.62552273e-01 -5.08260388e-04]
[-3.17734528e-02 -5.76574205e-01 -6.53805017e-01]]

36. Extract the integer part of a random array using 5 different methods(★★☆)

1 arr = np.random.uniform(3,8,10)
2 print(arr)
3 print(np.trunc(arr))
4 print(arr - arr%1)
5 print(np.floor(arr))
6 print(np.ceil(arr)-1)
7 print(arr.astype(int))

运行结果:

[7.31488564 7.18687183 6.17100343 4.79264848 4.71726774 5.95315196
5.29135106 4.35113601 4.78410156 4.56738764]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7 7 6 4 4 5 5 4 4 4]

37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)

1 arr = np.zeros((5,5))
2 arr += np.arange(5)
3 print(arr)

运行结果:

[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]

38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)

1 def generate():
2     for x in range(10):
3         yield x
4 arr = np.fromiter(generate(),dtype=float,count=-1)
5 print(arr)

运行结果:[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]

39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)

1 arr = np.linspace(0,1,11,endpoint=False)[1:]
2 print(arr)

运行结果:[0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091]

40. Create a random vector of size 10 and sort it (★★☆)

1 arr = np.random.randint(1,20,10)
2 print(arr)
3 print(np.sort(arr))

运行结果:

[ 2 15 13 14 16 18 8 18 1 8]
[ 1 2 8 8 13 14 15 16 18 18]

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!