I'm having trouble replicating a neural net created in matlab using python. Its a {9,8,4} network. Below is the original output in matlab and python respectively
0.00187283763854096 0.00280257304094145 0.00709416898379967 0.00474275971385824 0.000545071722266366
0.0520122170317888 0.0402746073491970 0.0179208146529717 0.0245726107168336 0.230693355244371
0.430695009441386 0.434492291029203 0.410151021812136 0.416871471927059 0.469873849186641
0.562954025662924 0.539410486293765 0.666336481449288 0.637779009735872 0.284564488176231
[1.0, -1.0, -0.6875955603907775, -0.9999999426232321]
[1.0, -1.0, 0.5569364789701737, -0.9994593106654553]
[1.0, -1.0, 0.5022468075847347, -0.999780120038859]
[1.0, -1.0, 0.4924691499951816, -0.9997110849203137]
[1.0, -1.0, 0.5945295051094253, -0.9991584098381949]
I obtained the input and layered weights using net2.IW{1}, net2.LW{2}. The bias was obtained as follows; net2.b{1} and net2.b{2}.
Without using bias, I got something that looks close:
[-0.6296705512038354, 0.9890465283687858, 0.1368924025968622, 0.5426776395855755]
[-0.05171165478856995, 0.2973298654798701, 0.02897695903082293, 0.0499820714219222]
[-0.10046933055782481, 0.40531232885083035, 0.033067381241777244, 0.06585830703439044]
[0.03167268710874907, 0.5485036035542894, 0.10579223668518502, 0.015475934153332364]
[0.006502829360007152, 0.22928662468119648, 0.03788967208701787, 0.012868192806301859]
Hence I think the problem may lie in the bias; I'm not quite sure though.
Python implementation with weights taken from Matlab
def sigmoid(x):
return math.tanh(x)
def NN(inputs, bias1, bias2):
wsum=[sum([(x*y) for x,y in zip(inputs[0],row)])for row in inputweights]
wsbias=[(x + y) for x,y in zip(wsum,bias1)]
inputactivation=[sigmoid(k) for k in wsbias]
wsoutput=[sum([(x*y) for x,y in zip(inputactivtion,row)])for row in hiddenweights]
wsbias2=[(x + y) for x,y in zip(wsoutput,bias2)]
outputactivation=[sigmoid(k) for k in wsbias2]
return 'output' outputactivation
I would really appreciate any solution that works.
Below are input and Layered weights as well as input and layered bias obtained.
IW=[[-9.1964, -2.3015, 0.2493, 3.3648, -2.6015, -0.0795, -11.2356, 4.6861,-0.8360],
[6.0201, -1.8708, 2.7844, 0.2419, -1.1808, -8.6800, 5.8519, -5.2958, 5.3233],
[0.8597, 0.8644, -0.6913, -0.0397, 0.0619, 0.4506, 1.0687, 0.4090, -0.2874],
[2.9459, 3.2596, 2.2859, 1.1933, 2.9675, -9.6017, 3.5893, 1.4808, -7.5311],
[-0.1533, -1.4806, -2.3748, 0.8059, -0.5502, -1.0447, -0.5920, -1.1667, -1.1447],
[4.7185, -9.2097, 1.1001, -0.0173, 1.4929, 0.3884, 3.7674, 6.3459, -4.2845],
[-16.4031, 8.1351, 2.0689, 2.1267, 6.2093, -8.3875, -15.8493, -0.6096, 2.9214],
[1.7329, 0.1797, 0.1500, 9.1616, -1.7226, 0.9479, 3.2542, -24.4003, -4.2790]]
LW=[[-18.5985, 12.2366, -0.8833, -1.6382, 4.6281, 8.1221, -23.7587, -0.8589],
[12.0462, -11.5464, 6.9612, -10.8562, -7.0647, 5.6653, 16.2527, -7.6119],
[12.4176, 0.9808, 0.7650, -2.9434, -0.2765, -3.0689, -3.1528, 3.0389],
[5.7570, 7.7584, -6.9550, -2.3679, -1.4884, -11.0668, 2.6764, 26.5427]]
bias1=[-1.7639, -1.2599, -0.7560, 0.2520,-0.2520,0.7560, -1.2599, -1.7639]
bias2= [0.2129,-8.1812, 0.0202,4.4512]
My inputs
[[0.0, 0.0, 0.0414444125526, 0.0, 0.0, 0.00670501464516, 0.0, 0.0, 0.0313140652051], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00398243636152, 0.0, 0.0, 0.000863557858377, 0.0, 0.0, 0.00356406423776], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00440892765754, 0.0, 0.0, 0.000725737283104, 0.0, 0.0, 0.00543503005753], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00565322288091, 0.0, 0.0, 0.00236630383341, 0.0, 0.0, 0.00642911490856], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00250332223564, 0.0, 0.0, 0.000926998841251, 0.0, 0.0, 0.00241792804103], [0.0, 0.0, 0.0, 1.0]]
Thanks for your suggestions.
Related
I have the following code for the stack bar chart
cols = ['Bug Prediction','Traceability','Security', 'Program Generation & Repair',
'Performance Prediction','Code Similarity & Clone Detection',
'Code Navigation & Understanding', 'Other_SE']
count_ANN = [2.0,0.0,1.0,0.0,0.0,3.0,5.0,1.0]
count_CNN = [1.0,0.0,5.0,0.0,1.0,4.0,4.0,0.0]
count_RNN = [1.0,0.0,3.0,1.0,0.0,4.0,7.0,2.0]
count_LSTM =[3.0,0.0,5.0,3.0,1.0,9.0,15.0,1.0]
count_GNN = [0.0,0.0,1.0,0.0,0.0,3.0,3.0,3.0]
count_AE = [0.0,0.0,1.0,3.0,0.0,6.0,11.0,0.0]
count_AM = [2.0,0.0,1.0,4.0,1.0,4.0,15.0,1.0]
count_other =[1.0,0.0,2.0,2.0,0.0,1.0,3.0,0.0]
b_RNN = list(np.add(count_ANN,count_CNN))
b_LSTM = list(np.add(np.add(count_ANN,count_CNN),count_RNN))
b_AE = list(np.add(np.add(np.add(count_ANN,count_CNN),count_RNN),count_AE))
b_GNN = list(np.add(b_AE,count_GNN))
b_others = list(np.add(b_GNN,count_other))
plt.bar(cols,count_ANN,0.4,label = "ANN")
plt.bar(cols,count_CNN,0.4,bottom=count_ANN,label = "CNN")
plt.bar(cols,count_RNN,0.4,bottom=b_RNN,label = "RNN")
plt.bar(cols,count_LSTM,0.4,bottom =b_LSTM, label = "LSTM")
plt.bar(cols,count_AE,0.4,bottom=b_AE,label = "Auto-Encoder")
plt.bar(cols,count_GNN,0.4,bottom=b_GNN,label = "GNN")
plt.bar(cols,count_other,0.4,bottom=b_others,label = "Others")
#ax.bar(cols, count)
plt.xticks(np.arange(len(cols))+0.1,cols)
fig.autofmt_xdate()
plt.legend()
plt.show()
Then the output for this is overlapped stacks as in the following figure
The specific problem is that b_AE is calculated wrong. (Also, there is a list called count_AM for which there is no label).
The more general problem, is that calculating all these values "by hand" is very prone to errors and difficult to adapt when there are changes. It helps to write things in a loop.
The magic of numpy's broadcasting and vectorization lets you initialize bottom as a single zero, and then use numpy's adding to add the counts.
To have a bit neater x-axis, you can put the individual words on separate lines. Also, plt.tight_layout() tries to make sure all text fits nicely into the plot.
import matplotlib.pyplot as plt
import numpy as np
cols = ['Bug Prediction', 'Traceability', 'Security', 'Program Generation & Repair',
'Performance Prediction', 'Code Similarity & Clone Detection',
'Code Navigation & Understanding', 'Other_SE']
count_ANN = [2.0, 0.0, 1.0, 0.0, 0.0, 3.0, 5.0, 1.0]
count_CNN = [1.0, 0.0, 5.0, 0.0, 1.0, 4.0, 4.0, 0.0]
count_RNN = [1.0, 0.0, 3.0, 1.0, 0.0, 4.0, 7.0, 2.0]
count_LSTM = [3.0, 0.0, 5.0, 3.0, 1.0, 9.0, 15.0, 1.0]
count_GNN = [0.0, 0.0, 1.0, 0.0, 0.0, 3.0, 3.0, 3.0]
count_AE = [0.0, 0.0, 1.0, 3.0, 0.0, 6.0, 11.0, 0.0]
count_AM = [2.0, 0.0, 1.0, 4.0, 1.0, 4.0, 15.0, 1.0]
count_other = [1.0, 0.0, 2.0, 2.0, 0.0, 1.0, 3.0, 0.0]
all_counts = [count_ANN, count_CNN, count_RNN, count_LSTM, count_GNN, count_AE, count_AM, count_other]
all_labels = ["ANN", "CNN", "RNN", "LSTM", "GNN", "Auto-Encoder", "AM", "Others"]
cols = ["\n".join(c.split(" ")) for c in cols]
cols = [c.replace("&\n", "& ") for c in cols]
bottom = 0
for count_i, label in zip(all_counts, all_labels):
plt.bar(cols, count_i, 0.4, bottom=bottom, label=label)
bottom += np.array(count_i)
# plt.xticks(np.arange(len(cols)) + 0.1, cols)
plt.tick_params(axis='x', labelrotation=45, length=0)
plt.legend()
plt.tight_layout()
plt.show()
PS: To have the bars in the same order as the legend, you could draw them starting from the top:
bottom = np.sum(all_counts, axis=0)
for count_i, label in zip(all_counts, all_labels):
bottom -= np.array(count_i)
plt.bar(cols, count_i, 0.4, bottom=bottom, label=label)
I want to extract let say the 3 max values in a matplotlib histogram.
There are a lot of ways to extract the (unique) max value in a histogram, but I don't find anything about extract the 2-3 or 4 max values in a histogram.
I also want it to be automatic (not specific to the following case).
Here is my data and my code:
from matplotlib.pyplot import *
Angle=[0.0, 0.0, 0.0, 0.0, 1.5526165117219184, 0.0, 1.559560844536934, 0.0, 1.5554129250143014, 1.5529410816553442, 1.5458015331759765, -0.036680787756651845, 0.0, 0.0, 0.0, 0.0, -0.017855245139552514, -0.03224688243525392, 1.5422326689561365, 0.595918005516301, -0.06731387579270513, -0.011627382956383872, 1.5515679276951895, -0.06413211500143158, 0.0, -0.6123221322275954, 0.0, 0.0, 0.13863973713415806, 0.07677189126977804, -0.021735706841792667, 0.0, -0.6099169030770674, 1.546410917622178, 0.0, 0.0, -0.24111767845146836, 0.5961991412974801, 0.014704822377851432]
figure(1,figsize=(16,10))
plt.hist(Angle, bins=100,label='Angle')
show()
plt.hist outputs the bin heights, the bin boundaries and the rectangular patches.
np.argsort can sort the values and use the result to index the other arrays.
The code below imports pyplot as plt because importing it as * can lead to al lot of confusion.
import matplotlib.pyplot as plt
import numpy as np
Angle=[0.0, 0.0, 0.0, 0.0, 1.5526165117219184, 0.0, 1.559560844536934, 0.0, 1.5554129250143014, 1.5529410816553442, 1.5458015331759765, -0.036680787756651845, 0.0, 0.0, 0.0, 0.0, -0.017855245139552514, -0.03224688243525392, 1.5422326689561365, 0.595918005516301, -0.06731387579270513, -0.011627382956383872, 1.5515679276951895, -0.06413211500143158, 0.0, -0.6123221322275954, 0.0, 0.0, 0.13863973713415806, 0.07677189126977804, -0.021735706841792667, 0.0, -0.6099169030770674, 1.546410917622178, 0.0, 0.0, -0.24111767845146836, 0.5961991412974801, 0.014704822377851432]
plt.figure(1,figsize=(10, 6))
values, bins, patches = plt.hist(Angle, bins=30)
order = np.argsort(values)[::-1]
print("4 highest bins:", values[order][:4])
print(" their ranges:", [ (bins[i], bins[i+1]) for i in order[:4]])
for i in order[:4]:
patches[i].set_color('fuchsia')
plt.show()
Output:
4 highest bins: [21. 8. 3. 2.]
their ranges: [(-0.03315333842372081, 0.03924276080176348), (1.4871647453114498, 1.559560844536934), (-0.1055494376492051, -0.03315333842372081), (0.5460154553801537, 0.6184115546056381)]
Another example highlighting the 3 highest bins:
Angle = np.random.normal(np.tile(np.random.uniform(1, 100, 20 ), 100), 5 )
values, bins, patches = plt.hist(Angle, bins=100)
I am looking to plot a network graph using the networkx package in python. The problem I am facing is that the customizations that I'm making are not taking place and the defaults values(probably) are getting used instead. The code I am using is below. It looks lengthy but it most of it setting up the data.
import pandas as pd
import networkx as NX
from matplotlib import pyplot as plt
import numpy as np
import pygraphviz as PG
# the dataframeI'm using
corr_mat_2 = pd.DataFrame.from_dict({'clump_thickness': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.0, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': -0.5790403219346321}, 'cell_size_uniformity': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.9490385801487778, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.5726586033292179, 'bare_nuclei': 0.0, 'bland_chromatin': 0.6533249167391942, 'normal_nucleoli': 0.5106708697857533, 'mitoses': -0.5473028893162575}, 'cell_shape_uniformity': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.9490385801487778, 'cell_shape_uniformity': 0.0, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.502767944815973, 'bare_nuclei': 0.5261228487320817, 'bland_chromatin': 0.631017333346977, 'normal_nucleoli': 0.5115973333620983, 'mitoses': -0.5850744184472585}, 'marginal_adhesion': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.0, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': 0.0}, 'epithelial_cell_size': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.5726586033292179, 'cell_shape_uniformity': 0.502767944815973, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': 0.0}, 'bare_nuclei': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.5261228487320817, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.5522628091390857, 'normal_nucleoli': 0.0, 'mitoses': -0.7437142606374423}, 'bland_chromatin': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.6533249167391942, 'cell_shape_uniformity': 0.631017333346977, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.5522628091390857, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': -0.716623255542893}, 'normal_nucleoli': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.5106708697857533, 'cell_shape_uniformity': 0.5115973333620983, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': 0.0}, 'mitoses': {'clump_thickness': -0.5790403219346321, 'cell_size_uniformity': -0.5473028893162575, 'cell_shape_uniformity': -0.5850744184472585, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': -0.7437142606374423, 'bland_chromatin': -0.716623255542893, 'normal_nucleoli': 0.0, 'mitoses': 0.0}}
)
G = NX.Graph()
# these are the nodes
nodes = ['clump_thickness', 'cell_size_uniformity', 'cell_shape_uniformity', 'epithelial_cell_size', 'bare_nuclei',
'bland_chromatin', 'normal_nucleoli', 'mitoses']
# the following list contains the pairs between which I want to add an edge
pairs = [['bare_nuclei', 'bland_chromatin'], ['bare_nuclei', 'cell_shape_uniformity'],
['bare_nuclei', 'mitoses'], ['bland_chromatin', 'cell_shape_uniformity'],
['bland_chromatin', 'cell_size_uniformity'],
['bland_chromatin', 'mitoses'], ['cell_shape_uniformity', 'cell_size_uniformity'],
['cell_shape_uniformity', 'epithelial_cell_size'], ['cell_shape_uniformity', 'mitoses'],
['cell_shape_uniformity', 'normal_nucleoli'], ['cell_size_uniformity', 'epithelial_cell_size'],
['cell_size_uniformity', 'mitoses']]
# the size of each node depends on the average value of the absolute values of the corresponding column.
# the below is the minimum size
node_default_size = 2
for each_node in nodes:
# the customisation I want for each node
avg_abs_corr = corr_mat.loc[:, each_node].abs().mean()
G.add_node(each_node,
weight=str(avg_abs_corr + node_default_size),
size=str(avg_abs_corr + node_default_size),
color='skyblue',
style='filled',
fontcolor='red',
fontname='Calibri',
fontsize=12,
penwidth=1)
#
for each_pair in pairs[::-1]:
edge_len = corr_mat.loc[each_pair[0], each_pair[1]]
# default edge color is red
color = 'red'
# change the edge color if its positive
if edge_len > 0:
color = 'green'
# the customisation for each edge
G.add_edge(each_pair[0], each_pair[1], len=str(5 * edge_len), color=color, width="2.0")
NX.draw(G)
The output is something like this
Not only does this not have any labels at the nodes, none of the customizations work. Any ideas on where I am going wrong?
What I eventually want to achieve is something like this graph.
Since you are creating the graph first and then you want to draw it, I would recommend to play with the options of the nx.draw command.
e.g.
NX.draw(G,
with_labels=True,
font_color='r',
node_size=[float(G.node[node]['size']) for node in G.nodes()],
node_color=[G.node[node]['color'] for node in G.nodes()],
fontweight=[G.node[node]['fontsize'] for node in G.nodes()],
edge_color=[G.edge[edge[0]][edge[1]]['color'] for edge in G.edges()],
width=[G.edge[edge[0]][edge[1]]['width'] for edge in G.edges()]
)
New to python coming from MATLAB.
I am using a hyperbolic tangent truncation of a magnitude-scale function.
I encounter my problem when applying the 0.5 * math.tanh(r/rE-r0) + 0.5 function onto an array of range values r = np.arange(0.1,100.01,0.01). I get several 0.0 values for the function on the side approaching zero, which cause domain issues when I perform the logarithm:
P1 = [ (0.5*m.tanh(x / rE + r0 ) + 0.5) for x in r] # truncation function
I use this work-around:
P1 = [ -m.log10(x) if x!=0.0 else np.inf for x in P1 ]
which is sufficient for what I am doing but is a bit of a band-aid solution.
As requested for mathematical explicitness:
In astronomy, the magnitude scale works roughly as such:
mu = -2.5log(flux) + mzp # apparent magnitude
where mzp is the magnitude at which one would see 1 photon per second. Therefore, greater fluxes equate to smaller (or more negative) apparent magnitude. I am making models for sources which use multiple component functions. Ex. two sersic functions with different sersic indices with a P1 outer truncation on the inner component and a 1-P1 inner truncation on the outer component. This way, when adding the truncation function to each component, the magnitude as defined by radius, will become very large because of how small mu1-2.5*log(P1) gets as P1 asymptotically approaches zero.
TLDR: What I would like to know is if there is a way of preserving floating points whose accuracy is insufficient to be distinguishable from zero (in particular in the results of functions that asymptotically approach zero). This important because when taking the logarithm of such numbers a domain error is the result.
The last number before the output in the non-logarithmic P1 starts reading zero is 5.551115123125783e-17, which is a common floating point arithmetic rounding error result where the desired result should be zero.
Any input would be greatly appreciated.
#user:Dan
without putting my whole script:
xc1,yc1 = 103.5150,102.5461;
Ee1 = 23.6781;
re1 = 10.0728*0.187;
n1 = 4.0234;
# radial brightness profile (magnitudes -- really surface brightness but fine in ex.)
mu1 = [ Ee1 + 2.5/m.log(10)*bn(n1)*((x/re1)**(1.0/n1) - 1) for x in r];
# outer truncation
rb1 = 8.0121
drs1 = 11.4792
P1 = [ (0.5*m.tanh( (2.0 - B(rb1,drs1) ) * x / rb1 + B(rb1,drs1) ) + 0.5) for x in r]
P1 = [ -2.5*m.log10(x) if x!=0.0 else np.inf for x in P1 ] # band-aid for problem
mu1t = [x+y for x,y in zip(P1,mu1)] # m1 truncated by P1
where bn(n1)=7.72 and B(rb1,drs1) = 2.65 - 4.98 * ( r_b1 / (-drs1) );
mu1 is the magnitude profile of the component to be truncated. P1 is the truncation function. Many of the final entries for P1 are zero, which is due to the floating points being undistinguished from zero due to the floating point accuracy.
An easy way to see the problem:
>>> r = np.arange(0,101,1)
>>> P1 = [0.5*m.tanh(-x)+0.5 for x in r]
>>> P1
[0.5, 0.11920292202211757, 0.01798620996209155, 0.002472623156634768, 0.000335350130466483, 4.539786870244589e-05, 6.144174602207286e-06, 8.315280276560699e-07, 1.1253516207787584e-07, 1.5229979499764568e-08, 2.0611536366565986e-09, 2.789468100949932e-10, 3.775135759553905e-11, 5.109079825871277e-12, 6.914468997365475e-13, 9.35918009759007e-14, 1.2656542480726785e-14, 1.7208456881689926e-15, 2.220446049250313e-16, 5.551115123125783e-17, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Note also the floats before zeros.
Recall that the hyperbolic tangent can be expressed as (1-e^{-2x})/(1+e^{-2x}). With a bit of algebra, we can get that 0.5*tanh(x)-0.5 (the negative of your function) is equal to e^{-2x}/(1+e^{-2x}). The logarithm of this would be -2*x-log(1+exp(-2*x)), which would work and be stable everywhere.
That is, I recommend you replace:
P1 = [ (0.5*m.tanh( (2.0 - B(rb1,drs1) ) * x / rb1 + B(rb1,drs1) ) + 0.5) for x in r]
P1 = [ -2.5*m.log10(x) if x!=0.0 else np.inf for x in P1 ] # band-aid for problem
With this simpler and more stable way of doing it:
r = np.arange(0.1,100.01,0.01)
#r and xvals are numpy arrays, so numpy functions can be applied in one step
xvals=(2.0 - B(rb1,drs1) ) * r / rb1 + B(rb1,drs1)
P1=2*xvals+np.log1p(np.exp(-2*xvals))
Two things you can try.
(1) brute force approach: find a variable-precision float arithmetic package and use that instead of built-in fixed precision. I am playing with your problem in Maxima [1] and I find that I have to increase the float precision quite a lot in order to avoid underflow, but it is possible. I can post the Maxima code if you want. I would imagine that there is some suitable variable-precision float library for Python.
(2) approximate log((1/2)(1 + tanh(-x)) with a Taylor series or some other kind of approximation in order to avoid the log(tanh(...)) altogether.
[1] http://maxima.sourceforge.net
The code below is a mixture of R and Python, which is used for determining the Outliers values from a list of values.
When I run this code :
#!/usr/bin/python
from rpy import *
r.library("robustbase")
listInput = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0016999999999999999, 0.0025999999999999999, 0.0086, 0.0095999999999999992, 0.012, 0.0132, 0.0149, 0.021700000000000001, 0.022700000000000001, 0.023300000000000001, 0.024799999999999999, 0.0263, 0.029100000000000001, 0.033000000000000002, 0.0424, 0.057099999999999998, 0.0625, 0.063299999999999995, 0.0654, 0.069900000000000004, 0.070599999999999996, 0.072999999999999995, 0.078, 0.085599999999999996, 0.085599999999999996, 0.086499999999999994, 0.088200000000000001, 0.088999999999999996, 0.091300000000000006, 0.092700000000000005, 0.092999999999999999, 0.097199999999999995, 0.099900000000000003, 0.1077, 0.1143, 0.1255, 0.128, 0.13009999999999999, 0.13159999999999999, 0.13270000000000001, 0.1333, 0.1351, 0.14369999999999999, 0.15060000000000001, 0.1547, 0.15529999999999999, 0.15740000000000001, 0.15870000000000001, 0.17630000000000001, 0.1799, 0.18179999999999999, 0.20660000000000001, 0.20930000000000001, 0.2152, 0.219, 0.22919999999999999, 0.22989999999999999, 0.23200000000000001, 0.23369999999999999, 0.23619999999999999, 0.2399, 0.2422, 0.24890000000000001, 0.2545, 0.255, 0.25519999999999998, 0.25990000000000002, 0.26050000000000001, 0.26400000000000001, 0.27489999999999998, 0.27739999999999998, 0.27800000000000002, 0.27889999999999998, 0.28310000000000002, 0.29220000000000002, 0.29470000000000002, 0.30120000000000002, 0.31119999999999998, 0.32829999999999998, 0.32890000000000003, 0.33119999999999999, 0.3347, 0.3407, 0.35310000000000002, 0.35580000000000001, 0.35980000000000001, 0.3705, 0.38009999999999999, 0.38569999999999999, 0.39389999999999997, 0.40060000000000001, 0.4108, 0.4173, 0.42859999999999998, 0.4289, 0.4294, 0.443, 0.44359999999999999, 0.4541, 0.47020000000000001, 0.49109999999999998, 0.5, 0.50449999999999995, 0.50749999999999995, 0.53769999999999996, 0.54779999999999995, 0.58220000000000005, 0.59089999999999998, 0.60070000000000001, 0.60360000000000003, 0.60970000000000002, 0.63070000000000004, 0.63390000000000002, 0.65880000000000005, 0.6653, 0.66620000000000001, 0.66669999999999996, 0.69120000000000004, 0.72240000000000004, 0.7399, 0.74629999999999996, 0.748, 0.76139999999999997, 0.76319999999999999, 0.76719999999999999, 0.79549999999999998, 0.80679999999999996, 0.8085, 0.81599999999999995, 0.82499999999999996, 0.84940000000000004, 0.85919999999999996, 0.8851, 0.8921, 0.89900000000000002, 0.92200000000000004, 0.92379999999999995, 0.95099999999999996, 0.96150000000000002, 0.96319999999999995, 0.96709999999999996, 0.9698, 0.97499999999999998, 0.97589999999999999, 0.98419999999999996, 0.99029999999999996, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
print len(listInput)
TupleInput = tuple(listInput)
print r("adjboxStats")(r.c(TupleInput), coef = 2.5, a = -4, b = 3, do_conf = True, do_out = True)
I get this error:
maximal number of iterations (100 =? 100) reached prematurely
Traceback (most recent call last):
File "/home/kritani/r_python/stack.py", line 49, in <module>
print r("adjboxStats")(r.c(TupleInput), coef = 2.5, a = -4, b = 3, do_conf = True, do_out = True)
rpy.RPy_RException: Error in mc.default(x, na.rm = TRUE) :
mc(): not 'converged' in 100 iterations
When I minimize the number to less than 100 number it works.
I uninstalled R and reinstalled it but it didn't fix the issue. I looked here and here and here and here and there's not a clear solution that really helps.
Does anyone know why this is happening?
Thank you, guys!