Download Excercises

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python Crash Course
dd 04-09-2013
Hour 6, numpy
1. What is the difference between astype() and cast[]() in terms of object orientation?
2. Create an array with 10 complex numbers with highest precision. Use attribute dtype to check the
type of these numbers
3. Construct a statement that creates 10 numbers between 1 and 10 (both inclusive) of type Complex.
Hint: combine methods linspace() and astype() in one statement.
4. Start an interactive Python session. Import NumPy. Create an array with
a = numpy.arange(20). What are the commands to print the following results:
1. 10
2. [0 1 2 3 4 5 6 7 8 9]
3. [ 5 8 11 14]
4. [0 2 4 6 8]
5. [10 8 6 4 2 0]
6. [19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0]
5. Create an array M with 60 numbers (starting with 0) with arange()!
Give it shape (3,4,5). Print the multidimensional array M and the element M that corresponds to the
number 33.
(e.g. statement: print M... must print 33. What is '...'?)
6. Given the array 'M' in the example above, give the Python statements that store the next slices into
a variable and print the contents of that variable.
1. [5 6 7 8 9]
The second row. Store in variable row2
2. [ 3 8 13 18]
The 4-th column
3. [[ 7 8]
[12 13]]
A 'sub' matrix
4. [[15 16 17 18 19]
[10 11 12 13 14]
[ 5 6 7 8 9]
[ 0 1 2 3 4]]
The original matrix but with the rows in reverse order
Then execute statement: row2[3] = 100 and print the contents of the original matrix M. What
happened and why?
7. Show how we could have made a copy of row2 in the previous question
8. Given the Python statements:
a = numpy.zeros((5,4))
b = numpy.arange(6).reshape(3,2)+10
What is the command to replace data in the center of array a with data in array b using the syntax
with strides. Print the result.
9. Write a script that prompts a user for a number of star masses (in solar mass units). With these
numbers it must print a report with the following statistics using NumPy methods (sum(), min(),
max() etc.):
Total mass
Mass average
Mass standard deviation
Mass median
Maximum and Minimum mass
For your input of masses you could choose to work with a list and its append() method to store the
masses in an input() loop, because you don't know how many masses you want to enter beforehand.
This part of the code is not trivial so find a good solution because you can use it in many
circumstances.
Then convert the list to an array with:
mass = numpy.array(list)
One can avoid the use of a list if you use the r_() concatenate function, but using a list for interactive
input is more efficient and more flexible.
Set your own rules for filtering unrealistic values and list those masses you filtered out.