In code segment, I saw it uses windows function as follows.
test_by_time = Window.partitionBy("testId").orderBy("Time")
My question is that how to view the results of the windows.partitonby, i.e., test_by_time here. I tried to use test_by_time.show(), but the error AttributeError: 'WindowSpec' object has no attribute` 'show' is given.
Related
I am trying to fit quantile regression with statsmodels. The same code that works in my laptop fails in the cloud and says it does not have fit method. But in the documentation, I see it has fit method. What is causing it? I am using it inside zeppelin notebook.
from statsmodels.regression.quantile_regression import QuantReg
from statsmodels.tools.tools import add_constant
X = temp[['X']]
y = temp['y']
X = add_constant(X)
mod = QuantReg(y, X)
res = mod.fit(q = 0.5)
This is the error message I am getting:
AttributeError: 'Interactive' object has no attribute 'fit'
It seems that the variable (mod) might have a namespace conflict internally within the statsmodel. Using a different name for mod variable to mod1 etc might help here.
My code accesses a light sensor via a python request:
address = 'https://api.particle.io/v1/devices/my_device_id/analogvalue'
headers = {'Authorization':'Bearer {0}'.format(access_token)}
vals = requests.get(address, headers=headers)
The code returns the following values:
{"cmd":"VarReturn","name":"analogvalue","result":171,"coreInfo":{"last_app":"","last_heard":"2019-06-13T21:55:57.387Z","connected":true,"last_handshake_at":"2019-06-13T20:51:02.691Z","deviceID":"my_device_id","product_id":6}}
Python tells me that this is a 'requests.models.Response' class and not a dictionary like I thought.
When I try to access the 'result' value, I get error messages. Here are the various ways I have tried along with their error messages.
print(vals[2])
TypeError: 'Response' object does not support indexing
print(vals['result'])
TypeError: 'Response' object is not subscriptable
print(vals[2].json())
TypeError: 'Response' object does not support indexing
print(vals['result'].json())
TypeError: 'Response' object is not subscriptable
I got the last two approaches (.json) from a answer here on stack overflow.
Can anyone tell me how to access this result value or am I going to be forced to use regular expression?
EDIT: With help from Sebastien D I added the following and was able to get the result I was looking for.
import json
new_vals = json.loads(vals.content)
print(new_vals['result'])
Just do :
import json
### your code ###
json.loads(vals.content)
I have a problem about exporting a savedmodel from an estimator of Tensorflow. My tensorflow program is using an estimator to do the CNN function, where the input is a 2D image. This is my code for the saving part.
def serving_input_rec_fn():
serving_features = {'images': tf.placeholder(shape=[None, self.num_input[0], self.num_input[1]], dtype=tf.float32)}
return tf.estimator.export.build_raw_serving_input_receiver_fn(features=serving_features)
self.model.export_savedmodel(export_dir, serving_input_rec_fn,
strip_default_attrs=True)
But when I ran export_savedmodel function, it produced the following error:
AttributeError: 'function' object has no attribute 'features'
When I checked the code, I actually provided the serving_features here. Could any one help me solve this problem?
you miss a bracket in the argument 'serving_input_rec_fn' passed to export_savedmodel, the right way should be like:
self.model.export_savedmodel(export_dir, **serving_input_rec_fn()**, strip_default_attrs=True)
Because the export_savedmodel api require a 'serving_input_receiver_fn' function, while the value of 'serving_input_rec_fn' is a function: 'serving_input_rec_fn'. You need to call 'serving_input_rec_fn()' which returns tf.estimator.export.build_raw_serving_input_receiver_fn(features=serving_features) which then returns a function: 'serving_input_receiver_fn'.
In method export_savedmodel(),
change
serving_input_rec_fn
to
serving_input_rec_fn()
In serving_input_rec_fn() change:
def serving_input_rec_fn():
...
return tf.estimator.export.build_raw_serving_input_receiver_fn(features=serving_features)
to:
def serving_input_rec_fn():
...
return tf.estimator.export.build_raw_serving_input_receiver_fn(serving_features)
As of documentation (here), build_raw_serving_input_receiver_fn function does not have a features argument
it should be like
def serving_input_rec_fn():
...
tf.estimator.export.build_raw_serving_input_receiver_fn(serving_features)()
I have function for newspaper3k which extract summary for given url. Given as :-
def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text
I have pandas dataframe with column named as url
url
https://www.xyssss.com/dddd
https://www.sbkaksbk.com/shshshs
https://www.ascbackkkc.com/asbbs
............
............
There is another function main_code() which runs perfectly fine and inside which Im using article_summary.I want to add both functions article_summary and main_code() into one function final_code.
Here is my code : 1st function as:-
def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text
Here is 2nd Function
def main_code():
article_data['article']=article_data['url'].apply(article_summary)
return article_data['articles']
When I have done:
def final_code():
article_summary()
main_code()
But final_code() not giving any output it shows as TypeError: article_summary() missing 1 required positional argument: 'row'
Are those the actual URLs you're using? If so, they seem to be causing an ArticleException, I tested your code with some wikipedia pages and it works.
On that note, are you working with just one df? If not, it's probably a good idea to pass it as a variable to the function.
-----------------------------------Edit after comments----------------------------------------------------------------------
I think a tutorial on Python functions will be beneficial. That said, in regards to your specific question, calling a function the way you described it will make it run twice, which is not needed in this case. As I said earlier, you should pass the df as an argument to the function, here is a tutorial on global vs local variables and how to use them.
The error you're getting is because you should pass an argument 'row' to the function article_summary (please see functions tutorial).
Hi I am getting the following error.
TypeError: 'numpy.ndarray' object is not callable
I wrote a function module by myself,like this:
from numpy import *
import operator
def creatDataset() :
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
then,I want to use this function in Microsoft's command window ,I've written some code, as follows:
import KNN
group,labels=KNN.creatDataset()
group()
when I input the code "group()",the error will appear.It's the first time that i describe the question and ask for help, maybe the description is not clear ,,please forgive me.
Since "group" is a numpy.array, you cannot call it like a function.
So "group()" will not work.
I assume, you want to see it's values, so you would have to use something like
"print(group)".