write() takes no keyword arquments in Python [duplicate] - python-3.7

This question already has an answer here:
Getting error: write() takes no keyword arguments
(1 answer)
Closed 3 years ago.
I get this error:
TypeError: write() takes no argument
I tried to write as str(k), but nothing helps
f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
f.write(k,end='')
f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())
I expected the keys and the values of my list. How can I fix this?

f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
f.write(k,'')//why use "end="
f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())
There is no need to use "end=''" in a function, just f.write(k,'')
Or, if you are trying to actually write end='', make sure to put it in quotes:
f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
f.write(k, "end=''")//Make sure to put it in quotes
f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())

This error is due to using end = in function.
See this...
Getting error: write() takes no keyword arguments

Related

TypeError: fit() got an unexpected keyword argument 'epochs'

i'm trying to use NeuralProphet, but get an error with epochs.
my code is:
m = NeuralProphet()
m.fit(price, freq = "M", epochs = 1000)
i get: TypeError: fit() got an unexpected keyword argument 'epochs'.
Anyone know what the problem could be? Tried to follow a tutorial that wrote it like this.
This happens when the function you are calling does not actually take the argument you provide. In this case the "fit" function you are calling does not have "epochs" as its arguments. After having looked into the NeuralProphet code , turns out that it should be something like this :
m = NeuralProphet(epochs = 1000)
m.fit(price, freq = "M")

Operations with function handle in matlab

Could you please help me with the following issue: I have the following function handle:
r1 = #(lambda) b + lambda*(r - b); % r and b are vectors of return data
I want to find the optimal lambdas that set me a mean function to zero, for a given set of powers within that function. What I tried to do and didn't work, as it returns me an error for undefined operators for input arguments of type 'function_handle' is:
lambda0 = 0.3;
for a = 2:10 %power coefficient
S1(a) = fzero(mean((r - b)*r1.^(1/(a - 1))),lambda0);
end
Any suggestion as to how to go about this problem is highly appreciated! Thank you in advance.
fzero accepts a function handle as the first input. As you currently have it, you're trying to pass a statement as the first input. This statement can't even be properly evaluated because you are trying to perform numerical operations on a function handle (more on this in a bit).
You need to instead do something like this where we create a new function handle that evaluates the original function handle and performs the other operations you need.
S1(a) = fzero(#(lambda)mean((r - b)*r1(lambda).^(1/(a - 1))),lambda0);
Further Explanation
Performing operations on a function handle is not the same as performing them on the result.
So for example, if we had a function handle:
func = #(x)2*x;
If we evaluation this, by calling it with an input value for x
func(2)
4
This works as we would expect. If now we really want the value (2*x)^2, we could try to write it the way that you wrote your statement in your question
func2 = func^2;
We will get an error!
Undefined operator '^' for input arguments of type 'function_handle'.
This does not work because MATLAB attempts to apply the ^ operation to the function handle itself and not the value of the evaluated function handle.
Instead, we would need to create a new function handle that essentially wraps the other one and performs any additional options:
func2 = #(x)func(x)^2;
func2(2)
16
Bringing it Full-Circle
So if we go back to your question, you defined your anonymous function r1 like this.
r1 = #(lambda) b + lambda*(r - b); % r and b are vectors of return data
This all looks great. You have one input argument and you reference r and b from the parent workspace.
Now when you call fzero you try to perform operations on this function handle in hopes of creating a new function handle.
mean((r - b)*r1.^(1/(a - 1)))
Like we just showed this will result in a very similar error
Undefined operator .^ for input arguments of type 'function_handle'
So we need to wrap this into a new function.
newfunc = #(lambda)mean((r - b)*r1(lambda).^(1 / (a - 1)));
Now we can safely pass this to fzero.
result = fzero(newfunc, lambda0);

Define a function with a function handler as argument

Is it possible to define a function with a function handler as an argument in Matlab?
I've tried with
function x = name(#f,gh)
but I get an error message stating Invalid syntax at '#'.
You cannot use syntax involving # in function definition. The anonymous function handle would do the work:
function x = SO_Example(h,gh)
x = h(gh);
And you can call the function as follows:
SO_Example(#(a)a.^2 , 2)
ans = 4
Or like this:
h = #(a)a.^2;
SO_Example(h,2)
ans = 4
Please, see comments for additional explanations

Issue: Confusing: Index must be a positive integer or logical

I've got such a problem using MATLAB:
I wrote this function:
function E = f(x, lamda)
E = 1 - exp(-lamda * x);
end
When I write: Prob = f(1000, lamda); where lamda = 3.4274e-004
I get this error:
??? Attempted to access f(1000,0.000341565); index must be a positive integer or logical.
I understand that it requires a positive integer, but why ? I need lamda to be real. What's the problem here ? Can you, please, tell me where I'm wrong?
You have a function f and a variable f declared at the same time. Do clear f; then try your code again. What's happening here is that the variable declaration takes precedence over your function and so doing f would try and access the variable f first.
If you're using f as a variable somewhere and can't change this, then rename your function to be something other than f... perhaps... comp or something. Once you do this, make sure you change your file name so that it's called comp.m, then do:
Prob = comp(1000, lamda);
Your error message indicates that there is a variable called f in your workspace and matlab thinks you are trying to access its elements. Remove the variable f with clear('f') or rename the function to something else and you should be fine.

Matlab says 'find' is not defined in a function [duplicate]

This question already has an answer here:
MATLAB error: "previously appeared to be used as a function or command"
(1 answer)
Closed 8 years ago.
In my command window, I can execute find([0 1 0]), but when I run find in a function, as in x = find([0 1 0]), the compiler tells me that find isn't defined. Why might that be?
The error is:
??? Error: File: frequentTuples.m Line: 12 Column: 21
"find" previously appeared to be used as a function or command, conflicting with its
use here as the name of a variable.
A possible cause of this error is that you forgot to initialize the
variable, or you have initialized it implicitly using load or eval.
and here's the code. The error occurs on the second line of the for loop.
function [ tuples ] = frequentTuples( k, candidates, transactions, min_support )
%FREQUENTTUPLES Get frequent itemsets of size k
% Detailed explanation goes here
candidate_tuple_is_frequent = zeros(size(candidates, 1));
for i = 1:size(candidates, 1)
columns_of_candidate_items = transactions(:, candidates(i, :));
indices_of_transactions_containing_all_items = find(sum(columns_of_candidate_items') == k);
candidate_tuple_is_frequent(i) = size(indices_of_transactions_containing_all_items) >= min_support;
end
tuples = candidates(find(candidate_tuple_is_frequent, :));
end
Ah, I see your problem now. You have a misplaced bracket on line 13. You have
tuples = candidates(find(candidate_tuple_is_frequent, :));
When you should have
tuples = candidates(find(candidate_tuple_is_frequent), :);
You're trying to call find(candidate_tuple_is_frequent, :), which is trying to treat find as a variable. This means that any other call to find in the function will treat it as a variable, hence your error.