I'm using a camera to store raw data in a numpy array, but I don't know What does mean a colon before a number in numpy array?
import numpy as np
import picamera
camera = picamera.PiCamera()
camera.resolution = (128, 112)
data = np.empty((128, 112, 3), dtype=np.uint8)
camera.capture(data, 'rgb')
data = data[:128, :112]
numpy array indexing is explained in the doc.
this example shows what is selected:
import numpy as np
data = np.arange(64).reshape(8, 8)
print(data)
data = data[:3, :5]
print(data)
the result will be the first 5 elements of the first 3 rows of the array.
as in standard python lst[:3] means everything up to the third element (i.e. the element with index < 3). in numpy you can do the same for every dimension with the syntax given in your question.
Related
I have 6 different images. I want to store them together in a single numpy array. Is that possible? If yes, how can I do that?
from PIL import Image
from matplotlib import image
import matplotlib.pyplot as plt
from os import listdir
from numpy import asarray
import numpy as np
for i in range(1,6):
image=Image.open(str(i)+'.jpg')
image=image.resize((100,100))
temp=asarray(image)
print(np.append(X_train,temp,axis=0))
This raises the following Exception:
ValueError: all the input arrays must have same number of dimensions
you can create a list of arrays and the convert back to numpy array
list_of_pics = list()
for i in range(1,6):
image=Image.open(str(i)+'.jpg')
image=image.resize((100,100))
list_of_pics.append(np.asarray(image))
new_array = np.array(list_of_pics)
the final dimentions of new_array should be (6,100,100)
I'm basically sorting my CNN images into a list with even and odd indexing. Even index will have positive images and odd index will have negative images. Here's my code so far:
from PIL import Image
import matplotlib.pyplot as plt
import os
import glob
import torch
from torch.utils.data import Dataset
def show_data(data_sample, shape = (28, 28)):
plt.imshow(data_sample[0].numpy().reshape(shape), cmap='gray')
plt.title('y = ' + data_sample[1])
directory="/resources/data"
negative='Negative'
negative_file_path=os.path.join(directory,negative)
negative_files=[os.path.join(negative_file_path,file) for file in os.listdir(negative_file_path) if file.endswith(".jpg")]
negative_files.sort()
negative_files[0:3]
positive="Positive"
positive_file_path=os.path.join(directory,positive)
positive_files=[os.path.join(positive_file_path,file) for file in os.listdir(positive_file_path) if file.endswith(".jpg")]
positive_files.sort()
positive_files[0:3]
n = len(negative_files)
p = len(positive_files)
number_of_samples = n + p
print(number_of_samples)
Y=torch.zeros([number_of_samples])
Y=Y.type(torch.LongTensor)
Y.type()
Y[::2]=1
Y[1::2]=0
Replace the code with:
directory="resources/data/"
RGB data. How to calculate and sort them on Python, OpenCV
I want to work on Python, OpenCV these below steps
1. Get the RGB data from pictures
2. Calculate the R*G*B on each pixel of the pictures
3. Sort the data by descending order and plot them on graph or csv
4. Get the max and min and medium of R*G*B
I could handle that the step1. as below code.
However, I don’t know how to write a program after step2
It's better to save the data as csv or numpy
Does anybody have an idea? Please help me. it would be very helpful if you show me the code.
import cv2
import numpy
im_f = np.array(Image.open('data/image.jpg'), 'f')
print(im[:, :])
It is better to keep data in-memory as numpy array. Also, read image using cv2.imread rather than Image.open if it has to be converted to np.array eventually.
For plotting, matplotlib can be used.
Here is how the above mentioned process can be achieved using OpenCV, numpy and matplotlib.
import numpy as np
import cv2, sys
import matplotlib.pyplot as plt
#Read image
im_f = cv2.imread('data/image.jpg')
#Validate image
if im_f is None:
print('Image Not Found')
sys.exit();
#Cast to float type to hold the results
im_f = im_f.astype(np.float32)
#Compute the product of channels and flatten the result to get 1D array
product = (im_f[:,:,0] * im_f[:,:,1] * im_f[:,:,2]).flatten()
#Sort the flattened array and flip it to get elements in descending order
product = np.sort(product)[::-1]
#Compute the min, max and median of product
pmin, pmax , pmed = np.amin(product), np.amax(product), np.median(product)
print('Min = ' + str(pmin))
print('Max = ' + str(pmax))
print('Med = ' + str(pmed))
#Show the sorted array
plt.plot(product)
plt.show()
Tested with Python 3.5.2, OpenCV 4.0.1, numpy 1.15.4, and matplotlib 3.0.2 on Ubuntu 16.04.
I have a pandas dataframe with two columns. One of the columns values needs to be mapped to colors in hex. Another graphing process takes over from there.
This is what I have tried so far. Part of the toy code is taken from here.
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
# Create dataframe
df = pd.DataFrame(np.random.randint(0,21,size=(7, 2)), columns=['some_value', 'another_value'])
# Add a nan to handle realworld
df.iloc[-1] = np.nan
# Try to map values to colors in hex
# # Taken from here
norm = matplotlib.colors.Normalize(vmin=0, vmax=21, clip=True)
mapper = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
df['some_value_color'] = df['some_value'].apply(lambda x: mapper.to_rgba(x))
df
Which outputs:
How do I convert 'some_value' df column values to hex in one go?
Ideally using the sns.cubehelix_palette(light=1)
I am not opposed to using something other than matplotlib
Thanks in advance.
You may use matplotlib.colors.to_hex() to convert a color to hexadecimal representation.
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import seaborn as sns
# Create dataframe
df = pd.DataFrame(np.random.randint(0,21,size=(7, 2)), columns=['some_value', 'another_value'])
# Add a nan to handle realworld
df.iloc[-1] = np.nan
# Try to map values to colors in hex
# # Taken from here
norm = matplotlib.colors.Normalize(vmin=0, vmax=21, clip=True)
mapper = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
df['some_value_color'] = df['some_value'].apply(lambda x: mcolors.to_hex(mapper.to_rgba(x)))
df
Efficiency
The above method it easy to use, but may not be very efficient. In the folling let's compare some alternatives.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def create_df(n=10):
# Create dataframe
df = pd.DataFrame(np.random.randint(0,21,size=(n, 2)),
columns=['some_value', 'another_value'])
# Add a nan to handle realworld
df.iloc[-1] = np.nan
return df
The following is the solution from above. It applies the conversion to the dataframe row by row. This quite inefficient.
def apply1(df):
# map values to colors in hex via
# matplotlib to_hex by pandas apply
norm = mcolors.Normalize(vmin=np.nanmin(df['some_value'].values),
vmax=np.nanmax(df['some_value'].values), clip=True)
mapper = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
df['some_value_color'] = df['some_value'].apply(lambda x: mcolors.to_hex(mapper.to_rgba(x)))
return df
That's why we might choose to calculate the values into a numpy array first and just assign this array as the newly created column.
def apply2(df):
# map values to colors in hex via
# matplotlib to_hex by assigning numpy array as column
norm = mcolors.Normalize(vmin=np.nanmin(df['some_value'].values),
vmax=np.nanmax(df['some_value'].values), clip=True)
mapper = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
a = mapper.to_rgba(df['some_value'])
df['some_value_color'] = np.apply_along_axis(mcolors.to_hex, 1, a)
return df
Finally we may use a look up table (LUT) which is created from the matplotlib colormap, and index the LUT by the normalized data. Because this solution needs to create the LUT first, it is rather ineffienct for dataframes with less entries than the LUT has colors, but will pay off for large dataframes.
def apply3(df):
# map values to colors in hex via
# creating a hex Look up table table and apply the normalized data to it
norm = mcolors.Normalize(vmin=np.nanmin(df['some_value'].values),
vmax=np.nanmax(df['some_value'].values), clip=True)
lut = plt.cm.viridis(np.linspace(0,1,256))
lut = np.apply_along_axis(mcolors.to_hex, 1, lut)
a = (norm(df['some_value'].values)*255).astype(np.int16)
df['some_value_color'] = lut[a]
return df
Compare the timings
Let's take a dataframe with 10000 rows.
df = create_df(10000)
Original solution (apply1)
%timeit apply1(df)
2.66 s per loop
Array solution (apply2)
%timeit apply2(df)
240 ms per loop
LUT solution (apply3)
%timeit apply1(df)
7.64 ms per loop
In this case the LUT solution gives almost a factor 400 of improvement.
I am trying to produce all combination of numpy array that satisfy a condition efficiently my code now looks like this
import numpy as np
import itertools
a = np.array([1,11,12,13])
a = np.tile(a,(13,1))
a = a.flatten()
for c in itertools.combinations(a,4):
if np.sum(c)==21:
print(c)
If you only care about unique combinations (and there are only 256 of them), you can use itertools.product:
version_1 = np.vstack(list(sorted({tuple(row) for row in list(itertools.combinations(a, 4))}))) # unique combinations, your way
version_2 = np.array(list(itertools.product((1, 11, 12, 13), repeat=4))) # same result, but faster
assert (version_1 == version_2).all()
I'm using this answer to get the unique elements of a Numpy array.
So the final answer would be:
import itertools, numpy as np
a = np.array(list(itertools.product((1, 11, 12, 13), repeat=4)))
for arr in a[a.sum(axis=1) == 21]:
print(arr)