In python3, the value returned by multiple calls to hexgigest is different - python-3.x

In python3, the value returned by multiple calls to hexgigest is different.why, I don't understand?thanks
In [1]: import hashlib
In [2]: s=hashlib.sha1()
In [3]: s.update('v3'.encode('utf8'))
In [4]: s.hexdigest()
Out[4]: 'c5e31d5915661de4393e3f1489b00ebc4497dd48'
In [5]: s.update('v3'.encode('utf8'))
In [6]: s.hexdigest()
Out[6]: '478fb161514c3e8b395c9968e042ab214a98d0d8'

Please read the docs:
Repeated calls [to update] are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b).

Related

How to create an empty Series on pandas?

I'm learning about Series and I'm using VS Code to do excercises to learn it's usage, but when I typed this
current_series_add = pd.Series()
in the terminal it shows me a message telling me that "Te default dtype for empty Series will be object instead of float64 in a future version"
How can I specify a dtype?
As the docs say:
class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)[source]ΒΆ
...
dtype : str, numpy.dtype, or ExtensionDtype, optional
Data type for the output Series. If not specified, this will be
inferred from data. See the user guide for more usages.
Example:
In [1]: import pandas as pd
In [2]: pd.Series(dtype=int)
Out[2]: Series([], dtype: int64)

Retrieve ecoinvent uuid in activity browser / brightway

Is it possible to retrieve the UUID of a ecoinvent activity in activity browser?
If not, can i access an LCA instantiated in ab through a brightway jupyter notebook to find this data?
In [1]: import bw2data as bd
In [2]: bd.projects.set_current("ecoinvent 3.7.1 bw2")
In [3]: ei = bd.Database("ecoinvent 3.7.1")
In [4]: a = ei.random()
In [5]: a['activity']
Out[5]: 'a9ef6142-b382-486d-ae2b-7c1b731a9efe'
In [6]: a['flow']
Out[6]: '45fbbc41-7ae9-46cc-bb31-abfa11e69de0'
In [7]: a['filename']
Out[7]: 'a9ef6142-b382-486d-ae2b-7c1b731a9efe_45fbbc41-7ae9-46cc-bb31-abfa11e69de0.spold'

How to identify error with scipy.stats.chisquare returns negative values?

I am using spyder 3.1.3 with python 3.6.8 under window 10, having scipy 1.2.1. I want to get the chisquare value but notice there is negative values returned. Why is that?
from scipy.stats import chisquare
chisquare(f_obs=[2,1], f_exp=[100000,1])
#Power_divergenceResult(statistic=14096.65412, pvalue=0.0)
but
chisquare(f_obs=[2,1], f_exp=[1000000,1])
#Power_divergenceResult(statistic=-731.379964, pvalue=1.0)
Is there an upperbound for expect values in chisquare? Thanks.
On Windows, the default integer type for numpy arrays is 32 bit. I can reproduce the problem by passing numpy arrays with dtype np.int32 to chisquare:
In [5]: chisquare(f_obs=np.array([2,1], dtype=np.int32), f_exp=np.array([1000000,1], dtype=np.int32))
Out[5]: Power_divergenceResult(statistic=-731.379964, pvalue=1.0)
This is a bug. I created an issue for this on the SciPy github site: https://github.com/scipy/scipy/issues/10159
To work around the problem, convert the input arguments to arrays with data type numpy.int64 or numpy.float64:
In [6]: chisquare(f_obs=np.array([2,1], dtype=np.int64), f_exp=np.array([1000000,1], dtype=np.int64))
Out[6]: Power_divergenceResult(statistic=999996.000004, pvalue=0.0)
In [7]: chisquare(f_obs=np.array([2,1], dtype=np.float64), f_exp=np.array([1000000,1], dtype=np.float64))
Out[7]: Power_divergenceResult(statistic=999996.000004, pvalue=0.0)

Import Math Module - Python

I am running some basic python functions. When I try to the import math module it doesn't seem to work properly. This is the code I received.
In [5]: import math
In [6]: 2^3
...: (2)^3
...: 2^(3)
...: 2**3
...: improt math
File "<ipython-input-6-92c86fb66a5e>", line 5
improt math
^
SyntaxError: invalid syntax
In [7]2**3
Out [7]: 8
In [8]: import math
...: math.sqrt (81)
...:
...: math
...: math .sqrt(81)
...: sqrt(81)
...: 2**3
I haven't configured the program or done anything beside play around with basic functions. But it isn't importing the math module. Any idea why?
You have a typo - improt on line 5
You can also import as
from math import *
Then you can use any mathematical function without prefixing math. e.g.
sqrt(81)
I forgot to add the first line which didn't have the typo. Nonetheless the import math function is working properly suddenly. Don't know why it was buggin

Python set literals didn't work on numpy unique array

I tried to use python set literals on numpy unique like
import numpy as np
# col_value_series is a series of strings
# it doesn't run
uniques = {np.unique(col_value_series)}
# it works
uniques = set(np.unique(col_value_series))
I have to use set instead of {} to get a set of unique values, so what's the different? Are they not the same?
No, they are not the same.
{np.unique(col_value_series)} (or, generally, {x}) is a set of one element. For this to work, the element x must be hashable [glossary], which it is not. Consider:
In [1]: import numpy as np
In [2]: {np.array([1])}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-f73a363763ae> in <module>()
----> 1 {np.array([1])}
TypeError: unhashable type: 'numpy.ndarray'
set(np.unique(col_value_series)) (or set(x)) is a set of elements taken from an iterable x. For this to work, the argument x must be iterable, and its elements must be hashable. These conditions are met, so it works.
In [3]: set(np.array([1]))
Out[3]: {1}

Resources