AttributeError: 'DataFrame' object has no attribute 'as_matrix' - anaconda

I am getting the following error:
AttributeError: 'DataFrame' object has no attribute 'as_matrix'
The code that produces the error:
def plot_fruit_knn(X, y, n_neighbors, weights):
X_mat = X[['height', 'width']].as_matrix()
y_mat = y.as_matrix()
Can anyone help me spot the problem?

as_matrix() is depreceated you can use Dataframe.to_numpy() instead
y_mat = y.to_numpy()

I had the same issue, and I used .to_numpy()
Use
y_mat = y.to_numpy()
instead

Related

AttributeError: 'Image' object has no attribute 'save'

I am following the basic tutorial for RDKit.
p = Chem.MolFromSmiles('[nH]1cnc2cncnc21')
subms = [x for x in ms if x.HasSubstructMatch(p)]
len(subms)
AllChem.Compute2DCoords(p)
for m in subms: AllChem.GenerateDepictionMatching2DStructure(m,p)
img=Draw.MolsToGridImage(subms,molsPerRow=4,subImgSize=(200,200),legends=[x.GetProp("_Name") for x in subms])
img.save('images/cdk2_molgrid.aligned.o.png')
My version looks like this:
NP=pd.read_excel(r'C:\Users\BajMic\NPPics.xlsx', header=0, index_col=False, keep_default_na=True)
NP['mol']=NP.smiles.apply(getMol)
ms= [i for i in NP['mol'] if i is not None]
img=Draw.MolsToGridImage(ms, molsPerRow=10)
img.save('Pic.png')
In both cases, the tutorial and my own code, I get the same error:
AttributeError: 'Image' object has no attribute 'save'
Now, this is confusing, because I was just following a simple tutorial. I looked at other similar topic, but I think my case is much more trivial. What am I missing here?
Tutorial: https://www.rdkit.org/docs/GettingStartedInPython.html
Although in MolsToGridImage() default is returnPNG=False, I have to set it explicit in the function and it works for me.
You should try: img=Draw.MolsToGridImage(ms, molsPerRow=10, returnPNG=False)

Getting a WARNING and ERROR: unexpected keyword argument 'queryset'

Unexpected keyword argument 'queryset' in constructor call [E:unexpected-keyword-arg]
Tried using form_kwargs as shown on stack overflow here:
# How to use the new form_kwargs on an inline formset?
if request.method == "POST":
ctx['formset'] = project_comparison_form_set(
data=request.POST, files=request.FILES, queryset=ctx['projects'])
ctx['data1'] = request.POST.copy
if ctx['formset'].is_valid():
instances = ctx['formset'].save(commit=False)
for project in instances:
project.save()
Getting both a warning and error message in pylint Unexpected keyword argument 'queryset' in constructor call [E:unexpected-keyword-arg]
You don't show where project_comparison_form_set is defined, but i assume that it is a modelformset_factory.
Here, you do not have the queryset argument. If you do want to pass a queryset, you can pass it to the formset and the formset than to the modelformset_factory.
Check the documentation https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/#changing-the-queryset.

Wordcount Nonetype error pyspark-

I am trying to do some text analysis:
def cleaning_text(sentence):
sentence=sentence.lower()
sentence=re.sub('\'','',sentence.strip())
sentence=re.sub('^\d+\/\d+|\s\d+\/\d+|\d+\-\d+\-\d+|\d+\-\w+\-\d+\s\d+\:\d+|\d+\-\w+\-\d+|\d+\/\d+\/\d+\s\d+\:\d+',' ',sentence.strip())# dates removed
sentence=re.sub(r'(.)(\/)(.)',r'\1\3',sentence.strip())
sentence=re.sub("(.*?\//)|(.*?\\\\)|(.*?\\\)|(.*?\/)",' ',sentence.strip())
sentence=re.sub('^\d+','',sentence.strip())
sentence = re.sub('[%s]' % re.escape(string.punctuation),'',sentence.strip())
cleaned=' '.join([w for w in sentence.split() if not len(w)<2 and w not in ('no', 'sc','ln') ])
cleaned=cleaned.strip()
if(len(cleaned)<=1):
return "NA"
else:
return cleaned
org_val=udf(cleaning_text,StringType())
df_new =df.withColumn("cleaned_short_desc", org_val(df["symptom_short_description_"]))
df_new =df_new.withColumn("cleaned_long_desc", org_val(df_new["long_description"]))
longWordsDF = (df_new.select(explode(split('cleaned_long_desc',' ')).alias('word'))
longWordsDF.count()
I get the following error.
File "<stdin>", line 2, in cleaning_text
AttributeError: 'NoneType' object has no attribute 'lower'
I want to perform word counts but any kind of aggregation function is giving me an error.
I tried following things:
sentence=sentence.encode("ascii", "ignore")
Added this statement in the cleaning_text function
df.dropna()
Its still giving the same issue, I do not know how to resolve this issue.
It looks like you have null values in some columns. Add an if at the beginning of cleaning_text function and the error will disappear:
if sentence is None:
return "NA"

'SparkContext' object has no attribute 'textfile'

I tried loading a file by using following code:
textdata = sc.textfile('hdfs://localhost:9000/file.txt')
Error message:
AttributeError: 'SparkContext' object has no attribute 'textfile'
It is sc.textFile(...) with a capital F.
You can inspect the API of SparkContext here.

XPath Xpression for XML-RPC

I have following XMLRPC response
<value><struct>
<member><name>dedicatedAccountID</name><value><i4>1</i4></value></member>
<member><name>dedicatedAccountValue1</name><value><string>0</string></value></member>
<member><name>expiryDate</name><value><dateTime.iso8601>99991231T00:00:00+1200</dateTime.iso8601></value></member>
</struct></value>
<value><struct>
<member><name>dedicatedAccountID</name><value><i4>2</i4></value></member>
<member><name>dedicatedAccountValue1</name><value><string>15635</string></value></member>
<member><name>expiryDate</name><value><dateTime.iso8601>99991231T00:00:00+1200</dateTime.iso8601></value></member>
</struct></value>
<value><struct>
<member><name>dedicatedAccountID</name><value><i4>3</i4></value></member>
<member><name>dedicatedAccountValue1</name><value><string>0</string></value></member>
<member><name>expiryDate</name><value><dateTime.iso8601>99991231T00:00:00+1200</dateTime.iso8601></value></member>
</struct></value>
I want to get value of dedicatedAccountValue1 where member name is dedicatedAccountID its value is 1.
Regards,
imran
//value/struct[member[name = 'dedicatedAccountID' and value/* = '1']]/member[name = 'dedicatedAccountValue1']/value/*
//Struct[member/name='dedicatedAccountID' and
member/value/i4='1']/member[name='dedicatedAccountValue1']/value
Try this. Not tested

Resources