%precision not respected IPython - ipython

Simply put, magic function %precision does not respect floating point precision for output of a simple variable.
#Configure matplotlib to run on the browser
%matplotlib notebook
%precision 3
from ipywidgets import widgets
from IPython.display import display
import pandas as pd
import numpy as np
import matplotlib as mpl
mpl.get_backend() #Import pyplot scripting layer as plt
import matplotlib.pyplot as plt
import ipywidgets as widgets
np.random.seed(12345)
np.set_printoptions(precision=3)
df = pd.DataFrame([np.random.normal(32000,200000,3650),
np.random.normal(43000,100000,3650),
np.random.normal(43500,140000,3650),
np.random.normal(48000,70000,3650)],
index=[1992,1993,1994,1995]).T
df_stats = df.describe()
a=8/11
The output is simply 0
Please assist.

It looks like this post is a modified replica of Python division.
I used the following to assist,
from __future__ import division
Have an excellent day and thanks for the help!

Related

How to implement parametrized plot using Jupyter and ipywidgets to work within Vusual Studio Code?

I am trying to implement parametrized plotting using Jupyter notebook and ipywidgets to work in VS Code. Below is a snippet I am trying with:
import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt
#widgets.interact(freq=(1.0, 10.0))
def plot(freq=1.0):
t = np.linspace(-1., +1., 1000)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(t, np.sin(2 * np.pi * freq * t), lw=2, color='blue')
This worked perfectly with Jupyter notebook in a browser, but in VS Code no plot appears. Simple ipywidgets controls like
#widgets.interact
def f(x=5):
print(x)
work perfectly good in my VS Code. Is this a general disadvantage of the VS Code Jupyter extension or am I doing things wrongly?
OS: Windows 10, 64 bit;
VS Code Jupyter extension: Miscosoft v. 57,661,722;
IPython: v. 8.9.0;
ipywidgets: v.8.0.4.

Graph missing from output

The following code works, when I run it via Jupyter through Anaconda, yet when I do the same through VSC although gives me no error messages the output is missing the graph and only shows the top "name" segment, see picture. I would really like to it to work on VSC, any ideas why it isnt working? I have tried everything, reinstalling, updating, switching interpreters/kernels etc and nothing works.
import lumicks.pylake as lk
import matplotlib.pyplot as plt
import glob
import numpy as np
%matplotlib notebook
import lumicks.pylake as lk
#lk.pytest()
pip install scipy
fdcurves = {}
for filename in
glob.glob('C:/Users/paulv/OneDrive/Desktop/20220321_BNT_groupA/20220321-140249 FD Curve B7 1nM DNA Fwd1.h5'):#Control curves
file = lk.File(filename)
key, curve = file.fdcurves.popitem()
fdcurves[key] = curve
fdcurves
selector = lk.FdDistanceRangeSelector(fdcurves)
plt.show()
Thank you in advance !!

Density-Based Clustering Validation (DBCV) never stops running

I have completed running DBSCAN on a dataset of mine clustering patches of deforestation and I am attempting to validate the results according to this paper.
I have install the package from this Github, but when I try and run the code it never completes. I ran it for over a 5 days and it never stopped running or threw an error. Running DBSCAN only took 15 minutes so I am a little confused why just the validating is taking so long. Is there something I'm getting wrong with the DBCV code or the inputs?
Since it never finishes running the code I don't know of an error that I can report. I am unsure if I'm inputting the data into the code correctly, but I tried to copy the example on GitHub as closely as I could. I don't know how to share my .csv file to show what my file is like. It has 16 dimensions that I consense down using a MinMaxScaler before running DBSCAN. I have previously completed the DBSCAN clustering and an just trying to get the DBCV to work.
import pandas as pd
import numpy as np
from pylab import rcParams
import matplotlib.pyplot as plot
import sklearn
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import MinMaxScaler
from scipy.spatial import euclidean
from DBCV import DBCV
f = pd.read_csv('csv_file_I_Don't_know_how_to_share')
x = f.loc[:, [1-15]].values
norm_data = MinMaxScaler()
data = norm_data.fit_transform(x)
dbscan = DBSCAN(eps=.15, min_samples = 100)
clusters = dbscan.fit_predict(data)
DBCV_score = DBCV(data, clusters, dist_function=euclidean)
print ('DBCV Score: ' + DBCV_score)
I'm expecting a score to be printed but instead the code continues to run and doesn't stop. Any help would be great!
You run:
from scipy.spatial import euclidean
But the code on GitHub defines the method to use euclidean imported like this:
from scipy.spatial.distance import euclidean
Try changing this, it might work.
In addition to the answer by #Dumbfool there seems to be an error in:
print('DBCV Score: ' + DBCV_score)
Try changing the + to ,
I hope this helps.

Finding all subgraphs of depth 2 Networkx

I have a huge graph in networkx and I would like to get all the subgraphs of depth 2 from each node. Is there a nice way to do that using buildin function in networkx?
As I said in the comment, networkx.ego_graph fits the bill. You just need to make sure that you set the radius to 2 (default is 1):
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
# create some test graph
graph = nx.erdos_renyi_graph(1000, 0.005)
# create an ego-graph for some node
node = 0
ego_graph = nx.ego_graph(graph, node, radius=2)
# plot to check
nx.draw(ego_graph); plt.show()

using draw_networkx(), How to show multiple drawing windows?

The following code will create only one window at a time, the second window will only show when the first one is closed by the user.
How to show them at the same time with different titles?
nx.draw_networkx(..a..)
nx.draw_networkx(..b..)
It works the same as making other plots with Matplotlib.
Use the figure() command to switch to a new figure.
import networkx as nx
import matplotlib.pyplot as plt
G=nx.cycle_graph(4)
H=nx.path_graph(4)
plt.figure(1)
nx.draw(G)
plt.figure(2)
nx.draw(H)
plt.show()
You can use matplotlib and a grid to show multiple graphs:
#!/usr/bin/env python
"""
Draw a graph with matplotlib.
You must have matplotlib for this to work.
"""
__author__ = """Aric Hagberg (hagberg#lanl.gov)"""
# Copyright (C) 2004-2008
# Aric Hagberg <hagberg#lanl.gov>
# Dan Schult <dschult#colgate.edu>
# Pieter Swart <swart#lanl.gov>
# All rights reserved.
# BSD license.
try:
import matplotlib.pyplot as plt
except:
raise
import networkx as nx
G=nx.grid_2d_graph(4,4) #4x4 grid
pos=nx.spring_layout(G,iterations=100)
plt.subplot(221)
nx.draw(G,pos,font_size=8)
plt.subplot(222)
nx.draw(G,pos,node_color='k',node_size=0,with_labels=False)
plt.subplot(223)
nx.draw(G,pos,node_color='g',node_size=250,with_labels=False,width=6)
plt.subplot(224)
H=G.to_directed()
nx.draw(H,pos,node_color='b',node_size=20,with_labels=False)
plt.savefig("four_grids.png")
plt.show()
Code above will generates this figure:
Reference: https://networkx.org/documentation/networkx-1.9/examples/drawing/four_grids.html