resizing image with numpy - python

lets say i have an image presented as this numpy array:
array([[ 55, 229, 185, 21, 128, 50, 109, 121, 251],
[138, 0, 143, 153, 22, 244, 102, 6, 63],
[250, 235, 57, 28, 220, 15, 217, 147, 70],
[121, 164, 128, 224, 56, 206, 104, 87, 154],
[232, 51, 20, 235, 8, 200, 119, 234, 180],
[182, 79, 79, 22, 221, 233, 54, 11, 209],
[249, 64, 92, 70, 167, 151, 214, 188, 213]], dtype=uint8)
this is 7X9 matrix and i want to double the width of the image to 7x18.
i know what to do when you want to compress an image, but im not sure what i supposed to do if i want to increase the size.
thanks!
`

Put your array in a, then
np.repeat(a, 2, axis=1)
gives
array([[ 55, 55, 229, 229, 185, 185, 21, 21, 128, 128, 50, 50, 109,
109, 121, 121, 251, 251],
[138, 138, 0, 0, 143, 143, 153, 153, 22, 22, 244, 244, 102,
102, 6, 6, 63, 63],
[250, 250, 235, 235, 57, 57, 28, 28, 220, 220, 15, 15, 217,
217, 147, 147, 70, 70],
[121, 121, 164, 164, 128, 128, 224, 224, 56, 56, 206, 206, 104,
104, 87, 87, 154, 154],
[232, 232, 51, 51, 20, 20, 235, 235, 8, 8, 200, 200, 119,
119, 234, 234, 180, 180],
[182, 182, 79, 79, 79, 79, 22, 22, 221, 221, 233, 233, 54,
54, 11, 11, 209, 209],
[249, 249, 64, 64, 92, 92, 70, 70, 167, 167, 151, 151, 214,
214, 188, 188, 213, 213]])
Which has shape 7x18.

Related

How to apply slicing to a pandas DataFrame? [duplicate]

This question already has answers here:
How to slice a pandas DataFrame by position?
(5 answers)
Closed 24 days ago.
I am trying to replace the following code:
DfInt['Closest Service'] = DfInt[
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201,
202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216,
217, 218, 219, 220, 221, 222, 223]
].idxmin(axis=1)
by something like
DfInt['Closest Service'] = DfInt[[0:224]].idxmin(axis=1)
But this is not working... Anyone an idea?
If need select by labels use DataFrame.loc, : is for select all rows:
DfInt['Closest Service'] = DfInt.loc[:, :223].idxmin(axis=1)
If select by positions - first 223 columns use DataFrame.iloc by 224:
DfInt['Closest Service'] = DfInt.iloc[:, :224].idxmin(axis=1)

convert image from [0.0, 1.0] to [0, 255]

Suppose the image x consists of floats in the range [0, 1],
Torchvision adopts the transform of clip(x*255+0.5, 0, 255).as(uint8) .
Skimage seems similar to torch
TensorFlow uses an asymmetric approach
Details on the conversion follow below.
However, while investigating a few things, I found that this method gives an unfairly small chance for values of 0 and 255 compared to other values.
Why do these machine learning libraries use these unfair transformations?
pytorch
https://pytorch.org/vision/main/_modules/torchvision/utils.html#save_image
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(X * 255 + 0.5, 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {17: [0, 255], 32: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 190, 191, 192, 193, 194, 195, 196, 198, 199, 200, 201, 202, 203, 204, 205, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 246, 248, 249, 250, 251, 252, 253, 254], 33: [8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247]})
skimage https://scikit-image.org/docs/dev/user_guide/data_types.html
from skimage.util import img_as_ubyte
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
for K, V in Counter(img_as_ubyte(np.linspace(0/256, 256/256, 32 * 256).reshape(-1, 1, 1)).reshape(-1)).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {17: [0, 255], 32: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 190, 191, 192, 193, 194, 195, 196, 198, 199, 200, 201, 202, 203, 204, 205, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 246, 248, 249, 250, 251, 252, 253, 254], 33: [8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247]})
tensorflow https://www.tensorflow.org/api_docs/python/tf/image/convert_image_dtype
import tensorflow as tf
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
img = tf.convert_to_tensor(np.linspace(0/256, 256/256, 32 * 256).reshape(-1, 1, 1))
img = tf.image.convert_image_dtype(img, dtype=tf.uint8, saturate=False)
img = tf.reshape(img, -1).numpy()
for K, V in Counter(img).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {33: [0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238], 32: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254], 17: [255]})
my suggestion 1
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(np.rint(X * 256 - 0.5), 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {32: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]})
my suggestion 2
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(X * 256, 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {32: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]})

Using Naive Bayes to do multi classification

I have a dataset as below:
data = [[92, 155],
[56, 186, 117, 210, 224],
[247, 202, 189, 210, 65, 3, 270, 224],
[20, 14, 157, 224],
[17, 89, 158, 224],
[263, 283, 68, 224],
[182, 166, 224],
[176, 37, 100, 224],
[33, 102, 41, 269, 177, 224],
[0, 260, 49, 207, 278, 217, 35],
[119],
[118],
[142, 185, 7, 246, 224],
[104, 22, 101, 224],
[84, 205, 224],
[225, 93, 54, 224],
[98, 32, 78, 224],
[159, 217, 212, 198, 224],
[178, 94, 187, 224],
[211, 149, 193, 149, 66, 139, 67, 28, 106, 224],
[133, 151],
[259, 109, 29, 224],
[215, 241, 73, 255, 77, 144, 224],
[36, 254, 19, 268, 183, 224],
[47, 234, 203, 111, 231, 141, 30],
[127, 275, 220, 161],
[214, 267, 22, 90, 224],
[46, 217, 103],
[17, 89, 128, 224],
[225, 22, 101, 224],
[285, 265, 151],
[215, 206, 264, 43, 224],
[244, 21, 224],
[82, 122, 240, 5, 224],
[259, 136, 162, 194, 224],
[176, 208, 112, 224],
[172, 19, 146, 276, 31, 246, 51, 224],
[45, 10],
[229, 24, 224],
[143, 108, 239, 224],
[225, 282, 83, 224],
[110, 267, 171],
[176, 245, 95, 123, 270, 224],
[248, 195, 139, 261, 173, 281, 232, 80, 18, 224],
[61, 60, 233],
[211, 120, 1, 23],
[225, 267, 249, 224],
[247, 202, 86, 196, 224],
[15, 127, 222, 224],
[247, 202, 186, 226, 145, 224],
[174, 242, 196, 224],
[259, 152, 71, 224],
[235, 44, 230, 224],
[69, 96, 50, 99, 116],
[259, 279, 224],
[228, 70],
[39, 139, 201, 190, 224],
[132, 40, 219, 81, 224],
[159, 221, 224],
[267, 16, 6, 62],
[143, 59, 175, 129, 48, 224],
[280, 140, 224],
[284, 124, 167, 150, 274],
[113, 265, 184],
[179, 4, 257, 145, 224],
[247, 202, 72, 11, 224],
[64],
[192, 125, 105],
[174, 134, 224],
[58, 139, 85, 160, 209, 224],
[130, 169, 137, 256, 224],
[215, 163, 265, 185, 26],
[176, 147, 74, 224],
[0, 266],
[143, 34, 153, 188, 224],
[121],
[243, 75, 135],
[38, 218, 199, 253, 224],
[178, 271, 224],
[154, 164, 180, 27, 270, 224],
[176, 189, 148, 139, 277, 224],
[57, 62],
[91, 168, 251, 224],
[172, 19, 146, 276, 53, 97, 200, 224],
[64],
[8, 237, 224],
[138, 107, 224],
[176, 238, 224],
[204, 217, 63, 165, 224],
[215, 216, 272, 62, 170, 2, 55, 224],
[247, 273, 202, 223, 9, 148, 224],
[258, 267, 181, 224],
[262, 76, 126, 12, 224],
[36, 254, 19, 268, 250, 213, 48, 224],
[227, 42],
[79, 197, 52, 87, 224],
[143, 131, 224],
[156, 88, 115, 236, 224],
[259, 13, 252, 224],
[114, 25, 191, 224]]
target = [9,
31,
20,
9,
3,
26,
16,
11,
28,
0,
9,
9,
9,
9,
7,
1,
33,
9,
13,
15,
9,
21,
9,
34,
9,
9,
9,
9,
3,
1,
9,
27,
14,
22,
21,
11,
17,
9,
6,
8,
1,
9,
11,
9,
9,
9,
1,
20,
29,
20,
23,
21,
9,
9,
21,
9,
18,
9,
9,
30,
8,
9,
9,
9,
9,
20,
9,
32,
23,
9,
24,
9,
11,
9,
8,
9,
9,
9,
13,
10,
11,
9,
12,
17,
9,
5,
9,
11,
9,
2,
20,
9,
25,
34,
9,
9,
8,
4,
21,
19]
I wish to do classification by using naive bayes or any other BEST algorithms available. However, I received error when using naive bayes as below:
from sklearn.naive_bayes import MultinomialNB
mnb = MultinomialNB(class_prior=[.25,.75])
mnb.fit(data, target)
Errros:
ValueError: Expected 2D array, got 1D array instead:
array=[list([92, 155]) list([56, 186, 117, 210, 224])
list([247, 202, 189, 210, 65, 3, 270, 224]) list([20, 14, 157, 224])
list([17, 89, 158, 224]) list([263, 283, 68, 224]) list([182, 166, 224])
list([176, 37, 100, 224]) list([33, 102, 41, 269, 177, 224])
list([0, 260, 49, 207, 278, 217, 35]) list([119]) list([118])
list([142, 185, 7, 246, 224]) list([104, 22, 101, 224])
list([84, 205, 224]) list([225, 93, 54, 224]) list([98, 32, 78, 224])
list([159, 217, 212, 198, 224]) list([178, 94, 187, 224])
list([211, 149, 193, 149, 66, 139, 67, 28, 106, 224]) list([133, 151])
list([259, 109, 29, 224]) list([215, 241, 73, 255, 77, 144, 224])
list([36, 254, 19, 268, 183, 224])
list([47, 234, 203, 111, 231, 141, 30]) list([127, 275, 220, 161])
list([214, 267, 22, 90, 224]) list([46, 217, 103])
list([17, 89, 128, 224]) list([225, 22, 101, 224]) list([285, 265, 151])
list([215, 206, 264, 43, 224]) list([244, 21, 224])
list([82, 122, 240, 5, 224]) list([259, 136, 162, 194, 224])
list([176, 208, 112, 224]) list([172, 19, 146, 276, 31, 246, 51, 224])
list([45, 10]) list([229, 24, 224]) list([143, 108, 239, 224])
list([225, 282, 83, 224]) list([110, 267, 171])
list([176, 245, 95, 123, 270, 224])
list([248, 195, 139, 261, 173, 281, 232, 80, 18, 224])
list([61, 60, 233]) list([211, 120, 1, 23]) list([225, 267, 249, 224])
list([247, 202, 86, 196, 224]) list([15, 127, 222, 224])
list([247, 202, 186, 226, 145, 224]) list([174, 242, 196, 224])
list([259, 152, 71, 224]) list([235, 44, 230, 224])
list([69, 96, 50, 99, 116]) list([259, 279, 224]) list([228, 70])
list([39, 139, 201, 190, 224]) list([132, 40, 219, 81, 224])
list([159, 221, 224]) list([267, 16, 6, 62])
list([143, 59, 175, 129, 48, 224]) list([280, 140, 224])
list([284, 124, 167, 150, 274]) list([113, 265, 184])
list([179, 4, 257, 145, 224]) list([247, 202, 72, 11, 224]) list([64])
list([192, 125, 105]) list([174, 134, 224])
list([58, 139, 85, 160, 209, 224]) list([130, 169, 137, 256, 224])
list([215, 163, 265, 185, 26]) list([176, 147, 74, 224]) list([0, 266])
list([143, 34, 153, 188, 224]) list([121]) list([243, 75, 135])
list([38, 218, 199, 253, 224]) list([178, 271, 224])
list([154, 164, 180, 27, 270, 224]) list([176, 189, 148, 139, 277, 224])
list([57, 62]) list([91, 168, 251, 224])
list([172, 19, 146, 276, 53, 97, 200, 224]) list([64])
list([8, 237, 224]) list([138, 107, 224]) list([176, 238, 224])
list([204, 217, 63, 165, 224]) list([215, 216, 272, 62, 170, 2, 55, 224])
list([247, 273, 202, 223, 9, 148, 224]) list([258, 267, 181, 224])
list([262, 76, 126, 12, 224]) list([36, 254, 19, 268, 250, 213, 48, 224])
list([227, 42]) list([79, 197, 52, 87, 224]) list([143, 131, 224])
list([156, 88, 115, 236, 224]) list([259, 13, 252, 224])
list([114, 25, 191, 224])].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
Please can anyone please help me with this? Or can someone show me some other best examples of algorithms to use for machine learning like decision tree, svm or anything.
So the direct answer to why you're getting an error is that you're passing an array of lists as an argument. Sklearn thus think you're passing in a 1D array of lists. It is not possible to transform your data to a 2D matrix because the number of values in your list in inconsistent.
From my understanding (which could be wrong), each row of your input feature matrix need to have the same amount of numbers. Given this is satisfied, then you should be able to feed your data into MultinomialNB no problem.
Consider padding with zeros:
data1 = np.zeros((len(data), 10))
for i in range(len(data)):
data1[i, :len(data[i])] = data[i]

numpy - make repeated random number blocks (noise image) [duplicate]

This question already has answers here:
How to repeat elements of an array along two axes?
(5 answers)
Quick way to upsample numpy array by nearest neighbor tiling [duplicate]
(3 answers)
Closed 4 years ago.
I want to make a "noise" image. If I do
img = np.random.randint(0, 255, (4, 4), dtype=np.uint8)
print(img)
out:
array([[150, 45, 246, 137],
[195, 141, 246, 197],
[206, 126, 188, 76],
[134, 168, 166, 190]])
Every pixel is different. But what if I want larger 'pixels', e.g.:
array([[150, 150, 246, 246],
[150, 150, 246, 246],
[206, 206, 188, 188],
[206, 206, 188, 188]])
How do I do something like this?
You can use np.kron:
>>> np.kron(np.random.randint(0, 256, (4, 4)), np.ones((2, 2), int))
array([[252, 252, 51, 51, 10, 10, 124, 124],
[252, 252, 51, 51, 10, 10, 124, 124],
[161, 161, 137, 137, 8, 8, 89, 89],
[161, 161, 137, 137, 8, 8, 89, 89],
[ 12, 12, 24, 24, 37, 37, 98, 98],
[ 12, 12, 24, 24, 37, 37, 98, 98],
[151, 151, 149, 149, 147, 147, 15, 15],
[151, 151, 149, 149, 147, 147, 15, 15]])
Or np.repeat (once for each dimension):
>>> np.repeat(np.repeat(np.random.randint(0, 256, (4, 4)), 2, 0), 2, 1)
array([[ 41, 41, 29, 29, 103, 103, 67, 67],
[ 41, 41, 29, 29, 103, 103, 67, 67],
[231, 231, 203, 203, 231, 231, 157, 157],
[231, 231, 203, 203, 231, 231, 157, 157],
[ 18, 18, 126, 126, 15, 15, 196, 196],
[ 18, 18, 126, 126, 15, 15, 196, 196],
[198, 198, 152, 152, 74, 74, 211, 211],
[198, 198, 152, 152, 74, 74, 211, 211]])

How to convert byte array to picture [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do I convert my byte array to a picture? I want it to be in saved JPG or BMP format, not just displayed as text or on the console.
This is sample array:
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, 0, 72, 0, 0, 255, 219, 0, 67, 0, 14, 10, 11, 13, 11, 9, 14, 13, 12, 13, 16, 15, 14, 17, 22, 36, 23, 22, 20, 20, 22, 44, 32, 33, 26, 36, 52, 46, 55, 54, 51, 46, 50, 50, 58, 65, 83, 70, 58, 61, 78, 62, 50, 50, 72, 98, 73, 78, 86, 88, 93, 94, 93, 56, 69, 102, 109, 101, 90, 108, 83, 91, 93, 89, 255, 219, 0, 67, 1, 15, 16, 16, 22, 19, 22, 42, 23, 23, 42, 89, 59, 50, 59, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 255, 192, 0, 17, 8, 0, 67, 0, 90, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 27, 0, 0, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 0, 2, 4, 6, 1, 7, 255, 196, 0, 46, 16, 0, 2, 2, 1, 3, 4, 0, 6, 0, 6, 3, 0, 0, 0, 0, 0, 1, 2, 0, 3, 17, 4, 18, 33, 19, 49, 65, 81, 5, 20, 34, 50, 97, 129, 35, 51, 66, 82, 113, 161, 145, 177, 241, 255, 196, 0, 25, 1, 0, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 3, 0, 5, 255, 196, 0, 32, 17, 0, 3, 1, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 17, 3, 33, 49, 4, 18, 65, 50, 81, 97, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 64, 23, 230, 43, 64, 123, 146, 48, 33, 151, 109, 78, 89, 185, 43, 244, 129, 234, 99, 171, 80, 90, 157, 152, 25, 7, 32, 226, 93, 89, 152, 41, 83, 245, 99, 13, 159, 18, 55, 254, 138, 57, 125, 101, 122, 132, 167, 40, 168, 16, 99, 129, 222, 69, 189, 107, 185, 171, 80, 25, 118, 131, 144, 115, 156, 192, 81, 131, 65, 177, 215, 42, 190, 120, 154, 180, 250, 122, 41, 2, 203, 47, 80, 196, 239, 36, 140, 136, 97, 82, 237, 5, 231, 140, 211, 165, 109, 129, 54, 18, 135, 183, 7, 17, 169, 212, 181, 40, 173, 96, 37, 79, 5, 189, 69, 86, 87, 180, 51, 171, 6, 243, 149, 237, 147, 54, 86, 83, 229, 16, 171, 51, 59, 113, 98, 191, 111, 212, 51, 78, 31, 125, 29, 245, 251, 46, 134, 212, 178, 218, 172, 235, 219, 196, 191, 76, 17, 145, 21, 211, 99, 212, 249, 175, 236, 30, 12, 105, 70, 173, 44, 93, 165, 72, 97, 46, 226, 249, 42, 186, 126, 146, 242, 112, 53, 218, 6, 213, 64, 189, 115, 122, 128, 227, 142, 227, 188, 27, 215, 42, 86, 77, 80, 208, 177, 235, 129, 233, 254, 35, 23, 174, 7, 100, 209, 80, 152, 112, 122, 95, 133, 88, 250, 94, 169, 179, 146, 50, 20, 119, 48, 26, 230, 21, 88, 136, 203, 176, 168, 236, 59, 159, 243, 49, 117, 110, 166, 210, 155, 217, 27, 177, 195, 120, 158, 239, 107, 92, 189, 132, 187, 118, 201, 158, 59, 149, 233, 234, 173, 28, 252, 57, 27, 90, 198, 189, 202, 170, 6, 78, 79, 113, 25, 184, 85, 78, 141, 149, 45, 71, 111, 117, 60, 17, 57, 154, 153, 171, 96, 245, 185, 87, 95, 34, 48, 249, 195, 171, 176, 29, 77, 228, 17, 129, 192, 238, 34, 185, 77, 96, 87, 79, 78, 155, 73, 69, 107, 163, 168, 217, 118, 75, 14, 63, 38, 21, 43, 61, 82, 21, 51, 248, 204, 207, 162, 27, 40, 110, 144, 123, 213, 120, 33, 187, 16, 61, 67, 13, 90, 223, 96, 122, 198, 8, 238, 177, 111, 142, 91, 218, 120, 141, 38, 158, 98, 44, 247, 109, 109, 140, 155, 88, 113, 159, 115, 222, 166, 208, 48, 199, 62, 79, 169, 109, 37, 226, 250, 109, 107, 16, 99, 171, 129, 159, 56, 255, 0, 201, 235, 168, 119, 42, 7, 62, 132, 202, 184, 190, 189, 203, 208, 167, 189, 52, 30, 141, 83, 161, 250, 142, 76, 217, 166, 212, 245, 9, 15, 199, 168, 156, 0, 167, 151, 24, 252, 201, 212, 193, 36, 28, 48, 154, 71, 45, 194, 79, 240, 202, 162, 95, 76, 126, 193, 76, 15, 76, 123, 139, 244, 218, 227, 140, 62, 61, 77, 95, 48, 159, 221, 44, 143, 149, 45, 118, 77, 92, 31, 209, 242, 64, 25, 156, 177, 28, 137, 161, 114, 216, 68, 4, 177, 30, 60, 192, 86, 72, 76, 159, 50, 201, 97, 22, 6, 4, 140, 30, 49, 49, 101, 33, 129, 193, 43, 130, 8, 134, 210, 186, 163, 146, 232, 31, 140, 115, 50, 245, 9, 39, 39, 36, 249, 133, 76, 240, 124, 67, 157, 4, 102, 150, 220, 41, 10, 150, 176, 172, 182, 72, 7, 180, 53, 90, 183, 210, 53, 157, 59, 3, 2, 177, 117, 119, 109, 98, 15, 57, 148, 123, 13, 231, 10, 112, 1, 201, 49, 90, 213, 140, 100, 240, 125, 240, 91, 152, 216, 149, 217, 119, 76, 31, 63, 153, 208, 116, 159, 78, 253, 71, 110, 165, 64, 99, 32, 114, 63, 83, 153, 248, 78, 152, 234, 237, 8, 70, 43, 79, 184, 231, 152, 254, 212, 170, 141, 37, 148, 37, 238, 89, 148, 133, 70, 96, 73, 63, 129, 30, 97, 53, 184, 35, 166, 186, 60, 179, 54, 218, 203, 75, 86, 219, 70, 67, 19, 156, 254, 160, 116, 203, 103, 68, 245, 173, 47, 110, 123, 145, 142, 34, 202, 236, 178, 187, 48, 114, 174, 135, 4, 71, 58, 166, 83, 66, 217, 253, 88, 236, 61, 153, 55, 29, 43, 151, 45, 102, 15, 200, 156, 180, 244, 206, 199, 248, 187, 43, 5, 136, 245, 39, 92, 249, 99, 42, 157, 55, 168, 216, 44, 41, 98, 248, 50, 163, 86, 216, 28, 87, 251, 19, 47, 4, 211, 131, 15, 244, 224, 152, 90, 233, 118, 165, 173, 82, 187, 83, 190, 76, 207, 129, 220, 96, 201, 146, 20, 228, 247, 241, 46, 195, 131, 212, 195, 60, 241, 152, 69, 99, 187, 129, 218, 100, 76, 130, 8, 239, 53, 212, 113, 201, 61, 160, 163, 130, 237, 12, 64, 108, 224, 247, 34, 71, 57, 33, 107, 224, 123, 131, 107, 25, 92, 21, 238, 124, 120, 158, 179, 251, 63, 84, 84, 16, 160, 90, 78, 90, 246, 3, 200, 94, 39, 77, 240, 199, 210, 232, 168, 172, 82, 203, 110, 170, 252, 2, 65, 201, 231, 223, 224, 78, 91, 113, 11, 140, 126, 230, 173, 14, 165, 180, 215, 173, 136, 161, 136, 247, 26, 107, 5, 164, 118, 90, 141, 62, 158, 203, 1, 101, 27, 148, 100, 145, 231, 159, 48, 26, 183, 85, 96, 120, 13, 216, 204, 21, 124, 75, 115, 90, 109, 82, 166, 204, 5, 2, 94, 219, 69, 141, 147, 140, 227, 7, 62, 102, 124, 183, 56, 243, 214, 116, 39, 189, 148, 102, 57, 35, 141, 179, 211, 91, 103, 238, 31, 238, 2, 195, 140, 159, 18, 163, 81, 102, 62, 227, 255, 0, 50, 83, 70, 142, 67, 204, 186, 114, 192, 30, 210, 73, 61, 1, 2, 40, 195, 15, 243, 46, 126, 252, 120, 245, 36, 145, 25, 199, 172, 126, 185, 226, 242, 78, 121, 146, 73, 223, 135, 4, 63, 202, 111, 212, 215, 167, 39, 253, 73, 36, 74, 240, 43, 211, 161, 214, 86, 155, 40, 59, 70, 70, 63, 234, 101, 14, 193, 73, 7, 28, 201, 36, 199, 155, 249, 6, 60, 51, 218, 236, 119, 101, 143, 136, 61, 199, 220, 146, 64, 135, 71, 255, 217]
Each MIME type has a signature(magic number). By first bytes its a JPEG img.
# your array
arr = [255, 216, 255, 224, 0, ...]
>>> bytearray(arr[:4])
bytearray(b'\xff\xd8\xff\xe0')
FF D8 FF E0 - its a jpeg signature image.
I tried:
f = open('/tmp/myimage.jpeg', 'wb')
f.write(bytearray(arr))
f.close()
and got a next image:

Categories

Resources