Combine two numpy array together - python

I have two numpy array like this,
this is the first one, called "pred_test"
array([2., 2., 2., 2., 2., 1., 2., 2., 2., 3., 2., 2., 3., 2., 2., 2., 2.,
2., 3., 3.], dtype=float32)
and this is the second one, called "pred_train"
array([13., 11., 9., 5., 5., 3., 2., 1., 1., 1., 4., 5., 11.,
9., 9., 4., 4., 3., 2., 1., 1., 1., 2., 1., 11., 12.,
8., 3., 3., 4., 4., 2., 2., 1., 2., 2., 4., 3., 2.,
1., 1., 1., 1., 1., 2., 1., 1., 2., 3., 2., 3., 2.,
2., 2., 2., 2., 2., 1., 2., 2., 10., 2., 2., 2., 2.,
2., 2., 3., 3., 2., 2., 3., 4., 2., 3., 3.],
dtype=float32)
How can i combine the two array to look like pred_train + pred test, something like this..
[2., 2., 2., 2., 2., 1., 2., 2., 2., 3., 2., 2., 3., 2., 2., 2., 2., 2., 3., 3.,
13., 11., 9., 5., 5., 3., 2., 1., 1., 1., 4., 5., 11.,
9., 9., 4., 4., 3., 2., 1., 1., 1., 2., 1., 11., 12.,
8., 3., 3., 4., 4., 2., 2., 1., 2., 2., 4., 3., 2.,
1., 1., 1., 1., 1., 2., 1., 1., 2., 3., 2., 3., 2.,
2., 2., 2., 2., 2., 1., 2., 2., 10., 2., 2., 2., 2.,
2., 2., 3., 3., 2., 2., 3., 4., 2., 3., 3.]
Can someone help me? Thank you..

Use numpy.concatenate((pred_test, pred_train))

You can go with:
np.append(pred_test, pred_train)

Related

How to map 5 numbers to 5 different colors

I have a problem concerning the management of a colormap.
In the figure below, each point displayed is associated with a value : 2, 3, 4, 5 or 13.
So, to know the value associated with each point, I used the colormap option. I generated a colormap of 5 colors, one color associated with each value.
The problem is that the values 2, 3 and 4 are associated with purple, the value 5 with blue and the value 13 with red. Indeed, the colormap contains 5 colors, but the scale of the labels on the right goes from 2 to 13.
However, I would like the 2 to be associated with purple, the 3 with blue, the 4 with green, the 5 with orange and the 13 with red.
My code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 14 15:04:21 2022
#author: cldelh
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
# Abscisses
W = np.array([0. , 0.01 , 0.01999402, 0.03412597, 0.05410511,
0.08232443, 0.12194226, 0.16661301, 0.20758763, 0.23904194,
0.24626125, 0.25039445, 0.25330903, 0.25582872, 0.25816522,
0.26031379, 0.26266544, 0.26568211, 0.27005595, 0.2770875 ,
0.28909234, 0.30636753, 0.32544111, 0.34694002, 0.36917884,
0.3897257 , 0.40656458, 0.41784063, 0.42541583, 0.43055847,
0.43452519, 0.43742307, 0.43919769, 0.44031273, 0.44112462,
0.44200454, 0.44347889, 0.44643343, 0.45229159, 0.46288266,
0.48056282, 0.50895736, 0.54927625, 0.59608409, 0.64487664,
0.7016973 , 0.76708481, 0.84185322, 0.92696579, 1.01707062,
1.10557259, 1.19256251, 1.27815237, 1.36244217, 1.44550112,
1.52735963, 1.60800828, 1.68740049, 1.76545689, 1.84207015,
1.91710981, 1.99042675, 2.06185747, 2.13122796, 2.1983573 ,
2.263061 , 2.32515405, 2.38445365, 2.44078163, 2.49396659,
2.54384564, 2.5902658 , 2.63308518, 2.67217368, 2.70741361,
2.73869997, 2.76594058, 2.78905614, 2.80798011, 2.82265863,
2.83305046, 2.83912688, 2.84087179, 2.83828174, 2.83136621,
2.82014797, 2.80466344, 2.78496333, 2.76111323, 2.73319431,
2.70130406, 2.665557 , 2.62608541, 2.58303999, 2.53659044,
2.48692603, 2.43425621, 2.3788113 , 2.32084358, 2.26062905,
2.1984705 , 2.13470266, 2.06970116, 2.0038978 , 1.93780705,
1.87207326, 1.80755842, 1.74551542, 1.68795847, 1.63852865,
1.60461127, 1.60094385, 1.64165693, 1.71498642, 1.80255956,
1.89604738, 1.99229146, 2.08996895, 2.18845668, 2.28743203,
2.38671536, 2.48620089, 2.58582339, 2.68554117, 2.78532671,
2.8851614 , 2.98503236, 3.08493046])
# Ordonnées
Xinf = np.array([0.36001558, 0.36002501, 0.36005669, 0.36013731, 0.36031898,
0.36062301, 0.36527713, 0.36485195, 0.38101038, 0.39612248,
0.40736786, 0.41642505, 0.42372367, 0.4296357 , 0.4329689 ,
0.43243038, 0.42777775, 0.41881406, 0.4058955 , 0.39050351,
0.37868412, 0.37464251, 0.3753022 , 0.38049347, 0.39215149,
0.41261855, 0.44186649, 0.47214887, 0.50022204, 0.52476197,
0.54846522, 0.57015033, 0.58647846, 0.59804236, 0.60505114,
0.60736793, 0.60431747, 0.59481333, 0.57794772, 0.55477589,
0.52875494, 0.50564226, 0.49269248, 0.49103505, 0.49670408,
0.50868991, 0.52725566, 0.55320582, 0.58777717, 0.62935965,
0.67447035, 0.72228144, 0.77209809, 0.82333207, 0.87550968,
0.92821221, 0.98109947, 1.03388713, 1.08631077, 1.13814125,
1.18917466, 1.23921078, 1.28807521, 1.33558548, 1.38158777,
1.42589996, 1.46839252, 1.50890861, 1.54730779, 1.58347723,
1.61727155, 1.64859434, 1.67732612, 1.70338193, 1.72667917,
1.74713082, 1.76466827, 1.77924246, 1.79079041, 1.79926455,
1.80462534, 1.80686203, 1.80593978, 1.80183511, 1.79455897,
1.78410278, 1.77046371, 1.75366114, 1.733729 , 1.7106789 ,
1.68454743, 1.65537695, 1.62321454, 1.58811057, 1.55013282,
1.50933504, 1.46577577, 1.41951312, 1.37061227, 1.31911356,
1.2650476 , 1.20840623, 1.14917691, 1.08726763, 1.02253009,
0.95471075, 0.88341169, 0.8079946 , 0.72748982, 0.64047401,
0.54539278, 0.44410944, 0.35174988, 0.28325452, 0.23475268,
0.19915439, 0.17195691, 0.15050578, 0.13316766, 0.11888317,
0.10692915, 0.09679719, 0.08811551, 0.08060803, 0.07406247,
0.06831513, 0.06323702, 0.05872426])
# Variables
variable = np.array([ 5., 2., 2., 2., 2., 2., 3., 4., 5., 5., 13., 4., 3.,
3., 4., 4., 4., 4., 4., 4., 4., 4., 3., 3., 3., 3.,
4., 4., 4., 3., 3., 4., 4., 4., 4., 4., 4., 4., 4.,
4., 4., 4., 4., 4., 3., 3., 3., 3., 3., 3., 3., 3.,
3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.,
3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.,
3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.,
3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.,
3., 3., 3., 3., 3., 3., 3., 4., 4., 4., 4., 3., 3.,
3., 3., 2., 2., 2., 2., 2., 2., 2., 2., 2.])
# Valeurs contenues dans variable
variable_delete = np.array([ 5., 2., 3., 4., 13.])
# Points
plt.scatter(W,Xinf,s=10,marker='o', c=variable, cmap=cm.get_cmap('rainbow', len(variable_delete)))
# Colorbar
cb = plt.colorbar(ticks=variable_delete, location='right', label=r'$k_{iter} \; [\emptyset]$')
The following approach first locates 5 equally spaced spots into the range from 0 to 1 (the default colormap range). Then it maps each of the 5 variable_delete values to its corresponding spot. These mapped values are used for the coloring. Afterwards, the colorbar positions get labeled accordingly.
The code also works when the numeric labels are in a random order.
(An alternative could be to work with a BoundaryNorm, but that gets more complicated to nicely locate the labels.)
import numpy as np
import matplotlib.pyplot as plt
# W = ...
# Xinf = ...
# variable = ...
variable_delete = np.array([2., 3., 4., 5., 13.])
cbar_spots = np.linspace(0, 1, 2 * len(variable_delete) + 1)[1::2]
cbar_dict = {var: spot for var, spot in zip(variable_delete, cbar_spots)}
var_mapped = np.zeros_like(variable)
for k, v in cbar_dict.items():
var_mapped[variable == k] = v
plt.scatter(W, Xinf, s=10, marker='o', c=var_mapped,
cmap=plt.get_cmap('rainbow', len(variable_delete)), vmin=0, vmax=1)
cb = plt.colorbar(ticks=cbar_spots, location='right', label=r'$k_{iter} \; [\emptyset]$')
cb.set_ticklabels(variable_delete.astype(int).astype(str))
plt.show()
May I suggest a simple alternative to using a color map?
In [26]: for v in sorted(variable_delete):
...: ix = variable == v
...: plt.scatter(W[ix], Xinf[ix], s=6, marker='o', label='%3.1f'%v)
...: plt.legend()
...: plt.grid()

How to flip half of a numpy array

I have a numpy array:
arr=np.array([[1., 2., 0.],
[2., 4., 1.],
[1., 3., 2.],
[-1., -2., 4.],
[-1., -2., 5.],
[1., 2., 6.]])
I want to flip the second half of this array upward. I mean I want to have:
flipped_arr=np.array([[-1., -2., 4.],
[-1., -2., 5.],
[1., 2., 6.],
[1., 2., 0.],
[2., 4., 1.],
[1., 3., 2.]])
When I try this code:
fliped_arr=np.flip(arr, 0)
It gives me:
fliped_arr= array([[1., 2., 6.],
[-1., -2., 5.],
[-1., -2., 4.],
[1., 3., 2.],
[2., 4., 1.],
[1., 2., 0.]])
In advance, I do appreciate any help.
You can simply concatenate rows below the nth row (included) with np.r_ for instance, with row index n of your choice, at the top and the other ones at the bottom:
import numpy as np
n = 3
arr_flip_n = np.r_[arr[n:],arr[:n]]
>>> array([[-1., -2., 4.],
[-1., -2., 5.],
[ 1., 2., 6.],
[ 1., 2., 0.],
[ 2., 4., 1.],
[ 1., 3., 2.]])
you can do this by slicing the array using the midpoint:
ans = np.vstack((arr[int(arr.shape[0]/2):], arr[:int(arr.shape[0]/2)]))
to break this down a little:
find the midpoint of arr, by finding its shape, the first index of which is the number of rows, dividing by two and converting to an integer:
midpoint = int(arr.shape[0]/2)
the two halves of the array can then be sliced like so:
a = arr[:midpoint]
b = arr[midpoint:]
then stack them back together using np.vstack:
ans = np.vstack((a, b))
(note vstack takes a single argument, which is a tuple containing a and b: (a, b))
You can do this with array slicing and vstack -
arr=np.array([[1., 2., 0.],
[2., 4., 1.],
[1., 3., 2.],
[-1., -2., 4.],
[-1., -2., 5.],
[1., 2., 6.]])
mid = arr.shape[0]//2
np.vstack([arr[mid:],arr[:mid]])
array([[-1., -2., 4.],
[-1., -2., 5.],
[ 1., 2., 6.],
[ 1., 2., 0.],
[ 2., 4., 1.],
[ 1., 3., 2.]])

Count values in numpy array and return index by result

I have a 2d numpy array my_array that starts out like this:
array([[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 6., 7., 8., 9.]])
But after some processing which is irrelevant now looks like this:
array([[1., 2., 0., 4., 5., 6., 0., 8., 9.],
[0., 2., 0., 0., 5., 6., 7., 8., 9.],
[0., 2., 0., 4., 5., 0., 7., 0., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 0., 7., 8., 9.],
[0., 2., 0., 4., 5., 6., 0., 8., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 0.]])
As you can see, some of the items have been "zeroed out" quite randomly, but only the value of 3 was left with only 1 item that isn't zero. I'm looking for a function that takes this array and returns the index / row number that has the value 3 (or any other value that only appears once and only once in the array).
To explain this differently:
I first have to figure out if there is such an item that only appears once (in this example the answer is yes and that item is the number 3), and then I need to return its row number (in this case 4 since the only line with 3 in it is: my_array[4])
I have successfully done that with iterating over the array, item by item, and counting the number of times each number appears (and returning only the item whose count is 1) and then iterating over everything a second time to find the correct index / row number of where that item is located.
This seems very inefficient, especially if the array will be larger. Is there a better way in numpy to do this?
EDIT: if the number that appears only once is 0 that shouldn't count, i'm only looking for the "column" that was zeroed-out completely except 1 item in it
Try using the numpy.count_nonzero method
numpy.count_nonzero(arr, axis=0)
This will count the non-zero values columnwise
Here's a Demo
I will leave the rest to you. Good Luck
Edit I wasn't even using the mask, you can just use the first and last lines:
x = np.array([[1., 2., 0., 4., 5., 6., 0., 8., 9.],
[0., 2., 0., 0., 5., 6., 7., 8., 9.],
[0., 2., 0., 4., 5., 0., 7., 0., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 9.],
[1., 2., 3., 4., 5., 0., 7., 8., 9.],
[0., 2., 0., 4., 5., 6., 0., 8., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 9.],
[1., 2., 0., 4., 5., 6., 7., 8., 0.]])
res = (x == 3)
print(np.where(res * x)[0])
Output:
[4]
The full response to np.where() is:
(array([4], dtype=int64), array([2], dtype=int64))
So if you wanted both the column and the row number, you could use both of these.

python - numpy - summation of numpy arrays of different dimension

Suppose we create an numpy array like this:
x = np.linspace(1,5,5).reshape(-1,1)
which results in this:
array([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.]])
now we add the transpose of this array to it:
x + x.T
which results in this:
array([[ 2., 3., 4., 5., 6.],
[ 3., 4., 5., 6., 7.],
[ 4., 5., 6., 7., 8.],
[ 5., 6., 7., 8., 9.],
[ 6., 7., 8., 9., 10.]])
I don't understand this because the two arrays have different dimensions (5x1 and 1x5) and I learned in linear algebra that we can only sum up matrices when they have the same dimension.
Edit: OK thanks, got it
Here, we have
x = array([[ 1.],[ 2.],[ 3.],[ 4.],[ 5.]])
x.T = array([[ 1., 2., 3., 4., 5.]])
Now you are trying to add two matrices of different dimensions (1 X 5) and (5 X 1).
The way numpy handles this is by copying elements in each row of 1st matrix across its columns to match a number of columns of 2nd matrix and copying elements in each column of 2nd matrix across its rows to match no. of rows of 1st matrix. This gives you 2 5 X 5 matrix which can be added together.
The element wise addition is done as
array([[ 1., 1., 1., 1., 1.],[ 2., 2., 2., 2., 2.,],[ 3., 3., 3., 3., 3.,],[4., 4., 4., 4., 4.],[ 5., 5., 5., 5., 5.,]]) + array([[ 1., 2., 3., 4., 5.],[ 1., 2., 3., 4., 5.],[ 1., 2., 3., 4., 5.],[ 1., 2., 3., 4., 5.],[ 1., 2., 3., 4., 5.]])
which produces the result
array([[ 2., 3., 4., 5., 6.],
[ 3., 4., 5., 6., 7.],
[ 4., 5., 6., 7., 8.],
[ 5., 6., 7., 8., 9.],
[ 6., 7., 8., 9., 10.]])

Multivariate (3D) Interpolation Using Vandermonde Matrix in Python

I can't seem to figure out how to implement the Vandermonde Matrix into Multivariate Interpolation. I am able to get the actual matrix, but I don't understand how to get the values (array) c00,c01,c02... . I know that c = V/z, but I feel like I am missing something (perhaps, not division?). I also know that I need to somehow set up a system of equations (the columns of V are each cij).
How do you do this in python?
Here is what I have so far:
import numpy as np
x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
z = [3.2, 4.4, 6.5, 2.5, 4.7, 5.8, 5.1, 3.6, 2.9]
numpy.polynomial.polynomial.polyvander2d(x, y, [2,2])
>>>array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 2., 4., 1., 2., 4., 1., 2., 4.],
[ 1., 3., 9., 1., 3., 9., 1., 3., 9.],
[ 1., 1., 1., 2., 2., 2., 4., 4., 4.],
[ 1., 2., 4., 2., 4., 8., 4., 8., 16.],
[ 1., 3., 9., 2., 6., 18., 4., 12., 36.],
[ 1., 1., 1., 3., 3., 3., 9., 9., 9.],
[ 1., 2., 4., 3., 6., 12., 9., 18., 36.],
[ 1., 3., 9., 3., 9., 27., 9., 27., 81.]])
np.dot(V, c.flat)and numpy.polynomial.polynomial.polyval2d(x, y, c) I think have to be incorporated into this somehow, but I don't know what to do. Please help!
I am supposed to output:
c = V \ z
c =
0.97500
-5.27500
5.95000
-3.92500
19.82500
-21.55000
3.40000
-14.70000
18.50000
Here is the site where I got this example (They used MatLab):
https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/05Interpolation/multi/
Hope this helps!
Given positions x and y:
x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
and function values z:
z = [3.2, 4.4, 6.5, 2.5, 4.7, 5.8, 5.1, 3.6, 2.9]
V will be the Vandermonde matrix:
V = numpy.polynomial.polynomial.polyvander2d(x, y, [2,2])
V = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 2., 4., 1., 2., 4., 1., 2., 4.],
[ 1., 3., 9., 1., 3., 9., 1., 3., 9.],
[ 1., 1., 1., 2., 2., 2., 4., 4., 4.],
[ 1., 2., 4., 2., 4., 8., 4., 8., 16.],
[ 1., 3., 9., 2., 6., 18., 4., 12., 36.],
[ 1., 1., 1., 3., 3., 3., 9., 9., 9.],
[ 1., 2., 4., 3., 6., 12., 9., 18., 36.],
[ 1., 3., 9., 3., 9., 27., 9., 27., 81.]])
Each row:
a = x[i]
b = y[i]
V[i,:] = [ 1, b, b², a, a*b, a*b², a², a²b, a²b²]
A linear interpolation aims to solve:
$$z = V \cdot c$$
therefore we need to solve:
$$c = V^{-1} z$$
c = np.linalg.solve(V, z)
c now holds the coefficients in the same orders as the Vandermonde matrix does.
You can evaluate it manually:
def poly_eval(x, y, z):
return z[0] + z[1]*y + z[2]*np.power(y,2) + z[3]*x + z[4]*x*y + z[5]*x*np.power(y,2) + z[6]*np.power(x,2) + z[7]*np.power(x,2)*y + z[8]*np.power(x,2)*np.power(y,2)
or use
np.polynomial.polynomial.polyval2d([1,2], [3,4], c)
Out[22]: array([-65.5, -88.4])

Categories