np.linalg.cond(x)
np.linalg.norm(x)
np.linalg.inv(x)
np.trace(x)
import numpy as np
x = np.arange(10)
np.random.shuffle(x)
print(x)
print("Same result using permutation():")
print(np.random.permutation(10))
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
np.bincount(x).argmax()
np.bincount()只能针对非负整数序列,返回从0开始直至最大数字的每个数字的出现次数
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.
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))
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))])
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]
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.]
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]))
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))
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))
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]
np.sign(x)
正返回1,非正返回0