python numpy 笔记(三)

NumPy Linear Algebra

1. Write a NumPy program to compute the condition number of a given matrix.

np.linalg.cond(x)

2. Write a NumPy program to compute the condition number of a given matrix.

np.linalg.norm(x)

3. Write a NumPy program compute the inverse of a given matrix.

np.linalg.inv(x)

4. Write a NumPy program compute the sum of the diagonal element of a given array.

np.trace(x)

NumPy Random

1. Write a NumPy program to shuffle numbers between 0 and 10 (inclusive).

import numpy as np
x = np.arange(10)
np.random.shuffle(x)
print(x)
print("Same result using permutation():")
print(np.random.permutation(10))

2. Write a NumPy program to find point by point distances of a random vector with shape (10,2) representing coordinates.

import numpy as np
a= np.random.random((10,2))
x,y = np.atleast_2d(a[:,0], a[:,1])
d = np.sqrt( (x-x.T)**2 + (y-y.T)**2)
print(d)
10*10 matrix

3. Write a NumPy program to find the most frequent value in an array.

np.bincount(x).argmax()
np.bincount()只能针对非负整数序列,返回从0开始直至最大数字的每个数字的出现次数

NumPy Sorting and Searching

Signature: np.sort(a, axis=-1, kind=None, order=None)
Docstring:
Return a sorted copy of an array.

Parameters
----------
a : array_like
    Array to be sorted.
axis : int or None, optional
    Axis along which to sort. If None, the array is flattened before
    sorting. The default is -1, which sorts along the last axis.
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
    Sorting algorithm. The default is 'quicksort'. Note that both 'stable'
    and 'mergesort' use timsort or radix sort under the covers and, in general,
    the actual implementation will vary with data type. The 'mergesort' option
    is retained for backwards compatibility.

    .. versionchanged:: 1.15.0.
       The 'stable' option was added.

order : str or list of str, optional
    When `a` is an array with fields defined, this argument specifies
    which fields to compare first, second, etc.  A single field can
    be specified as a string, and not all fields need be specified,
    but unspecified fields will still be used, in the order in which
    they come up in the dtype, to break ties.
 

Write a NumPy program to sort a given complex array using the real part first, then the imaginary part.

import numpy as np
complex_num = [1 + 2j, 3 - 1j, 3 - 2j, 4 - 3j, 3 + 5j]
print("Original array:")
print(complex_num)
print("\nSorted a given complex array using the real part first, then the imaginary part.")
print(np.sort_complex(complex_num))

Write a NumPy program to sort the specified number of elements from beginning of a given array.

import numpy as np
nums =  np.random.rand(10)
print("Original array:")
print(nums)
print("\nSorted first 5 elements:")
print(nums[np.argpartition(nums,range(5))])

Mathematics

1. Write a NumPy program to get the element-wise remainder of an array of division.

import numpy as np
x = np.arange(7)
print("Original array:")
print(x)
print("Element-wise remainder of division:")
print(np.remainder(x, 5))

Original array:                                                        
[0 1 2 3 4 5 6]                                                        
Element-wise remainder of division:                                    
[0 1 2 3 4 0 1]

2. Write a NumPy program to round elements of the array to the nearest integer.

import numpy as np
x = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print("Original array:")
print(x)
x = np.rint(x)
print("Round elements of the array to the nearest integer:")
print(x)

Original array:                                                        
[-0.7 -1.5 -1.7  0.3  1.5  1.8  2. ]                                   
Round elements of the array to the nearest integer:                    
[-1. -2. -2.  0.  2.  2.  2.]

3. Write a NumPy program to find the roots of the following polynomials.

a) x2 − 4x + 7.
b) x4 − 11x3 + 9x2 + 11x – 10

import numpy as np
print("Roots of the first polynomial:")
print(np.roots([1, -4, 7]))
print("Roots of the second polynomial:")
print(np.roots([1, -11, 9, 11, -10]))

4.Write a NumPy program to compute the following polynomial values.

a) x2 − 2x + 1, when x = 2
b) x4 − 12x3 + 10x2 + 7x – 10, when x = 3

import numpy as np
print("Polynomial value when x = 2:")
print(np.polyval([1, -2, 1], 2))
print("Polynomial value when x = 3:")
print(np.polyval([1, -12, 10, 7, -10], 3))

5. Write a NumPy program to add one polynomial to another, subtract one polynomial from another, multiply one polynomial by another and divide one polynomial by another.

from numpy.polynomial import polynomial as P
x = (10,20,30)
y = (30,40,50)
print("Add one polynomial to another:")
print(P.polyadd(x,y))
print("Subtract one polynomial from another:")
print(P.polysub(x,y))
print("Multiply one polynomial by another:")
print(P.polymul(x,y))
print("Divide one polynomial by another:")
print(P.polydiv(x,y))

6. Write a NumPy program to calculate the difference between neighboring elements, element-wise of a given array.

import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Original array: ")
print(x)
print("Difference between neighboring elements, element-wise of the said array.")
print(np.diff(x))

Original array: 
[1 3 5 7 0]
Difference between neighboring elements, element-wise of the said array.
[ 2  2  2 -7]

7. Write a NumPy program to compute an element-wise indication of the sign for all elements in a given array.

np.sign(x)
正返回1,非正返回0

你可能感兴趣的:(python,numpy)