i need to Solve the error importing the NumPy library - numpy

#i need to Solve the error importing the NumPy library
arrange = np.arange(15)np.reshape(3, 5)
np.allclose(arange, [[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]]) == True
#The error seems to tell me 'list' object has no attribute 'arange' I have tried
to define arange however it leaves me with syntax. Thank you in advance!

arange = np.arange(15).reshape(3, 5)
np.allclose(arange, [[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]]) == True
it was just a matter of spelling. all fixed. now for another coffee.

Related

print IPOPT parameters

I would like to get some parameters from IPOPT, like for instance,
Using Ipopt
k = barrier_tol_factor
I know this doesn't work. Does anyone now how I can access thoes?
I tried with
Ipopt.barrier_tol_factor
It seems getting the current option value is tricky. But you can set this option and then the value should be known:
Ipopt.AddIpoptNumOption(solver, "barrier_tol_factor", 15.0)
BTW the default value is 10.0.

Replace transformation: What is the right way to do it?

I do this to replace a character in a string:
df['msg'] = df['msg'].str.replace(u'X','W')
And get this warning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Then, I try to do this same transformation the right way (I thought) to avoid that warning:
df.loc[:,'msg'] = df.loc[:,'msg'].str.replace(u'X','W')
But, I am still getting the same warning, even though both codes works fine.
What is the correct way to do this kind of transformation?
This warning can be resolved by using the method copy():
df.loc[:,'msg'] = df['msg'].str.replace(u'X','W').copy()
Or assign()
df = df.assign(msg=df['msg'].str.replace(u'X','W'))

Create a new column based on specific character from existing column fail : 'str' object has no attribute 'str'

I hope you can help me. I'm looking for to classify some product based on the size: 40ML or other.
Here is my piece of code:
1. Dataframe creation
test = {'Name':['ProductA 40ML','ProductB 100ML','ProductC 40ML','ProductD 100ML']}
df1=pd.DataFrame(test)
2. Function built for classification
def size_class(row):
if row['Name'].str.contains('40ML'):
val = '40ML'
else:
val = 'other'
return val
df1['size_classification'] = df1.apply(size_class, axis=1)
Error message:
However the function returns the following error: AttributeError: 'str' object has no attribute 'str'
Question
Would you please be able to help me fix this one? I had a look at existing issues but couldn't find any answer addressing this.
I figure out some things you missed in your implementation:
In Python for most of the cases of membership tests, the operator in is more relevant than contains. Membership test operations documentation, see more details in this SOF question: Does Python have a string 'contains' substring method?
The default of the apply function is to look at the value of specific column, so you don't need to apply it on the whole data frame, but only on the relevant column.
The function applied with 'apply' looks separately on every cell's value. In your case, it's a string so you don't need to cast things.
So, the code that fixes your bugs is:
import pandas as pd
test = {'Name':['ProductA 40ML','ProductB 100ML','ProductC 40ML','ProductD 100ML']}
df1=pd.DataFrame(test)
def size_class(row):
if '40ML' in row:
val = '40ML'
else:
val = 'other'
return val
df1['size_classification'] = df1['Name'].apply(size_class)
print(df1.head())

VS Code - Completion is terrible, is it my setup?

Code completion and intellisense in VS Code is absolutely god-awful for me. In every language. I have extensions installed and updated but its always absolute trash.
import pandas as pd
data_all = pd.read_csv(DATA_FILE, header=None)
data_all. (press tab)
No suggestions.
Do you really not know its a Pandas DataFrame object, its literally the line above?
I have this issue in python, in ruby/rails, pretty much every langauge i try to use the completion is absolute garbage. Do i have an extension that is breaking other extensions? is code jsut this bad? Why is it so inexplicably useless?
Installed Currently:
abusaidm.html-s
nippets#0.2.1
alefragnani.numbered-bookmarks#8.0.2
bmewburn.vscode-intelephense-client#1.6.3
bung87.rails#0.16.11
bung87.vscode-gemfile#0.4.0
castwide.solargraph#0.21.1
CoenraadS.bracket-pair-colorizer#1.0.61
donjayamanne.python-extension-pack#1.6.0
ecmel.vscode-html-css#1.10.2
felixfbecker.php-debug#1.14.9
felixfbecker.php-intellisense#2.3.14
felixfbecker.php-pack#1.0.2
formulahendry.auto-close-tag#0.5.10
golang.go#0.23.2
groksrc.ruby#0.1.0
k--kato.intellij-idea-keybindings#1.4.0
KevinRose.vsc-python-indent#1.12.0
Leopotam.csharpfixformat#0.0.84
magicstack.MagicPython#1.1.0
miguel-savignano.ruby-symbols#0.1.8
ms-dotnettools.csharp#1.23.9
ms-mssql.mssql#1.10.1
ms-python.python#2021.2.636928669
ms-python.vscode-pylance#2021.3.1
ms-toolsai.jupyter#2021.3.619093157
ms-vscode.cpptools#1.2.2
rebornix.ruby#0.28.1
sianglim.slim#0.1.2
VisualStudioExptTeam.vscodeintellicode#1.2.11
wingrunr21.vscode-ruby#0.28.0
Zignd.html-css-class-completion
#1.20.0
If you check the IntelliSense of the read_csv() method (By hovering your mouse over it), you will see that it returns a DataFrame object
(function)
read_csv(reader: IO, sep: str = ...,
#Okay... very long definition but scroll to the end...
float_precision: str | None = ...) -> DataFrame
But if you use IntelliSense check the variable data_all
import pandas as pd
data_all = pd.read_csv(DATA_FILE, header=None)
It is listed as the default data type in python: Any. That's why your compiler isn't generating the autocomplete.
So, you simply need to explicitly tell your compiler that it is, in fact, a DataFrame object as shown.
import pandas as pd
from pandas.core.frame import DataFrame
DATA_FILE = "myfile"
data_all:DataFrame = pd.read_csv(DATA_FILE, header=None)
# Now all autocomplete options on data_all are available!
It might seem strange why the compiler cannot guess the data type in this example until you realize that the read_csv() method is overloaded with many definitions, and some of them return objects as Any type. So the compiler assumes the worst-case scenario and treats it as an Any type object unless specified otherwise.

defining a variable to set length of an array is failing but assert and print works

def count = * print response.teams[0].teamMembers.length throws below error
com.jayway.jsonpath.PathNotFoundException: Expected to find an object
with property ['length'] in path $['teams'][0]['teamMembers'] but
found 'net.minidev.json.JSONArray'.
This is not a json object
according to the JsonProvider:
'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
print response.teams[0].teamMembers.length and
assert response.teams[0].teamMembers.length == 9
are working just fine.
Any help here is much appreciated.
Yes, Karate assumes the right-hand-side as Json-Path (which is fine for 90% of the cases). Use parentheses to force JavaScript evaluation when needed.
Try this:
def count = (response.teams[0].teamMembers.length)
For a detailed explanation, please refer to this section in the documentation: Karate Expressions