Data Visulization : Matplotlib and Numpy throwing value error - python

I am new to machine learning. I was teaching myself data visualization with MATPLOTLIB. my code is pretty simple.
It takes a numpy array (x = np.random.rand(1,100)) of shape=(1, 100)).
It converts numpy array x into y(y = np.sin(x)).
Final task is to visualise this in a BAR(plt.bar(x, y, label="BAR", color='r'))
But it is throwing VALUE ERROR.Even though there are already answers to this question, but none seems to work so far for me.
In one answer for this question By unutbu
he explains that this error is raised "whenever one tries to evaluate an array in boolean context".
I am unable to understand how I am using these arrays as boolean?
MY CODE:
import matplotlib.pyplot as plt
import numpy as np
#arguments are shape: 1=row; 100=columns
x = np.random.rand(1, 100)
y = np.cos(x)
#bars
plt.bar(x, y, label='Bars1', color='pink')
#legends
plt.legend()
#show the figure
plt.show()

You need to replace
x = np.random.rand(1, 100)
with
x = np.random.rand(100)
The reason is that the former gives you an array of arrays (with one array inside, but it is still a 2D array overall with dimensions 1-by-100), while the latter gives you a 1D array (of length 100). In order to visualize it with plt, you need the latter.

Related

Error for a given function: only size-1 arrays can be converted to Python scalars

i am a newbie and currently trying to plot this function so I can choose a range of x values for which I should perform my experiments in the ChemistryLab.
I found different articles on plotting functions and it all worked with generic functions like sin(x).
But once I input my function it does not work. The problem already occurs after the first two lines:
import numpy as np
import math
X = np.linspace(0, 512, 256)
f = ((x+22)- math.sqrt((x+22)**2-4*2*x))
--> TypeError: only size-1 arrays can be converted to Python scalars
I found threads saying X should be vectorize, however I could not come up with a solution.
Thanks for help in advance!
Create a vectorized function. For example:
equation_func = np.vectorize(math.sqrt)
new_f = equation_func(X)
Swap math.sqrt with a function containing your more sophisticated equation and you're home.
A good way to implement a mathematical function in Python is, perhaps unsurprisingly, as a Python function. Then you can, for example, use that function in a list comprehension to get an array of values:
import numpy as np
import math
import matplotlib.pyplot as plt
X = np.linspace(0, 512, 256)
def f(x):
return x + 22 - math.sqrt((x + 22)**2 - 8 * x)
Y = np.array([f(x) for x in X])
plt.plot(X, Y);

Error plotting scikit-learn dataset training and test data

I am trying to plot the training and test data from a scikit-learn dataset.
import sys, os
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
plt.switch_backend('agg')
%matplotllib inline
diabetes = datasets.load_diabetes()
diabetes_X = diabetes.data[:, np.newaxis, 2]
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
diabetes_y_train = np.matrix(diabetes.target[:-20]).T
diabetes_y_test = np.matrix(diabetes.target[-20:]).T
plt.scatter(diabetes_X_train, diabetes_y_train, color='black')
plt.scatter(diabetes_X_test, diabetes_y_test, color='red')
but I have the following error:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 422 and the array at index 1 has size 1
I checked the shape of the matrices and the training data has (422,1) and the test data (20,1). What is causing this error?
plt.scatter is expecting to plot two same-shaped datasets against each other. IF they aren't 1D, they will be flattened. It does not make sense to flatten X in a machine-learning problem.
Check the dimensions of X_train and y_train. You'll see that they aren't compatible. This is a 2D plot you're making, you can only plot one set of numbers against another. X is a matrix: every row is a bunch of numbers.
So you can do this:
import numpy as np
import matplotlib.pyplot as plt
x, y = np.random.random((422, 1)), np.random.random((422, 1))
plt.scatter(x, y)
But you can't do this:
X, y = np.random.random((422, 10)), np.random.random((422, 1))
plt.scatter(X, y)
Which is essentially what you're trying to do. (I don't think you want to transpose y by the way.)
So this should work for you:
plt.scatter(diabetes_X_train[:, 0], diabetes_y_train)
But that only shows the relationship with one feature of X.
Assuming you're just trying to explore the data, I recommend checking out seaborn.pairplot. It's perfect for this sort of thing.

Plot 3rd axis of a 3D numpy array

I have a 3D numpy array that is a stack of 2D (m,n) images at certain timestamps, t. So my array is of shape (t, m, n). I want to plot the value of one of the pixels as a function of time.
e.g.:
import numpy as np
import matplotlib.pyplot as plt
data_cube = []
for i in xrange(10):
a = np.random(100,100)
data_cube.append(a)
So my (t, m, n) now has shape (10,100,100). Say I wanted a 1D plot the value of index [12][12] at each of the 10 steps I would do:
plt.plot(data_cube[:][12][12])
plt.show()
But I'm getting index out of range errors. I thought I might have my indices mixed up, but every plot I generate seems to be in the 'wrong' axis, i.e. across one of the 2D arrays, but instead I want it 'through' the vertical stack. Thanks in advance!
Here is the solution: Since you are already using numpy, convert you final list to an array and just use slicing. The problem in your case was two-fold:
First: Your final data_cube was not an array. For a list, you will have to iterate over the values
Second: Slicing was incorrect.
import numpy as np
import matplotlib.pyplot as plt
data_cube = []
for i in range(10):
a = np.random.rand(100,100)
data_cube.append(a)
data_cube = np.array(data_cube) # Added this step
plt.plot(data_cube[:,12,12]) # Modified the slicing
Output
A less verbose version that avoids iteration:
data_cube = np.random.rand(10, 100,100)
plt.plot(data_cube[:,12,12])

How can I plot a differential equation in python?

I want to plot the solution of my differential equation but I got this:
'ValueError: x and y must have same first dimension, but have shapes
(360,) and (1,)'
When I write _plt.plot(t,final[:1])_ I got
'Equality object is not subscriptable'
statement.
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from sympy.abc import *
import sympy as sy
L= float(input('L:'))
R= float(input('R:'))
v=220*sy.sqrt(2)
i=sy.Function('i')
q=sy.dsolve(sy.Eq(sy.Derivative(i(t)*L,t)+i(t)*R,v*sy.sin(t)),i(t)).evalf()
constant=sy.solve(q.subs(i(t),0),dict=True)
t=np.linspace(0,360,360)
final=q.subs(constant[0]).evalf()
plt.plot(t,final)
plt.show()
What should I do?
It's obvious from the code that t has 360 elements
t=np.linspace(0,360,360)
The error complains that final has an initial dimension of 1, where as it should be 360 like t. While it is possible the final has (1,) shape (1 element, containing another array or list), more likely it is (1, n).
When you get shape errors, you need to look at the shape of relevant arrays,
print(final.shape, final.dtype)
and decide from that the correct way of adjusting the shapes.
plot can handle a second argument that is (360,m), where m is the number of lines that it should plot.

Accessing all elements of a row in python float array

I have searched long and hard and cannot find a way to do this.
x = random.normal(100,100)
This is a variable X of type float. I want to pass all the elements of the first column as X coordinates and the elements of the second column as Y coordinates to the matplotlib.pyplot function. How do I do it ?
Also how to determine the shape of a float array ? In this case it is clearly 100x100 but since float objects do not have a float.shape attribute.
Your np.random.normal(100,100) is a simple, single float...
Like so?
import matplotlib.pyplot as plt
import numpy as np
data = np.random.normal((100,100)*100) # 2 * 100 values = 200 values normalized around 100
x = data[0::2] take even as X
y = data[1::2] take uneven as Y
plt.scatter(x,y)
plt.plot(x,y)
plt.grid(True)
plt.show()
To elaborate slightly on #Patrick Artner's answer...
x = random.normal(100,100)
This generates one random variable from a normal distribution with mean = 100 and standard deviation = 100. To see the answer more clearly, you could specify keyword arguments as
x = np.random.normal(loc=100, scale=100)
Note: loc = mean and scale = standard deviation.
See numpy's documentation: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.normal.html
To answer your question about how to determine the shape of a float array, you simply call the .shape function on a float array. For example:
x = np.random.normal(0, 1, (100, 2))
print("The shape of x is %s" % (x.shape,))

Categories