Showing posts with label numpy. Show all posts
Showing posts with label numpy. Show all posts

4/16/2023

Food order forecast by RandomForestRegressor, DecisionTreeRegressor, LinearRegression

 refer to code:



.

from sklearn.metrics import r2_score,mean_squared_error
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
from math import sqrt
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

features = pd.read_csv('./features.csv')
label = pd.read_csv('./label.csv')

features.head()
label.head()

#------------------------------------ data split ---------------------------------------------
X_train,X_test,y_train,y_test = train_test_split(features,label,test_size=0.20,random_state=33)

#------------------------------------ RandomForestRegressor ---------------------------------------------
RFRmodel = RandomForestRegressor(max_depth=3, random_state=0)
RFRmodel.fit(X_train,y_train)
y_pred = RFRmodel.predict(X_test)

print('RandomForestRegressor')
print("R2 score :",r2_score(y_test, y_pred))
print("MSE score :",mean_squared_error(y_test, y_pred))
print("RMSE: ",sqrt(mean_squared_error(y_test, y_pred)))
print('')

#------------------------------------ DecisionTreeRegressor---------------------------------------------
DTRmodel = DecisionTreeRegressor(max_depth=3,random_state=0)
DTRmodel.fit(X_train,y_train)
y_pred = DTRmodel.predict(X_test)

print('DecisionTreeRegressor')
print("R2 score :",r2_score(y_test, y_pred))
print("MSE score :",mean_squared_error(y_test, y_pred))
print("RMSE: ",sqrt(mean_squared_error(y_test, y_pred)))
print('')

#------------------------------------ LinearRegression ---------------------------------------------
model = LinearRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

print('LinearRegression')
print("R2 score :",r2_score(y_test, y_pred))
print("MSE score :",mean_squared_error(y_test, y_pred))
print("RMSE: ",sqrt(mean_squared_error(y_test, y_pred)))
print('')



..


You can download dataset from here:

https://www.marearts.com/Tea-Time-Computer-Vision-6bc925c53d46412691096825bfe0317a?p=004ca41eee0948c49f979016b6a31de8&pm=s


Thank you.

www.marearts.com

πŸ™‡πŸ»‍♂️

example source code of python for converting numpy ndarray to pandas dataframe

 refer to code:



.

import numpy as np
import pandas as pd

# Create a numpy ndarray
array = np.random.rand(5, 3)
print('array: \n', array)

# Convert the numpy ndarray to a pandas DataFrame
df = pd.DataFrame(array, columns=['Column1', 'Column2', 'Column3'])

# Print the DataFrame
print('df: \n',df)

..



Thank you.

www.marearts.com

πŸ™‡πŸ»‍♂️

2/18/2023

How to Vertically Stack Multiple Arrays Using numpy.vstack in Python

 refer to code:


.

import numpy as np

# Example arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([7, 8, 9])

# Create a list of arrays
array_list = [a, b, c]

# Vertically stack all arrays in the list
result = np.empty((0, a.shape[0]))
for arr in array_list:
result = np.vstack((result, arr))

# Print the vertically stacked array
print(result)

..



Thank you.

πŸ™‡πŸ»‍♂️

www.marearts.com

2/16/2023

Optical Flow Estimation, How might you calculate the velocity/speed of an object from a ".flo" output

refer to code 

..

import cv2
import numpy as np

# Load the .flo file using OpenCV
flow = cv2.readOpticalFlow('path/to/flow_file.flo')

# Convert displacement vectors to velocity vectors
time_interval = 1.0 # Time interval between frames in seconds
velocity = flow / time_interval

# Load the corresponding frames of the video
frame1 = cv2.imread('path/to/frame1.jpg')
frame2 = cv2.imread('path/to/frame2.jpg')

# Identify object pixels using object detection or segmentation
object_mask = np.zeros(frame1.shape[:2], dtype=np.uint8)
object_mask[...] = 255 # Example: assume the entire frame is the object

# Compute the magnitude of the velocity vector at each object pixel
object_velocity = np.sqrt(np.square(velocity[..., 0]) + np.square(velocity[..., 1])) * object_mask

# Calculate the average speed of the object
object_speed = np.mean(object_velocity[object_mask != 0])

print('Object speed:', object_speed, 'pixels per second')

..




In this example, we first load the .flo file using the cv2.readOpticalFlow() function from OpenCV. We then convert the displacement vectors in the .flo file to velocity vectors by dividing them by the time interval between the two frames. We assume a time interval of 1 second in this example.

Next, we load the corresponding frames of the video and identify the object pixels using a mask. In this example, we assume that the entire frame is the object for simplicity.

We then compute the magnitude of the velocity vector at each object pixel using the np.sqrt() and np.square() functions from NumPy. We apply the object mask to exclude pixels that do not belong to the object.

Finally, we calculate the average speed of the object by averaging the magnitudes of the velocity vectors over all the pixels corresponding to the object using the np.mean() function.

Note that the units of the speed will be in pixels per second, which can be converted to other units (e.g., meters per second) depending on the scale of the video frames. Also, this is just a simple example and you may need to modify it depending on the specific requirements of your application.


Thank you.

2/07/2023

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

 refer to code:


use

tensor.detach().numpy()

..

import torch

tensor1 = torch.tensor([1.0,2.0],requires_grad=True)

print(tensor1)
print(type(tensor1))

tensor1 = tensor1.detach().numpy()

print(tensor1)
print(type(tensor1))

..


Thank you.


9/20/2021

convert numpy.ndarray object to float


*Here is object dtype numpy.ndarray
print(oneD_pt.dtype, type(oneD_pt), oneD_pt)
> object <class 'numpy.ndarray'> [3 2 1 2 2] 

* convert float dtype numpy.ndarray
oneD_pt=oneD_pt.astype('float')
print(oneD_pt.dtype, type(oneD_pt), oneD_pt)
> float64 <class 'numpy.ndarray'> [3. 2. 1. 2. 2.]


thank you.

6/26/2021

matplotlib plt to cv2

This example code is based on plt.pie drawing.

But you can apply any drawing way, just refer to how to be converted plt.fig 2 Numpy(cv2).


..

    import matplotlib.pyplot as plt
fig = plt.figure()
plt.pie(ratio, labels = mylabels, colors = mycolors) #, radius=180)
def get_img_from_fig(fig, dpi=180):
import io
buf = io.BytesIO()
fig.savefig(buf, format="png", dpi=dpi)
buf.seek(0)
img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8)
buf.close()
img = cv2.imdecode(img_arr, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

return img

plot_img_np = get_img_from_fig(fig)

cv2.namedWindow('palette')
cv2.imshow('palette', plot_img_np)
cv2.waitKey(0)

..



Thank you.

πŸ™‡πŸ»‍♂️

3/17/2020

get unique value from list (python source code)

..
import numpy as np

def unique(list1):
x = np.array(list1)
x = np.unique(x)
return list(x)

list1 = [10, 20, 10, 30, 40, 40]
list1 = unique(list1)
print(list1)
..
output
[10, 20, 30, 40]
..

10/19/2019

Byte 2 opencv Mat


refer to below source code. ^^

import base64
import numpy as np
import cv2

def byte2Mat(data):
    imgdata = base64.b64decode(data)
    nparr = np.frombuffer(imgdata, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    print(img.shape)
    
    return img    

9/18/2018

How to make Mat in opencv python

newMat_3ch = np.zeros((rows, cols, 3), dtype = "uint8") #3channel
newMat_1ch = np.zeros((rows, cols), dtype = "uint8") #1channel

Mat is just numpy array.

8/20/2018

python numpy to CSV, numpy to pandas, pandas to CSV

Sample code for
- numpy to CSV
- numpy to pandas
- pandas to CSV



import numpy as np
import pandas as pd

f1_numpy = "./data/test1.csv"
f2_pandas = "./data/test2.csv"

#numpy to csv
np.savetxt(f1_numpy, np.array([10,20]))
print(f1_numpy)

#numpy to pandas and csv
pda = pd.DataFrame(np.array([10,20]), columns=['data'])
pda.to_csv(f2_pandas, index=False)

Thank you.

8/10/2018

3D array numpy -> pandas ->csv -> pandas -> 3d array numpy

This article is example source code for
3D array numpy -> pandas -> csv -> pandas -> 3D array numpy

Let's see step by step


Step 1, make example data

import numpy as np
import pandas as pd


#make list
a = [[11, 12, 13, 14, 15], [15, 16, 17, 18, 19]]
b = [[21, 22, 23, 24, 25], [25, 26, 27, 28, 29]]
c = []
c.append(a)
c.append(b)
#make numpy
npa = np.array(c)
print('npa\n',npa)
print('npa shape\n',npa.shape) #2 by 2 by 5


result
npa
 [[[11 12 13 14 15]
  [15 16 17 18 19]]

 [[21 22 23 24 25]
  [25 26 27 28 29]]]
npa shape
 (2, 2, 5)


Step 2, numpy to pandas
#make numpy to panda
m,n,r = npa.shape
#numpy ->group indexing, reshape
out_arr = np.column_stack((np.repeat(np.arange(m),n),npa.reshape(m*n,-1)))
out_df = pd.DataFrame(out_arr, columns=['group','a','b','c','d','e'])
print('pnadas\n',out_df) #pandas

result

group   a   b   c   d   e
0      0  11  12  13  14  15
1      0  15  16  17  18  19
2      1  21  22  23  24  25
3      1  25  26  27  28  29


Step 3, save csv, load csv

#save to csv
out_df.to_csv('test3Dpandas.csv', index=False)
#load csv
df = pd.read_csv('test3Dpandas.csv')


Step 4, pandas to numpy

#pandas to numpy
npb = df.values
npb = npb[:,1:]
npb2 = npb.reshape(m,n,r)
print('numpy\n',npb2)

result

numpy
 [[[11 12 13 14 15]
  [15 16 17 18 19]]

 [[21 22 23 24 25]
  [25 26 27 28 29]]]







1/12/2018

python list, numpy slicing

Oh.. I seem to be old.. I need a memo everything..
This is memo for me about list slicing.

nums = list(range(5))
print(nums)      #[0, 1, 2, 3, 4]
print(nums[2:4]) #[2, 3]
print(nums[2:])  #[2, 3, 4] 
print(nums[:2])  #[0, 1]
print(nums[:])   #[0, 1, 2, 3, 4]
print(nums[:-1]) #[0, 1, 2, 3]
nums[2:4] = [8,9]
print(nums)      #[0, 1, 8, 9, 4]

result
[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]


numpy slicing
import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a[1:3]) #array [2 3]
print(a[-1]) #5
a[0:2] = 9
print(a) #array [9 9 3 4 5]
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(b)
#array
#[[ 1  2  3  4]
# [ 5  6  7  8]
# [ 9 10 11 12]]

print(b[:,1]) #array [2 6 10]
print(b[-1])      #array [9 10 11 12]
print(b[-1,:])    #array [9 10 11 12]
print(b[-1, ...]) #array [9 10 11 12]

print(b[0:2, :])
#array#[[1 2 3 4]# [5 6 7 8]]
πŸ˜€

6/27/2014

python + opencv study -> class making, opencv and numpy simple usages,

I made simple image subtraction class by python + opencv.
More detail, the class evaluate whether two image is same or diffrent by 2 threshold.
first threshold is the britness different of pixel.
second threshold is percent of change. eg. count(changed pixel) / area(width*height)

This class can be applied detection of motion in continues image.

And you can study how to run opencv in the python.
I am also bigginer of python use.

I studied a part of relation numpy and opencv.

class_ImgSubtraction.py
--
__author__ = 'mare'


import numpy as np
import cv2


class ImgSubtraction:
    #image load
    def __init__(self, r_img, th1, th2):
        self.RImg = r_img
        self.Th1, self.Th2 = th1, th2
        self.cols, self.rows = r_img.shape[:2]
        self.area = self.cols * self.rows

    #image subtraction
    def eval_subtraction(self, c_img):

        #return false if c_img size is different with RImg
        if self.RImg.shape[:2] != c_img.shape[:2]:
            return 0

        ic_img = c_img
        #subtraction
        is_img = np.subtract(self.RImg, np.int_(ic_img))
        #abs
        ia_img = np.abs(is_img)
        #count pixels difference over than th1
        dcount = np.sum(ia_img > self.Th1)
        #image change percent
        dpersent = (dcount/np.float32(self.area) ) * 100

        if dpersent >= self.Th2:
            return 1
        else:
            return 0

--

main.py
--
__author__ = 'mare'


import cv2
from class_ImgSubtraction import ImgSubtraction


RImg = cv2.imread('test.png', 0)
CImg = cv2.imread('test2.png', 0)

e1 = cv2.getTickCount()

cImgSub = ImgSubtraction(RImg, 10, 1)


if cImgSub.eval_subtraction(CImg):
    print ('image different')

e2 = cv2.getTickCount()
time = (e2 - e1)/cv2.getTickFrequency()
print(time, 1/time)

cv2.waitKey(0)

--

you can also download the source code on the github
-> https://gist.github.com/mare90/2ea9b9ca7c80c8c259e1