When I try to multiply this by a negative integer it just returns an error
I use:
A = np.array([[1,2,0], [2,4,-2], [0,-2,3]])
From the screenshot, I can see this is homework.
So it asks for the matrix inverse. In maths this is written as A^(-1)
import numpy as np
A = np.array([[1,2,0], [2,4,-2], [0,-2,3]])
np.linalg.inv(A)
array([[-2. , 1.5 , 1. ],
[ 1.5 , -0.75, -0.5 ],
[ 1. , -0.5 , 0. ]])
In numpy, you can not raise integers by negative integer powers (Read this).
In python, the ** operator returns the value without any error.
In [6]: A = 20
In [7]: print(A ** -1)
0.05
You can also use pow(),
In [1]: A = 20
In [2]: pow(20, -1)
Out[2]: 0.05
If you're working with matrices, it's a good idea to ensure that they are instances of the numpy.matrix type rather than the more-generic numpy.ndarray.
import numpy as np
M = np.matrix([[ ... ]])
To convert an existing generic array to a matrix you can also pass it into np.asmatrix().
Once you have a matrix instance M, one way to get the inverse is M.I
To avoid the "integers not allowed" problem, ensure that the dtype of your matrix is floating-point, not integer (specify dtype=float in the call to matrix() or asmatrix())
To Insert power as negative value assume an another variable and name it "pow" and assign that negative value.
Now put below in your code.
pow = -3
value = 5**pow
print(value)
Execute the code and you will see result.
Hope it helps... 🤗🤗🤗
Related
Every idea or suggestion would be appreciated! I have several "the same style" numpy objects(u1,u2,u3...) each of them is :
Object 1:
[[Timestamp('2004-02-28 00:59:16'), 19.9884],
[Timestamp('2004-02-28 01:03:16'), 19.3024],
...
[Timestamp('2004-02-28 01:06:16'), 19.1652]]
Object 2:
[[Timestamp('2004-02-28 01:08:17'), 19.567],
[Timestamp('2004-02-28 01:10:16'), 19.5376],
...
[Timestamp('2004-02-28 01:26:47'), 19.4788]]
I would like to find which of the these objects has the same "trends"in the time series by clustering them. I tried several ways including:
from sklearn.neighbors import NearestNeighbors
X = np.array([u1, u2, u3])
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
distances, indices = nbrs.kneighbors(X)
print(distances)
Some of my errors:
TypeError: float() argument must be a string or a number, not 'Timestamp'
ValueError: setting an array element with a sequence.
TypeError: only size-1 arrays can be converted to Python scalars
Conclusion
Can someone atleast give me a suggestion what should I do. Thanks!
(1) Your first error means that Timestamp must be converted into a string or a number. Just convert them to numbers by .value, which means nanoseconds since Unix epoch time (1970-01-01). Operation in lists:
u1 = list(map(lambda el: (el[0].value / 1e9, el[1]), u1))
u2 = list(map(lambda el: (el[0].value / 1e9, el[1]), u2))
...
(2) np.array([u1, u2, u3]) produces a 3D array instead of the usually expected 2D. This may be the cause of the second error (expected a number but got a sequence instead because of a redundant dimension). Replace this by one of the following:
X = np.array(u1 + u2 + ...) # for lists
X = pd.concat([u1, u2, ...], axis=0) # for dataframes
The revised code can run. Output using your sample data:
[[ 0. 240.00098041]
[ 0. 180.00005229]
[ 0. 121.00066712]
[ 0. 119.00000363]
[ 0. 119.00000363]
[ 0. 991.00000174]]
I have to do some math operations (e.g., add, multiply) on a large array.
To prevent any 'MemoryError' , I am doing my computations as suggested on the answer from this thread.
However, I am running into some trouble while applying the assignment operations as per suggested in the thread. I will demonstrate my problem using a small 3x3 array.
I have the following input array K:
array([[ 0. , 0.51290339, 0.24675368],
[ 0.51290339, 0. , 0.29440921],
[ 0.24675368, 0.29440921, 0. ]])
I want to apply the following computation to the input array K:
output = K* (1.5 - 0.5 * K* K)
I apply the above equation to compute the desired output as follows in Python:
K*= (1.5+np.dot(np.dot(-0.5,K),K))
However, the output answer is not correct.
My desired answer should be:
0.0000000 0.7018904 0.3626184
0.7018904 0.0000000 0.4288546
0.3626184 0.4288546 0.0000000
Any help is welcome.
The difference arises because dot computes the dot product whereas * computes the element-wise product. Try using
K *= 1.5 - 0.5 * K * K
instead.
Addition
Unfortunately, that does not yet solve the memory problems. I would recommend using cython to compute the desired function without allocating extra memory.
# This cython function must be compiled
def evaluate_function_inplace(double[:] values):
cdef int i
for i in range(values.shape[0]):
values[i] *= 1.5 - 0.5 * values[i] * values[i]
Subsequently, you can use the function like so.
K = ...
evaluate_function_inplace(K.ravel())
The K.ravel() call will flatten the array but will not allocate new memory.
Of course, you can also use the above approach without resorting to cython but the performance overhead of iterating over the elements of such a large array in python are very large.
Your problem is that you're actually performing matrices multiplications.
In your case what you want is the following :
K = (np.dot(-0.5,K) * K + 1.5) * K
Try this
K*= (1.5+np.multiply(np.multiply(-0.5,K),K))
It gives output
array([[ 0. , 0.70189037, 0.36261843],
[ 0.70189037, 0. , 0.42885459],
[ 0.36261843, 0.42885459, 0. ]])
say I have a numpy array like this io = np.asarray(['hello world','hello Graz', 'hello all']). Now its shape is io.shape (3,). I would like to perform a split per each element. I know this works splituf = lambda i: np.asarray([item.split(" ",1) for item in i]). Because the real life application will be on much larger array I'd like to avoid the for loop and use vectorized operation.
Any ideas?
Many thanks
There's a collection nu py functions that applies the Python str operations to elemets of an array
http://docs.scipy.org/doc/numpy/reference/routines.char.html
This includes a np.char.split.
In my limited experience these aren't significantly faster than a list comprehension because they still call Python functions, not fast compiled numpyccode.
If the split occurs at the same point in each string , ega[:5],a[5:]`, we might be able to do some dtype conversion.
The result will be 2d, right?
You can used pandas library. It is built uses numpy, providing rich documentation and wonderful operations like pivot, graphs, element-wise operations,... lots of them
Note: pandas is not a replacement for numpy.
Pandas element wise operation
Here is one special case of element wise operation
>>> sam = np.arange(15)
>>> print sam
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
>>> print pd.rolling_apply(sam, 2, lambda x: x[1] - x[0])
[ nan 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
You can use join and re.split
import numpy as np
import re
io = np.asarray(['hello world','hello Graz', 'hello all'])
print(re.split('[ -]', '-'.join(io)))
import numpy as np
import math
def normalize(array):
mean = sum(array) / len(array)
deviation = [(float(element) - mean)**2 for element in array]
std = math.sqrt(sum(deviation) / len(array))
normalized = [(float(element) - mean)/std for element in array]
numpy_normalized = (array - np.mean(array)) / np.std(array)
print normalized
print numpy_normalized
print ""
normalize([2, 4, 4, 4, 5, 5, 7, 9])
normalize([1, 2])
normalize(range(5))
Outputs:
[-1.5, -0.5, -0.5, -0.5, 0.0, 0.0, 1.0, 2.0]
[-1.5 -0.5 -0.5 -0.5 0. 0. 1. 2. ]
[0.0, 1.414213562373095]
[-1. 1.]
[-1.414213562373095, -0.7071067811865475, 0.0, 0.7071067811865475, 1.414213562373095]
[-1.41421356 -0.70710678 0. 0.70710678 1.41421356]
Can someone explain to me why this code behaves differently in the second example, but similarly in the other two examples?
Did I do anything wrong in the hard coded example? What does NumPy do to end up with [-1, 1]?
As seaotternerd explains, you're using integers. And in Python 2 (unless you from __future__ import division), dividing an integer by an integer gives you an integer.
So, why aren't all three wrong? Well, look at the values. In the first one, the sum is 40 and the len is 8, and 40 / 8 = 5. And in the third one, 10 / 5 = 2. But in the second one, 3 / 2 = 1.5. Which is why only that one gets the wrong answer when you do integer division.
So, why doesn't NumPy also get the second one wrong? NumPy doesn't treat an array of integers as floats, it treats them as integers—print np.array(array).dtype and you'll see int64. However, as the docs for np.mean explain, "float64 intermediate and return values are used for integer inputs". And, although I don't know this for sure, I'd guess they designed it that way specifically to avoid problems like this.
As a side note, if you're interested in taking the mean of floats, there are other problems with just using sum / div. For example, the mean of [1, 2, 1e200, -1e200] really ought to be 0.75, but if you just do sum / div, you're going to get 0. (Why? Well, 1 + 2 + 1e200 == 1e200.) You may want to look at a simple stats library, even if you're not using NumPy, to avoid all these problems. In Python 3 (which would have avoided your problem in the first place), there's one in the stdlib, called statistics; in Python 2, you'll have to go to PyPI.
You aren't converting the numbers in the array to floats when calculating the mean. This isn't a problem for your second or third inputs, because they happen to work out neatly (as explained by #abarnert), but since the second input does not, and is composed exclusively of ints, you end up calculating the mean as 1 when it should be 1.5. This propagates through, resulting in your discrepancy with the results of using NumPy's functions.
If you replace the line where you calculate the mean with this, which forces Python to use float division:
mean = sum(array) / float(len(array))
you will ultimately get [-1, 1] as a result for the second set of inputs, just like NumPy.
I have used numpy's arange function to make the following range:
a = n.arange(0,5,1/2)
This variable works fine by itself, but when I try putting it anywhere in my script I get an error that says
ZeroDivisionError: division by zero
First, your step evaluates to zero (on python 2.x that is). Second, you may want to check np.linspace if you want to use a non-integer step.
Docstring:
arange([start,] stop[, step,], dtype=None)
Return evenly spaced values within a given interval.
[...]
When using a non-integer step, such as 0.1, the results will often not
be consistent. It is better to use ``linspace`` for these cases.
In [1]: import numpy as np
In [2]: 1/2
Out[2]: 0
In [3]: 1/2.
Out[3]: 0.5
In [4]: np.arange(0, 5, 1/2.) # use a float
Out[4]: array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
If you're not using a newer version of python (3.1 or later I think) the expression 1/2 evaluates to zero, since it's assuming integer division.
You can fix this by replacing 1/2 with 1./2 or 0.5, or put from __future__ import division at the top of your script.