TypeError: fmin_() missing 1 required positional argument: 'pp'
Résolu
Meve
-
Meve -
Meve -
Bonjour,
[Débutante]
J'essaie d'approximer la valeur d'une liste de moyennes centrées sur la première valeur d'une liste de probabilités.
Exemple : probabilités = [0.90, 0.019, 0.077, 1.0902e-04]
moyennes hypothétiques= [0.000, -2.747, -1.985, -4.679]
Pour cela, je souhaite utiliser la fonction scipy.optimize.fmin
Seulement, mon code renvoie un message d'erreur à la ligne d'appel de la fonction (avant dernière ligne du morceau de code copié ici). Le message est: TypeError: minimize() missing 1 required positional argument: 'pp'
Je crois que cela vient du fait que la fonction fmin ne comprend pas que l'entrée 'pp' de minimize() est ce qu'elle devait générer et minimiser elle-même. fmin s'attend donc à ce que je passe deux arguments pour minimize(). J'aimerais comprendre pourquoi cela se produit et comment régler cela.
[Débutante]
J'essaie d'approximer la valeur d'une liste de moyennes centrées sur la première valeur d'une liste de probabilités.
Exemple : probabilités = [0.90, 0.019, 0.077, 1.0902e-04]
moyennes hypothétiques= [0.000, -2.747, -1.985, -4.679]
Pour cela, je souhaite utiliser la fonction scipy.optimize.fmin
Seulement, mon code renvoie un message d'erreur à la ligne d'appel de la fonction (avant dernière ligne du morceau de code copié ici). Le message est: TypeError: minimize() missing 1 required positional argument: 'pp'
initguess_oblique = [0.000, -2.747, -1.985, -4.679] initguess_centrect= [0.000, -2.820, -2.822, -6.586] initguess_rhombic= [0.000, 0.000, -1.185, -9.313] initguess_hexa= [0.000, 0.000, 0.000, -7.996] #Fit the mus from the probabilities (depending on the alpha value of each participant). def mu2pr(mu_list): '''Gives the probabilities estimated from the found mu''' my_mu_lst= [0.0]+ mu_list mu_nb=len(my_mu_lst) prob_lst= np.zeros((1,4)) max_inds=[] for i in range(simul_nb): simul_mat= np.array(my_mu_lst)+np.random.randn(mu_nb) max_inds.append(np.argmax(simul_mat)) for probindex in range(mu_nb-1): prob_lst[0,probindex]= max_inds.count(probindex)/ simul_nb return prob_lst def minimize(real_prob,pp): ''' We try to get the minimize the difference between the computed probabilities and the probabilities we get from the estimated mus (mu2pr)''' mu2pr(pp) errfunc= sum(((real_prob - mu2pr(pp)) **2)) return errfunc mus_all_participants= np.zeros((len(alpha_all_participants),nstim,number_orientations)) for index in range(len(probs_all_participants)): prob_oblique= probs_all_participants[index,0] prob_centrect= probs_all_participants[index,1] prob_rhombic= probs_all_participants[index,2] prob_hexa= probs_all_participants[index,3] p1ob= fmin(minimize,initguess_oblique,args=prob_oblique) oblique_mus= [0.0]+ p1ob
Je crois que cela vient du fait que la fonction fmin ne comprend pas que l'entrée 'pp' de minimize() est ce qu'elle devait générer et minimiser elle-même. fmin s'attend donc à ce que je passe deux arguments pour minimize(). J'aimerais comprendre pourquoi cela se produit et comment régler cela.
EDIT : Ajout du LANGAGE dans les balises de code (la coloration syntaxique).
Explications disponibles ici : ICI Merci d'y penser dans tes prochains messages. |
A voir également:
- Missing one positional argument python
- Typeerror: can't multiply sequence by non-int of type 'str' - Forum Python
- TypeError: 'int' object is not iterable ✓ - Forum Python
- TypeError: 'NoneType' object is not subscriptable - Forum Python
- TypeError: 'float' object is not subscriptable - Forum Python
- Uncaught TypeError: document.getElementsById is not a functi ✓ - Forum Javascript
3 réponses
Je n'ai pas donné le code en entier, le voilà (il ne pourra tout de même pas être exécuté car je fais appel à des variables et fonctions contenues dans d'autres fichiers):
import numpy as np
import pandas as pd
import seaborn as sn
from math import pi
from math import sqrt
from math import cos
from scipy.stats import norm
from scipy.optimize import fmin
from Fit_Alpha import attraction_function
from Fit_Alpha import perception_probability
from Compute_confidence import alpha_all_participants
#Basic parameters
a=1
bs = [1.5,1.5,1,1];
nstim = len(bs)
number_orientations=4
gammas = [60,70.53,75,60]
gamma_rad =[]
for i in gammas:
gamma_rad.append(i*pi/180)
# To compute f_vectors and p_vectors
array_orientations= np.zeros((nstim,number_orientations))
array_orientations[:,0]=a
for i in range(nstim):
b = bs[i]
gam = gamma_rad[i]
c = sqrt((a**2)+(b**2)-(2*a*b*cos(gam)))
d = sqrt((a**2)+(b**2)+(2*a*b*cos(gam)))
array_orientations[i,1]=b
array_orientations[i,2]=c
array_orientations[i,3]=d
#Get the probabilities to see each vector for each participant's alpha and store this into an array.
probs_all_participants= np.zeros((len(alpha_all_participants),nstim,number_orientations))
index= 0
for alpha in alpha_all_participants:
f_vectors = np.zeros((nstim,4))
p_vectors = np.zeros((nstim,4))
for i in range(nstim):
for j in range(number_orientations):
f_vectors[i,j]=attraction_function(array_orientations[i,j],a,alpha)
for i in range(nstim):
for j in range(number_orientations):
p_vectors[i,j]= perception_probability(f_vectors[i,j],
f_vectors[i,0],
f_vectors[i,1],
f_vectors[i,2],
f_vectors[i,3])
probs_all_participants[index]=p_vectors
index+=1
simul_nb= 1000
#Initial guess of the mus for the b, c and d vectors
initguess_oblique = [0.000, -2.747, -1.985, -4.679]
initguess_centrect= [0.000, -2.820, -2.822, -6.586]
initguess_rhombic= [0.000, 0.000, -1.185, -9.313]
initguess_hexa= [0.000, 0.000, 0.000, -7.996]
#Fit the mus from the probabilities (depending on the alpha value of each participant).
def mu2pr(mu_list):
'''Gives the probabilities estimated from the found mu'''
my_mu_lst= [0.0]+ mu_list
mu_nb=len(my_mu_lst)
prob_lst= np.zeros((1,4))
max_inds=[]
for i in range(simul_nb):
simul_mat= np.array(my_mu_lst)+np.random.randn(mu_nb)
max_inds.append(np.argmax(simul_mat))
for probindex in range(mu_nb-1):
prob_lst[0,probindex]= max_inds.count(probindex)/ simul_nb
return prob_lst
def minimize(real_prob,pp):
''' We try to get the minimize the difference between the computed
probabilities and the probabilities we get from the estimated mus (mu2pr)'''
mu2pr(pp)
errfunc= sum(((real_prob - mu2pr(pp)) **2))
return errfunc
mus_all_participants= np.zeros((len(alpha_all_participants),nstim,number_orientations))
for index in range(len(probs_all_participants)):
prob_oblique= probs_all_participants[index,0]
prob_centrect= probs_all_participants[index,1]
prob_rhombic= probs_all_participants[index,2]
prob_hexa= probs_all_participants[index,3]
p1ob= fmin(minimize,initguess_oblique,args=prob_oblique)
oblique_mus= [0.0]+ p1ob
p1cent= fmin(minimize,initguess_centrect,args=prob_centrect)
centrect_mus= [0.0]+ p1cent
p1rho= fmin(minimize,initguess_rhombic,args=prob_rhombic)
rhombic_mus= [0.0]+ p1rho
p1hex= fmin(minimize,initguess_hexa,args=prob_hexa)
hexa_mus= [0.0]+ p1hex
il serait alors préférable de fournir un programme testable.
un exemple d'appel de fmin, avec passage d'un second paramètre:
c'est le premier paramètre pour lequel fmin cherche une valeur minimisant la fonction. tu devrais donc probablement inverser les paramètres dans la déclaration de minimize().
un exemple d'appel de fmin, avec passage d'un second paramètre:
import scipy.optimize def f(x,y): return abs(x-2*y) trois=3 r=scipy.optimize.fmin(f,123,args=(trois,)) print("r:",r)
c'est le premier paramètre pour lequel fmin cherche une valeur minimisant la fonction. tu devrais donc probablement inverser les paramètres dans la déclaration de minimize().