「百練」成鋼:NumPy 100練

2021-01-18 Python大本營


整理 | Jane

出品 | Python大本營


在之前的練習中,營長為大家準備了 Python 入門學習、100+編程題,Python 開源項目 Top100、80+ Python 項目和 100+ 本 Python 免費書等資源。今天,營長為大家準備的是 Numpy 一百練。


NumPy(Numerical Python)是 Python 語言的一個擴展程序庫。支持大量的數組與矩陣運算,還提供了大量的數學函數庫,是使用 Python 進行科學計算的基礎包。


此外,NumPy 還包含:(1)一個強大的 N 維數組對象;(2)複雜的函數;(3)用於整合 C、C++、Fortran 代碼的工具;(4)可實現線性代數、傅立葉變化、隨機數生成等功能。


NumPy 經常還與 SciPy(Scientific Python)和 Matplotlib(繪圖庫)一起使用,都是要掌握的必備技能。今天就先從 NumPy 學起。


查看官網介紹,或下載安裝,可訪問:


http://www.numpy.org/


所有題目的難度劃分易、中、難,三個級別:


1-33 題,難度:易

34-63 題,難度:中

64-100 題,難度:


下面正式上題:


1. Import the numpy package under the name np


2. Print the numpy version and the configuration


3. Create a null vector of size 10


4. How to find the memory size of any array


5. How to get the documentation of the numpy add function from the command line?


6. Create a null vector of size 10 but the fifth value which is 1


7. Create a vector with values ranging from 10 to 49


8. Reverse a vector (first element becomes last)


9. Create a 3x3 matrix with values ranging from 0 to 8


10. Find indices of non-zero elements from [1,2,0,0,4,0]


11. Create a 3x3 identity matrix


12. Create a 3x3x3 array with random values


13. Create a 10x10 array with random values and find the minimum and maximum values


14. Create a random vector of size 30 and find the mean value


15. Create a 2d array with 1 on the border and 0 inside


16. How to add a border (filled with 0's) around an existing array?


17. What is the result of the following expression?


18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal


19. Create a 8x8 matrix and fill it with a checkerboard pattern


20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?


21. Create a checkerboard 8x8 matrix using the tile function


22. Normalize a 5x5 random matrix


23. Create a custom dtype that describes a color as four unsigned bytes (RGBA)


24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)


25. Given a 1D array, negate all elements which are between 3 and 8, in place.


26. What is the output of the following script?


27. Consider an integer vector Z, which of these expressions are legal?


28. What are the result of the following expressions?


29. How to round away from zero a float array ?


30. How to find common values between two arrays?


31. How to ignore all numpy warnings (not recommended)?


32. Is the following expressions true?


33. How to get the dates of yesterday, today and tomorrow?


34. How to get all the dates corresponding to the month of July 2016?


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


36. Extract the integer part of a random array using 5 different methods


37. Create a 5x5 matrix with row values ranging from 0 to 4


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


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


40. Create a random vector of size 10 and sort it


41. How to sum a small array faster than np.sum?


42. Consider two random array A and B, check if they are equal


43. Make an array immutable (read-only)


44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates


45. Create random vector of size 10 and replace the maximum value by 0


46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area


47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))


48. Print the minimum and maximum representable value for each numpy scalar type


49. How to print all the values of an array?


50. How to find the closest value (to a given scalar) in a vector?


51. Create a structured array representing a position (x,y) and a color (r,g,b)


52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances


53. How to convert a float (32 bits) array into an integer (32 bits) in place?


54. How to read the following file?


55. What is the equivalent of enumerate for numpy arrays?


56. Generate a generic 2D Gaussian-like array


57. How to randomly place p elements in a 2D array?


58. Subtract the mean of each row of a matrix


59. How to I sort an array by the nth column?


60. How to tell if a given 2D array has null columns?


61. Find the nearest value from a given value in an array


62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator?


63. Create an array class that has a name attribute


64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)?


65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)?


66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors


67. Considering a four dimensions array, how to get sum over the last two axis at once?


68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices?


69. How to get the diagonal of a dot product?


70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value?


71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)?


72. How to swap two rows of an array?


73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles


74. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C?


75. How to compute averages using a sliding window over an array?


76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1])


77. How to negate a boolean, or to change the sign of a float inplace?


78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])?


79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])?


80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary)


81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]?


82. Compute a matrix rank


83. How to find the most frequent value in an array?


84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix


85. Create a 2D array subclass such that Z[i,j] == Z[j,i]


86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1))


87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)?


88. How to implement the Game of Life using numpy arrays?


89. How to get the n largest values of an array


90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item)


91. How to create a record array from a regular array?


92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods


93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B?


94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3])


95. Convert a vector of ints into a matrix binary representation


96. Given a two dimensional array, how to extract unique rows?


97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function


98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples ?


99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n.


100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means).



Github 地址:

作者 | Nicolas P. Rougier

https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.md



推薦閱讀:

相關焦點

  • 小學五年級數學100道解方程應用題,給孩子練練,提升成績很有用
    小學五年級數學100道解方程應用題,給孩子練練,提升成績很有用小學五年級處於小學和初中的過渡階段,數學知識的教學是為學生今後漫長的學習生涯奠定基礎,因此,同學們需要在五年級這個階段好好學習,夯實雙基,才能在高年級數學學習過程中,更加如魚得水。
  • 練口才要練什麼?原來是在練這三個人體器官(每日觀察0198)
    練口才要練什麼?原來是在練這三個人體文/樊榮強有人跟我在微信上討教,問練習誦讀朗誦需要多久才可以做到即興演講?這話問得我很無語。我回覆說,這兩者沒有直接關係。我的結論是,要練人體的三個器官。但是首先要強調,練這三個器官,解決的是不同的問題,千萬不可混淆,也不可以認為練一得三。第一,練嘴。口才,毫無疑問跟嘴有關係。狹義的口才,指的就是口齒伶俐。周星馳有部經典電影《九品芝麻官》,候補縣令包龍星目睹妓院老闆娘的罵街吵架功夫了得,便暗中苦練能讓死人活過來的鬥嘴功夫。雖然情節誇張,但是告訴了我們一個道理,口齒伶俐是練出來的。
  • 與冬練三九和與之對應的下聯什麼 冬練三九夏練三伏意思是什麼?
    在民間,還有冬練三九的說法,那麼與「冬練三九」對應的一句俗語是什麼,下一句是什麼?下面我們一起來了解一下吧。與「冬練三九」對應的一句俗語是?這是螞蟻莊園2020年10月23日莊園小課堂的題目,這個問題的答案選項是【百病不生】和【夏練三伏】,大家答對才能獲得180g飼料,不知道螞蟻莊園今日正確答案的朋友,請看小編帶來的螞蟻莊園今日10月23日莊園小課堂答案。螞蟻莊園2020年10月23日問題與「冬練三九」對應的一句俗語是?
  • 健身不練腿,會有什麼後果呢?如何正確練腿?
    尤其是健身新手,他們大都會跳過練腿日,只重視上半身的訓練。而健身老手會重視練腿日,他們 更重視下肢的訓練,而不是只鍛鍊上半身肌群。健身過來人告訴:健身不練腿,就不要說你在健身。練腿在健身中具有重要意義。健身訓練中,任何一個部位都沒有練腿來得更重要。但是,練腿特別痛苦,讓人無比懼怕,練腿後下肢會酸軟無力,走路軟綿綿,需要幾天後才能恢復。這就讓很多新手選擇跳過練腿日。
  • 用這5種方法練肌肉,恐怕練幾年也練不出來吧!
    你想要練出飽滿而又結實的肌肉嗎?你想擁有一身健壯而發達的肌肉嗎?那麼你得考慮系統參加健身了。用運動撕裂自己的肌肉纖維,讓肌肉得到成長和突破。很多人都喜歡肌肉,也都去努力健身了。但是,他們的肌肉怎麼也起不來,你想知道原因嗎?用本文所介紹的5種方法練肌肉,兩年也練不出來!
  • 小學二年級:奧數思維拓展訓練100題,從小給孩子練,數學考100
    今天老師就總結了一套二年級的數學奧數思維拓展訓練題,一共100題,建議家長朋友們可以列印出來,拿給孩子做一做,練一練,對於提升孩子的數學成績,打開孩子的思維都是很有幫助的,趕緊列印一份吧,從小拿給孩子練,數學才能考100分!
  • 《口袋妖怪XY》努力值應該如何練及分配
    在上面兩個的公式裡會看到( Lv / 100 ),也就是現有等級除100,所以說等級越低的時候,不管是努力值或是其他能力值的影響幅度都會比較小,這也是有人常常覺得練了努力值卻覺得沒什麼差別的原因所在,而且努力值早練或晚練也都是沒有影響的,只要其他能力值都一樣,最後的結果是不會變的。
  • 小學生練「字」成段子?掌握「筆畫秘訣」,一旦領悟,進步神速
    講真,小學生真是段子王,隨便練練字,都能成了熱搜段子!怎麼回事呢?一位小學生考試時「員」字屢次寫錯,媽媽一怒之下,罰孩子寫100遍「員」字,媽媽的初衷是一來加強記憶,二來練練字,畢竟現在小學生寫一手漂亮的好字是硬要求。
  • 周一圍楊爍練練辛芷蕾 演技的春天中生代的崛起
    周一圍楊爍練練辛芷蕾 演技的春天中生代的崛起 1905電影網訊 17年底到18年年初,演藝圈風向突變,小鮮肉不再通吃一切
  • 一課一練:三年級上冊數學《米分米釐米毫米的換算和計算》
    (在○裡填上「>」「<」或「=」  )8毫米<8釐米 1米>9分米  5分米>6釐米        5釐米=50毫米  2分米>40毫米100毫米<2分米50毫米<7釐米     12米>90分米    6000毫米<60米        600釐米
  • 空間文,她的空間有劇毒,種毒草靈氣練毒煙,空間修煉百毒不侵!
    空間文,她的空間有劇毒,種毒草靈氣練毒煙,空間修煉百毒不侵!新的一天大家好,歡迎大家再次來到小編的百家號,我們又見面了。今天小編跟大家推薦的是四本空間小說,空間文,她的空間有劇毒,種毒草靈氣練毒煙,空間修煉百毒不侵!希望各位品貌非凡,英俊瀟灑,傾國傾城的小主們能夠喜愛,關注小編,從此不再書荒。
  • 健身先練哪個部位?練得不對,身材不好
    先練胸?還是先練核心?健身還有順序?平常健身到底該從哪個部位開始練?背?胳膊?還是腹肌?一個問題結果引發了一系列的問題在健身的時候都有一個非常嚴重的誤區▼只練想練的部位很多人到了健身房就是做臥推+二頭彎舉+練腹肌就收工回家了,只練身體前側肌群的情形下,肌力與肌肉量會發展不平均而造成:1、身形怪異:上身大下身細
  • 練瑜伽要練對正確的姿勢否則危害很大
    瑜伽是現代女性減肥的首選,很多女孩子為了減肥,為了塑造身型,會選擇去練瑜伽,但是瑜伽是一個很難得健身方式,它需要一定的柔軟性,和一定的耐力,但是有些女性沒有堅持的毅力,常常三天打魚兩個曬網。其實這是很不正確的健身方式。而且練瑜伽一定要講究方式,如果只是靠著自己想練什麼就練什麼,完全沒規劃的去練,也達不到預期想要達到的效果。
  • 如何練腹肌小技巧練腹肌的必備神器
    對於健身小白而言,飛鳥練胸充血極快因而時常感覺良好,練肩重量的循序漸進也容易找到健身的實感,唯獨腹肌的雕塑見效奇慢而過程痛苦,一度爆紅的腹肌撕裂者帶來的真實撕裂感,相信很多人經歷過。不讓你的汗水白流,這些小技巧和輔助裝備助你事半功倍!
  • 舉重運動員的鐵腰是怎麼練的?
    舉重運動員的鐵腰是怎麼練的? 對運動員來說,練腰背能提高成績 對我們來說,練腰背也有數不盡的好處 那麼,舉重運動員的鐵腰到底是怎麼練的
  • 王牌動作硬拉既能練背又能練腿,健身小夥都迷茫了,到底該咋練?
    硬拉是一個練背動作還是練腿動作?到底是應該放在練背日還是練腿日呢?這估計會是一個讓很多健身者都犯難的問題。因為硬拉是一個全身性的動作,它對大腿後側,豎脊肌,握力,大腿前側,背闊肌乃至腹肌的鍛鍊都有著很不錯的效果,可以非常好的刺激這些肌肉。
  • 先練背?還是先練腹?看完本文你就知道今天該練哪個部位了
    先練胸?還是先練腿?什麼時候虐腹?健身訓練到底有沒有個先後順序?我應該從哪個部位開始練?▼浣熊老師身邊的很多朋友,健身都是從自己喜歡的部位開始訓練,或者說,只認真練自己想練的部位,很多兄弟一到健身房就是臥推、彎舉加虐腹。
  • 這份測試卷(共2套)得練練,建議收藏!
    這份測試卷(共2套)得練練,建議收藏!二年級上冊數學,這個學期同學們主要學習了八個單元:長度單位、100以內的加減法、角的初步認識、表內乘法、觀察物體、統計、表內乘法、數學廣角等八個單元。可以說,這八個單元的內容,基本都是二年級上冊知識的重難點。針對二年級的小同學而言,想要全部掌握起來比較費力,而且呢,二年級的知識點還特別特別多!
  • 練了好久都沒變化?練前練後沒區別?是沒找對發力感!
    導語:「練了好久都沒有變化,沒信心,不想練了。」、「怎麼練了一個小時,肌肉一點變化都沒有?」、「明明練的是背部肌群,怎麼手臂那麼酸?」這是不是大部分新手都有的問題與苦惱?該疲累的肌肉毫無感覺,其他不相關的卻酸得不行。健身舉鐵大半年,還是原來瘦弱的樣子。其實都是肌肉發力感錯誤。
  • 不推薦初學者練腹肌輪,不是傳說的中練腹神器嗎?
    原標題:不推薦初學者練腹肌輪,不是傳說的中練腹神器嗎? 腹肌輪真的能夠練腹肌嗎?答案是肯定的。但為什麼不推薦初學者訓練呢?赤裸裸的歧視? 腹肌輪是個爭議很大的小型健身器材,名字雖然誘人,但好似華而不實。