1.

how to write input format in python to accept array of 5 elements separated by space and without any braces surrounded

Answer»

tion:NUMPY has a number of functions for creating arrays. We focus on four (or five or six, depending on how you count!). The first of these, the array function, converts a list to an array: In [1]: a = [0, 0, 1, 4, 7, 16, 31, 64, 127] In [2]: b = array(a) In [3]: b Out[3]: array([  0,  0,  1,  4,  7, 16, 31, 64, 127]) In [4]: C = array([1, 4., -2, 7]) In [5]: c Out[5]: array([ 1., 4., -2., 7.]) Notice that b is an integer array, as it was created from a list of integers. On the other hand, c is a floating point array even though only one of the elements of the list from which it was made was a floating point number. The array function automatically promotes all of the numbers to the type of the most general entry in the list, which in this case is a floating point number. In the case that elements of the list is made up of numbers and strings, all the elements become strings when an array is formed from a list. The second way arrays can be created is using the NumPy linspace or logspace functions. The linspace function creates an array of N evenly spaced points between a starting point and an ending point. The form of the function is linspace(start, stop, N). If the third argument N is omitted, then N=50. In [6]: linspace(0, 10, 5) Out[6]: array([  0. ,  2.5,  5. ,  7.5, 10. ]) The linspace function produced 5 evenly spaced points between 0 and 10 inclusive. NumPy also has a closely related function logspace that produces evenly spaced points on a logarithmically spaced scale. The arguments are the same as those for linspace except that start and stop refer to a power of 10. That is, the array starts at 10^{\mathrm{start}} and ends at 10^{\mathrm{stop}}. In [7]: %precision 1       # display only 1 digit after decimal Out[7]: u'%.1f' In [8]: logspace(1, 3, 5) Out[8]: array([   10. ,   31.6,  100. ,  316.2, 1000. ]) The logspace function created an array with 5 points evenly spaced on a logarithmic axis starting at 10^1 and ending at 10^3. The logspace function is particularly useful when you want to create a log-log plot. The third way arrays can be created is using the NumPy arange function, which is similar to the Python range function for creating LISTS. The form of the function is arange(start, stop, step). If the third argument is omitted step=1. If the first and third arguments are omitted, then start=0 and step=1. In [9]: arange(0, 10, 2) Out[9]: array([0, 2, 4, 6, 8]) In [10]: arange(0., 10, 2) Out[10]: array([ 0., 2., 4., 6., 8.]) In [11]: arange(0, 10, 1.5) Out[11]: array([ 0. , 1.5, 3. , 4.5, 6. , 7.5, 9. ]) The arange function produces points evenly spaced between 0 and 10 exclusive of the final point. Notice that arange produces an integer array in the first case but a floating point array in the other two cases. In general arange produces an integer array if the arguments are all integers; making any one of the arguments a float causes the array that is created to be a float. A fourth way to create an array is with the zeros and ones functions. As their names imply, they create arrays where all the elements are either zeros or ones. They each take on mandatory argument, the number of elements in the array, and one optional argument that specifies the data type of the array. Left unspecified, the data type is a float. Here are three EXAMPLES In [12]: zeros(6) Out[12]: array([ 0., 0., 0., 0., 0., 0.]) In [13]ones(8) Out[13]: array([ 1., 1., 1., 1., 1., 1., 1., 1.]) In [14]ones(8, dtype=int) Out[14]: array([1, 1, 1, 1, 1, 1, 1, 1]) Recap of ways to create a 1-d array array(a): Creates an array from the list a. linspace(start, stop, num): Returns num evenly spaced numbers over an interval from start to stop inclusive. [num=50 if omitted.] logspace(start, stop, num): Returns num logarithmically spaced numbers over an interval from 10^{\mathrm{start}} to 10^{\mathrm{stop}} inclusive. [num=50 if omitted.] arange([start,] stop[, step,], dtype=None): Returns data points from start to END, exclusive, evenly spaced by step. [step=1 if omitted. start=0 and step=1 if both are omitted.] zeros(num, dtype=float): Returns an an array of 0s with num elements. Optional dtype argument can be used to set data type; left unspecified, a float array is made. ones(num, dtype=float): Returns an an array of 1s with num elements. Optional dtype argument can be used to set data type; left unspecified, a float array is made.



Discussion

No Comment Found