I have the following data which needs to be linearly classified using least squares. I wanted to visualise my data and then plot the features with colours but I got the following error when assigning the colour colour_cond.
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Note that data_t is made of 1s and 0s.
import numpy as np
import matplotlib.pyplot as plt
import glob
from scipy.io import loadmat
%matplotlib inline
data = glob.glob('Mydata_A.mat')
data_c1 = np.array([loadmat(entry, variable_names= ("X"), squeeze_me=True)["X"][:,0] for entry in data])
data_c2 = np.array([loadmat(entry, variable_names= ("X"), squeeze_me=True)["X"][:,1] for entry in data])
data_t = np.array([loadmat(entry, variable_names= ("T"), squeeze_me=True)["T"][:] for entry in data])
colour_cond=['red' if t==1 else 'blue' for t in data_t]
plt.scatter(data_c1,data_c2,colour=colour_cond)
plt.xlabel('X1')
plt.ylabel('X2')
plt.title('Training Data (X1,X2)')
plt.show()
Your problem is that the arrays data_c1, data_c2 and data_t seem to have more that one dimension. In your following line:
colour_cond=['red' if t==1 else 'blue' for t in data_t]
the variable t is not a scalar but a NumPy array, and t == 1 is ambiguous for non-scalar NumPy objects. I would suggest you to ravel (i.e. flatten) all your arrays:
import glob
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
%matplotlib inline
data = loadmat('Mydata_A.mat')
data_c1 = np.array([
loadmat(entry, variable_names=("X"), squeeze_me=True)["X"][:, 0]
for entry in entries]).ravel()
data_c2 = np.array([
loadmat(entry, variable_names=("X"), squeeze_me=True)["X"][:, 1]
for entry in entries]).ravel()
data_t = np.array([
loadmat(entry, variable_names=("T"), squeeze_me=True)["T"][:]
for entry in entries]).ravel()
colour_cond = ['red' if t==1 else 'blue' for t in data_t]
plt.scatter(data_c1, data_c2, color=colour_cond)
plt.xlabel('X1')
plt.ylabel('X2')
plt.title('Training Data (X1,X2)')
plt.show()
Related
Want to plot normalised values in array but getting empty plot
import numpy as np
x_array = np.array([2,3,5,6,7,4,8,7,6])
normalized_arr = preprocessing.normalize([x_array])
print(normalized_arr)
plt.plot(normalized_arr)
plt.show()
Empty plot - https://i.stack.imgur.com/NnSbI.png
Is there function that can fill the empty plot with values?
You probably need to change your code into:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing
x_array = np.array([2,3,5,6,7,4,8,7,6])
normalized_arr = preprocessing.normalize([x_array])
print(normalized_arr)
plt.plot(x_array.reshape(-1,1),normalized_arr.reshape(-1,1))
plt.show()
Output
I am trying to generate a heatmap from 3D-data in a csv-file. The csv-file has the format x,y,z for each line. The problem is when I create a array to link the values, I can't use float-numbers as keys. When setting the dtype to int in np.loadtext(), the code works fine; but this makes the resolution only half of what the csv-file can replicate. Is there another way of linking the values?
The code so far is:
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt
fname = 'test18.csv'
x, y, z = np.loadtxt(fname, delimiter=',', dtype=float).T
pltZ = np.zeros((y.max()+1, x.max()+1), dtype=float)
pltZ[y, x] = z
heat_map = sb.heatmap(pltZ, cmap=plt.cm.rainbow)
plt.show()
%matplotlib inline
from sklearn.datasets import load_svmlight_file
import numpy as np
import matplotlib.pyplot as plt
Xtr,Ytr = load_svmlight_file("a9a")
Xtst,Ytst = load_svmlight_file("a9a.t")
Xtr=Xtr.todense()
Xtst=Xtst.todense()
print (Xtr.shape, "", Ytr.shape)
The Output was
Xtr = (32561,123)
Ytr = (32561,)
I want to plot this on a scatter graph . But I receive an error which says Xtr and Ytr are not of the same size . How do I make them same size .
Which column of Xtr do you want to plot? It has 123 columns, so you'll have to choose only one of them.
I have numpy array with values 0,1,2. I want to separate them in different arrays and plot them. How can I do that?
for i in range(2):
if i==0
z = [i]
elif i==1
y = [i]
else
w = [i]
this is what i tried
just use the histogram function from pyplot
import numpy as np
import matplotlib.pyplot as plt
y = np.random.randint(0,3,100)
plt.hist(y)
plt.show()
I want to plot ECG graph using matplotlib . y values from a file having float values and x value is incrementing(ie x ranges from 1 to 1000). went through tutorials and couldn't find any solutions.
Demo Code
import numpy as np
import matplotlib.pyplot as plt
import random
import pickle
#Y Axis : Generate 1000 random numbers
yAxisNumbers = np.random.uniform(1,100,1000)
#Save numbers to a file for demo purpose
with open('numpyData.txt', 'wb') as myFile:
pickle.dump(yAxisNumbers,myFile)
#X Axis :Generate 1000 random numbers
xNumbers = [ x for x in range(1000)]
#Load file data to a list
with open('numpyData.txt', 'rb') as aFile:
yNumbers = pickle.load(aFile)
#Plot and label Graph
plt.plot(xNumbers,yNumbers)
plt.ylabel("Random Float Numbers")
plt.xlabel("Number Count")
plt.title("ECG Graph")
plt.show()
Graph
Here's a minimal answer, based on the scant details provided.
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
Y = np.loadtxt(filename, other needed options)
plt.plot(np.arange(len(Y))+1,Y)
import numpy as np
import pylab as p
aa=np.loadtxt('....your file ....')
x,y= aa.T # transpose data into 2 columns, assuming you have 2 columns
p.plot(x,y)
p.show()