how to plot edgelist file as a network in networkx - social-networking

I have an edgelist file. How to plot this edgelist as a network in networkx? I need to visualize it as a network graph like nodes and edges. Can anyone know this?

The basic pattern works like this:
In [1]: import networkx as nx
In [2]: import matplotlib.pyplot as plt
In [3]: G = nx.Graph() # create empty graph
In [4]: G.add_edges_from([(1,2),(1,3),(1,4),(4,5)]) # or use nx.read_edgelist("path")
In [5]: nx.draw(G)
In [6]: plt.show()
If you are reading the edgelist from a file, then use: G=nx.read_edgelist("test.edgelist") and substitute the path to your edgelist in step 4. More in the documentation on read_edgelist()

Related

How to find silhouette_score for K-means cluster Algorithm

I am trying to find silhouette_score for K-means cluster Algorithm. Actually I am using 4 other algorithms and I have to find silhouette_score of all four algorithms. I am trying to find for k-mean cluster first and use the same code for all others as well.
import pandas as pd
import numpy as np
from sklearn.datasets import load_wine
df = load_wine()
from sklearn.preprocessing import MinMaxScaler
X_scaled_data = MinMaxScaler().fit_transform(df.data)
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3).fit(X_scaled_data)
from sklearn.metrics import silhouette_score
silhouette_avg = silhouette_score(X_scaled_data, kmeans.labels_)
print("For n_clusters =", 3, "The average silhouette_score is :", silhouette_avg)
Here is the Error:
The code example you posted works for me.
However as the error message states, the number of unique labels (n_labels) in you predicted labels in no larger than 1. That means your algorithms assigns all points to the same cluster. If you look at the documentation for the Silhouette-score you will notice that in this case the metric is not defined:
Note that Silhouette Coefficient is only defined if number of labels
is 2 <= n_labels <= n_samples - 1.
Maybe consider using a different metric. Here are some examples. Or check the number of unique labels from your predictions before calculating the Silhouette-score.

Python: how to create 3 single channel images

I want to create a 3 single channel images, red, green and blue with random scatters.
So far I manage to create one color images, but it is in RGB format.
import matplotlib.pyplot as plt
import numpy as np
import cv2
from PIL import Image
### blue
cells = np.random.RandomState(400)
x = cells.randn(10)
y = cells.randn(10)
colors = "blue"
sizes = 250 * cells.rand(200)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.7, cmap='viridis')
plt.axis("off")
plt.show()
I want to use the single files and combinations to test an image- analysis tool for object detection.
Thanks for any help!

How do i analyse scientific plots programmatically

I have a graphs that contain a series of curves.I want to know which curve is closest to the origin.
Given that i have the csv file , how can i process the CSV file in an efficient manner to get the curve closest to origin.For the plot above AV1 is the expected output.
Those look like either exponential decay curves, of the form y = a * c^x
This means the logarithm is a linear function: log y = log a + x * log c
Perhaps use a linear regression model on the logarithm of "ssimulacra score" to get a slope and intercept for each line, and choose the line having the smallest intercept?
scikit learn has an easy-to-use linear regressor:
http://scikit-learn.org/stable/modules/linear_model.html
You can use pandas to read the csv file easily.
pip3 install scikit-learn pandas numpy
python:
import pandas as pd
import numpy as np
from sklearn import linear_model
df = pd.read_csv('filename.csv')
X = df['bpp'].values
y = np.log(df['ssimulacra score'].values)
reg = linear_model.LinearRegression()
reg.fit(X, y)
intercept = reg.intercept_
Get the intercept for each line and return the line having the smallest intercept.

Animating plots on ipython 2 for mac os

I'm attempting to animate (at run time) unlike this answer in mac os and ipython notebook 2.0. I have the following code:
%pylab inline
import time, sys
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output
f, ax = plt.subplots()
x = np.linspace(0,6,200)
for i in range(10):
y = i/10*np.sin(x)
print i
ax.plot(x,y)
time.sleep(0.1)
clear_output(True)
display(f)
ax.cla() # turn this off if you'd like to "build up" plots
plt.close()
Which seems to almost work, as the print is working without flashing (a previous problem, corrected by clear_output), but the axes are not updating.
The issue here is that i is an integer, so the line y = i/10*np.sin(x) does integer division, which always returns 0. It is animating! But the result is always a flat line at 0. Change that line to
y = float(i)/10*np.sin(x)
When you do that, you'll notice that it doesn't animate in a very nice way. To make it look better, we can explicitly set the y-axis limits instead of letting Matplotlib do it automatically. Inside your loop, add the line
ax.set_ylim(-1, 1)
The final code below animates nicely.
%pylab inline
import time, sys
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output
f, ax = plt.subplots()
x = np.linspace(0,6,200)
for i in range(10):
y = float(i)/10*np.sin(x)
print i
ax.set_ylim(-1, 1)
ax.plot(x,y)
time.sleep(0.1)
clear_output(True)
display(f)
ax.cla() # turn this off if you'd like to "build up" plots
plt.close()

matplotlib: histogram and bin labels

I'm trying to plot a histogram with bar chart, and I'm having difficulties figuring out how to align the x-axis labels with the actual bins. The code below generates the following plot:
as you can see, the end of each x-label is not aligned to the center of its bin. The way i'm thinking about this is: when i apply a 45-degree rotation, the label pivots around its geometrical center. I was wondering if it's possible to move the pivot up to the top of the label. (Or simply translate all the labels slightly left.)
import matplotlib.pyplot as plt
import numpy as np
#data
np.random.seed(42)
data = np.random.rand(5)
names = ['A:GBC_1233','C:WERT_423','A:LYD_342','B:SFS_23','D:KDE_2342']
ax = plt.subplot(111)
width=0.3
bins = map(lambda x: x-width/2,range(1,len(data)+1))
ax.bar(bins,data,width=width)
ax.set_xticks(map(lambda x: x, range(1,len(data)+1)))
ax.set_xticklabels(names,rotation=45)
plt.show()
Use:
ax.set_xticklabels(names,rotation=45, rotation_mode="anchor", ha="right")
The output is:

Resources