text:
#!/usr/bin/python

import pickle
import numpy
import sys
sys.path.append('../tools')
import dos2unix as d2u
numpy.random.seed(42)

### The words (features) and authors (labels), already largely processed.
### These files should have been created from the previous (Lesson 10)
### mini-project.
words_file = "../text_learning/your_word_data.pkl"
authors_file = "../text_learning/your_email_authors.pkl"
word_data = d2u.pickle_load(words_file)
authors = d2u.pickle_load(authors_file)

### test_size is the percentage of events assigned to the test set (the
### remainder go into training)
### feature matrices changed to dense representations for compatibility with
### classifier functions in versions 0.15.2 and earlier
from sklearn import cross_validation
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(word_data, authors, test_size=0.1, random_state=42)

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
                             stop_words='english')
features_train = vectorizer.fit_transform(features_train)
features_test  = vectorizer.transform(features_test).toarray()

### a classic way to overfit is to use a small number
### of data points and a large number of features;
### train on only 150 events to put ourselves in this regime
features_train = features_train[:150].toarray()
labels_train   = labels_train[:150]

### your code goes here
from sklearn.tree import DecisionTreeClassifier
DECISION_TREE = DecisionTreeClassifier()
DECISION_TREE.fit(features_train, labels_train)
print(f"scored: {DECISION_TREE.score(features_test, labels_test)}")

IMPORTANCES = DECISION_TREE.feature_importances_
INDICIES = numpy.argsort(IMPORTANCES)[::-1]
VOCAB = vectorizer.vocabulary_

print("Top 10 features:")
i = 0
for f in range(features_train.shape[1]):
    if i > 10: break
    string = (
        f"feature: {INDICIES[f]}, "
        f"importance: {IMPORTANCES[INDICIES[f]]:.2%}, "
        f"word: {list(VOCAB.keys())[list(VOCAB.values()).index(INDICIES[f])]}"
    )
    print(string)
    i += 1


text:
import cv2
import numpy as np

img = cv2.imread('messi5.jpg',0)
img2 = img.copy()
template = cv2.imread('messi_face.jpg',0)
w, h = template.shape[::-1]
# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
for meth in methods:
    img = img2.copy()
    method = eval(meth)
    # Apply template Matching
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv2.rectangle(img,top_left, bottom_right, 255, 2)
    cv2.imshow("img", img)
    cv2.normalize(res,res)
    cv2.imshow("res", res)
    cv2.waitKey()

ext: py

reason: import numpy


text:
import csv
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

from matplotlib.ticker import FormatStrFormatter
from tensorflow.keras import backend as K

def load_data_from_images(image_path, datasplit):
    """Load images from directory

    Parameters:
    image_path (str): Path to images
    datasplit (str): Choose 'train', 'valid', or 'test'

    Returns:
    DataFrame: table with path, label, and dataset description for each image
    numpy array: image matrices
    numpy array: corresponding labels

    """
    image_path = image_path
    data = {
        "Path": [
                 glob.glob(f"{image_path}/{datasplit}/asteroids/" + '*'),
                 glob.glob(f"{image_path}/{datasplit}/other/" + '*')
                ],
        "Label": [1,0],
        "Set": datasplit
         }
    df = pd.DataFrame(data).explode('Path')
    df = df.sample(frac=1, random_state=35) #shuffle
    x = []
    y = []
    for i, file in enumerate(df['Path']):
        im = Image.open(file)
        im = np.asarray(im)
        x.append(im)
        y.append(df['Label'].iloc[i])

    return df, np.array(x, dtype=int), np.array(y, dtype=float)

def crop_center(im, new_w, new_h):
    """
    Crop center of image
    """
    width, height = im.size   # Get dimensions

    left = (width - new_w)/2
    top = (height - new_h)/2
    right = (width + new_w)/2
    bottom = (height + new_h)/2

    # Crop the center of the image
    im = im.crop((left, top, right, bottom))

    return im

def f1_metric(y_true, y_pred):
    """
    F1 metric that combines precision and recall
    """

    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    recall = true_positives / (possible_positives + K.epsilon())
    f1_val = 2 * (precision * recall) / (precision + recall + K.epsilon())

    return f1_val

def analyze_5unit_errors(predictionsLabel, Y_testLabel): #,image_size=20
    """
    Compares predictions of labels and coordinates to ground truth
    """
    tp = 0
    tn = 0
    fp = 0
    fn = 0
    for i in range(len(predictionsLabel)):
        trueLabel = Y_testLabel[i]
        predictedLabel = round(predictionsLabel[i])
        if trueLabel == 1 and predictedLabel == 1:
            tp += 1
        elif trueLabel == 0 and predictedLabel == 0:
            tn += 1
        elif trueLabel == 0 and predictedLabel == 1:
            fp += 1
        elif trueLabel == 1 and predictedLabel == 0:
            fn += 1
        else:
            pass
    accuracy = (tp + tn) / len(predictionsLabel) * 100
    precision = tp / (tp + fp) * 100
    recall = tp / (tp + fn) * 100

    print("\nClassification accuracy, precision, recall:", f"{accuracy:.2f}", f"{precision:.2f}", f"{recall:.2f}")
    print("TP, TN, FP, FN:", tp, tn, fp, fn)

    return f"\nClassification accuracy, precision, recall: {accuracy:.2f} {precision:.2f} {recall:.2f}\n TP, TN, FP, FN: {tp} {tn} {fp} {fn}"

def convert_pixels_to_arcsec_per_h(pixels):
    """
    Converts streak length in pixels to arcsec/h.
    Values are specific to ESA Euclid mission.
    """

    coefficient = 565/3600*10
    arcsecPerH = pixels/coefficient

    return arcsecPerH

def plot_results_heatmap(dataframe2D, binsMag, title, fig_name = 'Plot_2D_histogram_CNN.pdf', savepdf=True):
    """
    Plots the recall (completeness of predictions as a heatmap).
    Saves the heatmap to a pdf.
    """
    cmap = "PRGn"
    xticklabels = np.append(dataframe2D.columns.values.round(0).astype(int), 80)
    yticklabels = np.append(dataframe2D.index.values.round(2), 26.0)
    fig4, ax4 = plt.subplots(figsize=(10, 6), dpi=100)
    sns.heatmap(dataframe2D,
                ax=ax4,
                cmap=cmap,
                annot=True,
                fmt='3.0f',
                cbar_kws={'label': 'Completeness [%]'},
                xticklabels=xticklabels,
                yticklabels=yticklabels,
                vmin=0,
                vmax=100,
                linewidths=0.5)
    ax4.set_ylim(0, binsMag)
    ax4.set_title(title)
    ax4.set_xlabel(r'Sky motion [$\rm arcsec\,h^{-1}$]')
    ax4.set_ylabel('Magnitude')
    ax4.yaxis.set_major_formatter(FormatStrFormatter('%5.2f'))
    yticks = ax4.get_yticks() - 0.5
    ax4.set_yticks(yticks)
    xticks = ax4.get_xticks() - 0.5
    ax4.set_xticks(xticks)
    ax4.set_xticklabels(ax4.get_xticklabels(), rotation=0)  # , horizontalalignment='right')
    ax4.set_yticklabels(ax4.get_yticklabels(), rotation=0)
    #PRGn
    #PuBuGn
    fig4.tight_layout()
    if savepdf:
        fig4.savefig(fig_name, dpi=300, format="pdf")
    return

max_stars_repo_name: ymzayek/asteroidsCNN

max_issues_repo_name: ymzayek/asteroidsCNN

max_forks_repo_name: ymzayek/asteroidsCNN


text:
# -*- coding: utf-8 -*-
"""Numpy and Pandas.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/17Ow1VLaE2H5cThFIkqs0dAI13JzYUgQW

# Python Basic operations
"""

class Disease:
  def __init__(self, disease = 'Depression'):
    self.type = disease

  def getName(self):
    print("Mental Health Diseases: {0}".format(self.type))

d1 = Disease('Social Anxiety Disorder')
d1.getName()

# Try Catch Block
# The try block will generate a NameError, because x is not defined:

try:
  print(y)
except NameError:
  print("Well, the variable y is not defined")
except:
  print("OMG, Something else went wrong")

try:
 Value = int(input("Type a number between 1 and 10:"))
except ValueError:
  print("You must type a number between 1 and 10!")
else:
  if (Value > 0) and (Value <= 10):
      print("You typed: ", Value)
  else:
      print("The value you typed is incorrect!")

"""# Numpy Array Basics"""

# importing numpy
import numpy as np

# Defining 1D array
my1DArray = np.array([1, 8, 27, 64])
print(my1DArray)

# Defining and printing 2D array
my2DArray = np.array([[1, 2, 3, 4], [2, 4, 9, 16], [4, 8, 18, 32]])
print(my2DArray)

#Defining and printing 3D array
my3Darray = np.array([[[ 1, 2 , 3 , 4],[ 5 , 6 , 7  ,8]], [[ 1,  2,  3,  4],[ 9, 10, 11, 12]]])
print(my3Darray)

# Print out memory address
print(my2DArray.data)

# Print the shape of array
print(my2DArray.shape)

# Print out the data type of the array
print(my2DArray.dtype)

"""
Print the stride of the array.

So what is stride of array?  It is the number of locations in memory between
beginnings of successive array elements, measured in bytes or in units of the
size of the array's elements
"""
print(my2DArray.strides)

"""# Array using numpy built-in functions"""

# Array of ones
ones = np.ones((3,4))
print(ones)

# Array of zeros
zeros = np.zeros((2,3,4),dtype=np.int16)
print(zeros)

# Array with random values
np.random.random((2,2))

# Empty array
emptyArray = np.empty((3,2))
print(emptyArray)

# Full array
fullArray = np.full((2,2),7)
print(fullArray)

# Array of evenly-spaced values
evenSpacedArray = np.arange(10,25,5)
print(evenSpacedArray)

# Array of evenly-spaced values
evenSpacedArray2 = np.linspace(0,2,9)
print(evenSpacedArray2)

"""# Numpy array and file operations"""

# Save a numpy array into file
x = np.arange(0.0,50.0,1.0)
np.savetxt('data.out', x, delimiter=',')

# Loading numpy array from text
z = np.loadtxt('data.out', unpack=True)
print(z)

# Loading numpy array using genfromtxt method
my_array2 = np.genfromtxt('data.out',
                      skip_header=1,
                      filling_values=-999)
print(my_array2)

"""# Inspecting numpy arrays"""

# Print the number of `my2DArray`'s dimensions
print(my2DArray.ndim)

# Print the number of `my2DArray`'s elements
print(my2DArray.size)

# Print information about `my2DArray`'s memory layout
print(my2DArray.flags)

# Print the length of one array element in bytes
print(my2DArray.itemsize)

# Print the total consumed bytes by `my2DArray`'s elements
print(my2DArray.nbytes)

"""# Broadcasting in NumPy
Broadcasting is a mechanism that permits `NumPy` to operate with arrays of different shapes when performing arithmetic operations.
"""

# Rule 1: Two dimensions are operatable if they are equal
# Create an array of two dimension
A =np.ones((6, 8))

# Shape of A
print(A.shape)

# Create another array
B = np.random.random((6,8))

# Shape of B
print(B.shape)

# Sum of A and B, here the shape of both the matrix is same.
print(A + B)

# Rule 2: Two dimensions are also comptable when one of them is 1
# Initialize `x`
x = np.ones((3,4))
print(x)

# Check shape of `x`
print(x.shape)

# Initialize `y`
y = np.arange(4)
print(y)

# Check shape of `y`
print(y.shape)

# Subtract `x` and `y`
x - y

# Rule 3: Arrays can be broadcasted together if they are compatible in all dimensions

x = np.ones((6,8))
y = np.random.random((10, 1, 8))
print(x + y)

# Analytical question
"""
The dimensions of x(6,8) and y(10,1,8) are diffrent. However, it is possible
to add them. Why is that? Also, change y(10,2,8) or y(10,1,4) it will
give ValueError. Can you find out why?
"""

"""# Numpy and mathematics at work"""

# Basica operations (+, -, *, /, %)
x = np.array([[1, 2, 3], [2, 3, 4]])
y = np.array([[1, 4, 9], [2, 3, -2]])

# Add two array
add = np.add(x, y)
print(add)

# Subtract two array
sub = np.subtract(x, y)
print(sub)

# Multipley two array
mul = np.multiply(x, y)
print(mul)

# Divide x, y
div = np.divide(x,y)
print(div)

# Calculated the remainder of x and y
rem = np.remainder(x, y)
print(rem)

"""# Subset, Slice, And Index Arrays"""

x = np.array([10, 20, 30, 40, 50])

# Select items at index 0 and 1
print(x[0:2])

# Select item at row 0 and 1 and column 1 from 2D array
y = np.array([[ 1,  2,  3,  4], [ 9, 10, 11 ,12]])
print(y[0:2, 1])

# Specifying conditions
biggerThan2 = (y >= 2)
print(y[biggerThan2])

# Importing pandas
import pandas as pd

"""# Can you set default parameters in Pandas?"""

print("Pandas Version:", pd.__version__)

pd.set_option('display.max_columns', 500)
pd.set_option('display.max_rows', 500)

"""# Data strcutre of pandas

*   Series
*   DataFrames
"""

series = pd.Series([2, 3, 7, 11, 13, 17, 19, 23])
print(series)

# Creating dataframe from Dictionary
dict_df = [{'A': 'Apple', 'B': 'Ball'},{'A': 'Aeroplane', 'B': 'Bat', 'C': 'Cat'}]
dict_df = pd.DataFrame(dict_df)
print(dict_df)

# Creating dataframe from Series
series_df = pd.DataFrame({
    'A': range(1, 5),
    'B': pd.Timestamp('20190526'),
    'C': pd.Series(5, index=list(range(4)), dtype='float64'),
    'D': np.array([3] * 4, dtype='int64'),
    'E': pd.Categorical(["Depression", "Social Anxiety", "Bipolar Disorder", "Eating Disorder"]),
    'F': 'Mental health',
    'G': 'is challenging'
})
print(series_df)

# Creating a dataframe from ndarrays
sdf = {
    'County':['Østfold', 'Hordaland', 'Oslo', 'Hedmark', 'Oppland', 'Buskerud'],
    'ISO-Code':[1,2,3,4,5,6],
    'Area': [4180.69, 4917.94, 454.07, 27397.76, 25192.10, 14910.94],
    'Administrative centre': ["Sarpsborg", "Oslo", "City of Oslo", "Hamar", "Lillehammer", "Drammen"]
    }
sdf = pd.DataFrame(sdf)
print(sdf)

"""# Loading a dataset into Pandas DataFrame"""

import pandas as pd
columns = ['age', 'workclass', 'fnlwgt', 'education', 'education_num',
    'marital_status', 'occupation', 'relationship', 'ethnicity', 'gender','capital_gain','capital_loss','hours_per_week','country_of_origin','income']
df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data',names=columns)
df.head(10)

# Displays the rows, columns, data types and memory used by the dataframe
df.info()

# Displays the no of data points and columns in the dataframe
df.shape

# Display all columns of the dataframe
df.columns

# Displays summary statistics for each numerical column in the dataframe
df.describe()

"""# Selecting rows and columns in the dataframe"""

# Selects a row
df.iloc[10]

# Selects 10 rows
df.iloc[0:10]

# Selects a range of rows
df.iloc[10:15]

 # Selects the last 2 rows
df.iloc[-2:]

# Selects every other row in columns 3-5
df.iloc[::2, 3:5].head()

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'F': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 5), columns=list('EDCBA'))],
               axis=1)
df.iloc[0, 2] = np.nan
df

# Define a function that should color the values that are less than 0
def colorNegativeValueToRed(value):
  if value < 0:
    color = 'red'
  elif value > 0:
    color = 'black'
  else:
    color = 'green'

  return 'color: %s' % color

s = df.style.applymap(colorNegativeValueToRed, subset=['A','B','C','D','E'])
s

# Let us hightlight max value in the column with green background and min value with orange background
def highlightMax(s):
    isMax = s == s.max()
    return ['background-color: orange' if v else '' for v in isMax]

def highlightMin(s):
    isMin = s == s.min()
    return ['background-color: green' if v else '' for v in isMin]

df.style.apply(highlightMax).apply(highlightMin).highlight_null(null_color='red')

import seaborn as sns

cm = sns.light_palette("pink", as_cmap=True)

s = df.style.background_gradient(cmap=cm)
s

class Disease:
  def __init__(self, disease = 'Depression'):
    self.type = disease

  def getName(self):
    print("Mental Health Diseases: {0}".format(self.type))

d1 = Disease('Social Anxiety Disorder')
d1.getName()

try:
  Value = int(input("Type a number between 1 and 10:"))
except ValueError:
   print("You must type a number between 1 and 10!")
else:
   if (Value > 0) and (Value <= 10):
       print("You typed: ", Value)
   else:
       print("The value you typed is incorrect!")

max_stars_repo_path: Chapter 1/numpy_and_pandas.py

max_issues_repo_path: Chapter 1/numpy_and_pandas.py

max_forks_repo_path: Chapter 1/numpy_and_pandas.py


text:
#this is data analysis function

import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import re
from numpy import NaN
import glob
l = [pd.read_csv(filename, sep=",", encoding="ISO-8859-1") for filename in glob.glob("rent_data/*.csv")]
rent_df = pd.concat(l, ignore_index=True)
rent_df = rent_df.drop_duplicates(subset=["Headline"]).reset_index(drop=True)
rent_df['Housing'] = rent_df['Housing'].astype(str)

pattern = re.compile('([0-9]+)\s?(bed|bedroom|brd|br|Bedroom|BR)')
pattern_bed = re.compile('(..)([0-9]+)\s*(br)')

def convert_housing(row, pattern, pattern_bed):
    headline = row['Headline']
    housing = row['Housing']
    if housing == 'nan':
        if bool(pattern.search(headline)):
            housing = pattern.search(headline).group(1)
            housing = int(housing)
            return housing
        else:
            return ('NaN')
    else:
        housing = pattern_bed.search(housing).group(2)
        housing = int(housing)
        return housing

rent_df['Housing'] = rent_df.apply(convert_housing, axis=1, pattern=pattern, pattern_bed=pattern_bed)
temp_housing = []
for row in rent_df['Housing']:
    if row != 'NaN':
        temp_housing.append(row)
mean_housing = round(np.mean(temp_housing))
for i, row in enumerate(rent_df['Housing']):
    if row == 'NaN':
        rent_df['Housing'][i] = mean_housing
rent_df['Housing'] = rent_df['Housing'].astype(int)
# deal with Price variable
rent_df['Price'] = rent_df['Price'].astype(str)

pattern = re.compile('^\$\d*(\,?\d{3})*')

def convert_money(row, pattern):
    price = row['Price']
    if bool(pattern.match(price)):
        price = price.replace("$", "")
        price = price.replace(",", "")
        price = float(price)
        return price
    else:
        return (NaN)
rent_df['Price'] = rent_df.apply(convert_money, axis=1, pattern=pattern)
rent_df['Price'] = rent_df['Price'].astype(float)
# cmu is on (40.443322, -79.943583)
# range: latitude--(40.429331, 40.454349)
# range: longitude--(-79.961402, -79.914453)
# only concern the apartments inside this area range
import math
def cal_distance(row):
    latitude = row['Latitude']
    longitude = row['Longitude']
    distance = math.sqrt((latitude - 40.443322) ** 2 + (longitude - (-79.943583)) ** 2)
    if latitude >= 40.429331 and latitude <= 40.454349 and longitude >= -79.961402 and longitude <= -79.914453:
        return distance
    else:
        return (np.nan)

rent_df['Distance'] = rent_df.apply(cal_distance, axis=1)
distance_df = rent_df[['Distance', 'Price']]
distance_df = distance_df.dropna(axis=0, how='any')
# distance_df
def distance_level(row):
    distance = row['Distance']
    interval = (max(distance_df['Distance']) - min(distance_df['Distance'])) / 3
    if distance >= min(distance_df['Distance']) and distance < interval:
        level = 1
    elif distance >= interval and distance < interval * 2:
        level = 2
    else:
        level = 3
    return level

distance_df['Distance'] = distance_df.apply(distance_level, axis=1)
# general price distribution
from scipy.stats import norm
import matplotlib.mlab as mlab

x = rent_df['Price']
(mu, sigma) = norm.fit(x)
fig, ax = plt.subplots(figsize=(10, 8))

n, bins, patches = plt.hist(x, 15, normed=1, facecolor='g', alpha=0.75)
ax.axvline(np.mean(x), color='b', linestyle='dashed', linewidth=2)

# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
ax.plot(bins, y, '--')

plt.xlabel('Price')
plt.ylabel('Probability')
plt.title('Price Distribution Histogram')
plt.grid(True)
plt.show()

# price distribution by housing plan

x = rent_df['Housing']
y = rent_df['Price']

plt.figure(figsize=(10, 8))
plt.bar(x, y, color='green')

plt.grid(True)
plt.xlabel('Number of bedroom')
plt.ylabel('Price')
plt.title('Price Distribution by Housing')
plt.show()

# price distribution by housing per bedroom
# add another variable 'Price_per_bed'
rent_df['Price_per_bed'] = rent_df['Price'] / rent_df['Housing']

x = rent_df['Housing']
y = rent_df['Price_per_bed']

plt.figure(figsize=(10, 8))
plt.bar(x, y, color='green')
plt.xlabel('Number of bedroom')
plt.ylabel('Price per bedroom')
plt.title('Price (per bedroom) Distribution by Housing')
plt.grid(True)
plt.show()

import matplotlib.pylab as plb

rent_df['Available Date'] = pd.to_datetime(rent_df['Available Date'])
# rent_df['Month'] = rent_df['Available Date'].dt.month

df = pd.DataFrame(columns=('Available Date', 'Price'), data=rent_df)
df = df.sort_values(by=['Available Date'])

dates = np.array(df['Available Date'])
price = np.array(df['Price'])

price_series = Series(price, index=dates)
plt.figure(figsize=(10, 8))
price_series['2018-01-01':'2018-12-01'].plot(style='k.')
plt.ylim(0, 5000)
plt.xticks(rotation=35)
plt.xlabel('Available Date')
plt.ylabel('Price')
plt.title('Price Time Series')
plt.show()

# regress price on housing
import statsmodels.formula.api as smf
model_1 = smf.ols(formula='Price ~ C(Housing)', data=rent_df)
results_model1 = model_1.fit()
results_model1.summary()

rent_df['Price_per_bed'] = rent_df['Price_per_bed'].astype(int)
# rent_df.info()
# regress price per bedroom on housing
model_2 = smf.ols(formula='Price_per_bed ~ C(Housing)', data=rent_df)
results_model2 = model_2.fit()
results_model2.summary()

# regess price on distance
model_3 = smf.ols(formula='Price ~ C(Distance)', data=distance_df)
results_model3 = model_3.fit()
results_model3.summary()

model_3 = smf.ols(formula='Price ~ C(Month)', data=rent_df)
results_model3 = model_3.fit()
results_model3.summary()

# Final regression model for prediction
model_4 = smf.ols(formula='Price ~ C(Housing) + C(Month)', data=rent_df)
results_model4 = model_4.fit()
results_model4.summary()

reason: import numpy,from numpy,from scipy,import statsmodels


text:
import os
from collections import OrderedDict
from pathlib import Path
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import seaborn as sns

from algorithms.aaa_util import frame_loss
from datasets.mot import MOT

sns.set()
sns.set_style("whitegrid")

def calc_rank(dataset, trackers_name, scores, reverse=False):
    ranks = []
    for seq_name in dataset.sequence_names["train"]:
        value = np.array(
            [scores[tracker_name][seq_name] for tracker_name in trackers_name]
        )
        temp = value.argsort()
        if reverse:
            temp = temp[::-1]
        rank = np.empty_like(temp)
        rank[temp] = np.arange(len(value))
        rank = len(trackers_name) - rank
        ranks.append(rank)
    ranks = np.array(ranks)
    return ranks

def calc_rank_test(mota_scores, reverse=False):
    ranks = []
    for i in range(len(list(mota_scores.values())[0])):
        value = np.array(
            [mota_scores[tracker_name][i] for tracker_name in mota_scores.keys()]
        )
        temp = value.argsort()
        if reverse:
            temp = temp[::-1]
        rank = np.empty_like(temp)
        rank[temp] = np.arange(len(value))
        rank = len(mota_scores.keys()) - rank
        ranks.append(rank)
    ranks = np.array(ranks)
    return ranks

def draw_rank(eval_dir, dataset, algorithm, experts_name, result_dir, in_al=True):
    fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(10, 9))
    fig.add_subplot(111, frameon=False)

    if in_al:
        trackers_name = experts_name + [algorithm]
    else:
        trackers_name = experts_name

    xs = list(range(1, len(trackers_name) + 1))
    seq_names = []

    mota_scores = {tracker_name: {} for tracker_name in trackers_name}
    for seq in dataset:
        dataset_name = seq.seq_info["dataset_name"]
        seq_name = seq.seq_info["seq_name"]
        seq_names.append(seq_name)
        for tracker_name in trackers_name:
            error_path = eval_dir / tracker_name / dataset_name / f"{seq_name}.csv"
            errors = pd.read_csv(error_path, header=0)
            frame_error = frame_loss(errors, range(1, len(seq) + 1))
            frame_error = -frame_error.sum()
            mota_scores[tracker_name][seq_name] = frame_error

    # draw error
    ax = axes[0]
    for i, tracker_name in enumerate(trackers_name):
        ax.plot(
            range(len(seq_names)),
            [-mota_scores[tracker_name][seq_name] for seq_name in seq_names],
            label="Ours" if i == len(trackers_name) - 1 and in_al else tracker_name,
            linewidth=2,
        )
    ax.set_ylabel("Error")
    ax.legend(
        frameon=False,
        loc="upper center",
        ncol=len(trackers_name),
        bbox_to_anchor=(0.5, 1.2),
    )

    mota_ranks = calc_rank(dataset, trackers_name, mota_scores)

    # draw rank
    ax = axes[1]
    for i, (tracker_name, rank) in enumerate(zip(trackers_name, mota_ranks.T)):
        ax.plot(
            range(len(seq_names)),
            rank,
            label="Ours" if i == len(trackers_name) - 1 else tracker_name,
            linewidth=2,
        )
    ax.set_ylabel("Rank")

    # draw mota
    ax = axes[2]
    for i, (tracker_name, rank) in enumerate(zip(trackers_name, mota_ranks.T)):
        ax.plot(
            xs,
            [np.sum(rank == x) / len(dataset) for x in xs],
            label="Ours" if i == len(trackers_name) - 1 else tracker_name,
            linewidth=2,
        )

    ax.yaxis.set_major_locator(MultipleLocator(0.1))
    ax.set_xticks([1, (len(trackers_name) + 1) // 2, len(trackers_name)])
    ax.set_xticklabels(["Best", str((len(trackers_name) + 1) // 2), "Worst"])
    ax.set_ylabel("Frequency")

    # hide tick and tick label of the big axes
    plt.tick_params(
        labelcolor="none",
        which="both",
        top=False,
        bottom=False,
        left=False,
        right=False,
    )
    plt.grid(False)

    plt.subplots_adjust(wspace=None, hspace=0.2)

    save_dir = result_dir / dataset_name / algorithm
    os.makedirs(save_dir, exist_ok=True)

    plt.savefig(save_dir / "rank.pdf", bbox_inches="tight")
    plt.close()

def draw_rank_test(mota_scores, result_dir):
    fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(10, 9))
    fig.add_subplot(111, frameon=False)

    trackers_name = mota_scores.keys()
    xs = list(range(1, len(trackers_name) + 1))

    # draw mota
    ax = axes[0]
    for tracker_name in trackers_name:
        ax.plot(
            range(len(mota_scores[tracker_name])),
            mota_scores[tracker_name],
            label=tracker_name,
            linewidth=2,
        )
    ax.set_ylabel("MOTA")
    ax.legend(
        frameon=False,
        loc="upper center",
        ncol=len(trackers_name),
        bbox_to_anchor=(0.5, 1.2),
    )

    mota_ranks = calc_rank_test(mota_scores)

    # draw rank
    ax = axes[1]
    for i, (tracker_name, rank) in enumerate(zip(trackers_name, mota_ranks.T)):
        ax.plot(
            range(len(mota_scores[tracker_name])),
            rank,
            label=tracker_name,
            linewidth=2,
        )
    ax.set_ylabel("Rank")

    # draw frequency
    ax = axes[2]
    for i, (tracker_name, rank) in enumerate(zip(trackers_name, mota_ranks.T)):
        ax.plot(
            xs,
            [np.sum(rank == x) / len(mota_scores[tracker_name]) for x in xs],
            label=tracker_name,
            linewidth=2,
        )

    ax.yaxis.set_major_locator(MultipleLocator(0.1))
    ax.set_xticks([1, (len(trackers_name) + 1) // 2, len(trackers_name)])
    ax.set_xticklabels(["Best", str((len(trackers_name) + 1) // 2), "Worst"])
    ax.set_ylabel("Frequency")

    # hide tick and tick label of the big axes
    plt.tick_params(
        labelcolor="none",
        which="both",
        top=False,
        bottom=False,
        left=False,
        right=False,
    )
    plt.grid(False)

    plt.subplots_adjust(wspace=None, hspace=0.2)

    os.makedirs(result_dir, exist_ok=True)
    plt.savefig(result_dir / "rank.pdf", bbox_inches="tight")
    plt.close()

def draw_weight(
    output_dir, eval_dir, dataset, algorithm, experts_name, result_dir,
):
    for seq in dataset:
        dataset_name = seq.seq_info["dataset_name"]
        seq_name = seq.seq_info["seq_name"]

        dataset_dir = output_dir / dataset_name / algorithm

        weight_path = dataset_dir / f"{seq_name}_weight.txt"
        weights = pd.read_csv(weight_path, header=None).set_index(0)

        loss_path = dataset_dir / f"{seq_name}_loss.txt"
        loss = pd.read_csv(loss_path, header=None).set_index(0)
        cum_loss = loss.cumsum()

        trackers_name = experts_name + [algorithm]
        vis_name = experts_name + ["Ours"]

        frame_errors = []
        for tracker_name in trackers_name:
            error_path = eval_dir / tracker_name / dataset_name / f"{seq_name}.csv"
            errors = pd.read_csv(error_path, header=0)
            frame_error = frame_loss(errors, range(1, len(seq) + 1))
            frame_error = frame_error.sum(axis=1)
            frame_errors.append(frame_error)

        save_dir = result_dir / dataset_name / algorithm / seq_name
        os.makedirs(save_dir, exist_ok=True)

        fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(10, 6))
        fig.add_subplot(111, frameon=False)

        error_ax = axes[0]
        cum_error_ax = axes[1]
        weight_ax = axes[2]
        loss_ax = axes[3]

        # draw error graph
        for i in range(len(vis_name)):
            if vis_name[i] in ["DeepSORT", "MOTDT"]:
                continue
            error = frame_errors[i]
            error_ax.plot(range(len(error)), error, label=vis_name[i])
            error_ax.set(
                ylabel="Error", xlim=(0, len(seq)),
            )
        error_ax.set_xticks([])

        # draw cum error graph
        for i in range(len(vis_name)):
            error = np.cumsum(frame_errors[i])
            cum_error_ax.plot(range(len(error)), error, label=vis_name[i])
            cum_error_ax.set(
                ylabel="Cum Error", xlim=(0, len(seq)),
            )
        cum_error_ax.set_xticks([])

        # draw weight graph
        for i in weights.columns:
            weight = weights[i]
            weight_ax.plot(range(len(weight)), weight, label=vis_name[i - 1])
            weight_ax.set(
                ylabel="Weight", xlim=(0, len(seq)), ylim=(-0.05, 1.05,),
            )
        weight_ax.set_xticks([])

        # draw loss graph
        for i in cum_loss.columns:
            loss = cum_loss[i]
            loss_ax.plot(cum_loss.index, cum_loss, label=vis_name[i - 1])
            loss_ax.set(
                ylabel="Cum Loss", xlim=(0, len(seq)),
            )
        loss_ax.set_xticks([])

        # draw anchor line
        for i in loss.index:
            weight_ax.axvline(x=i, color="gray", linestyle="--", linewidth=1, alpha=0.3)
            error_ax.axvline(x=i, color="gray", linestyle="--", linewidth=1, alpha=0.3)
            cum_error_ax.axvline(
                x=i, color="gray", linestyle="--", linewidth=1, alpha=0.3
            )
            loss_ax.axvline(x=i, color="gray", linestyle="--", linewidth=1, alpha=0.3)

        error_ax.legend(
            ncol=len(vis_name),
            frameon=False,
            loc="upper center",
            bbox_to_anchor=(0.5, 1.3),
        )

        # hide tick and tick label of the big axes
        plt.axis("off")
        plt.grid(False)
        plt.subplots_adjust(wspace=0, hspace=0)
        plt.savefig(save_dir / "weight.pdf", bbox_inches="tight")
        plt.close()

def draw_weight_test(
    output_dir, dataset, algorithm, experts_name, result_dir,
):
    for seq in dataset:
        dataset_name = seq.seq_info["dataset_name"]
        seq_name = seq.seq_info["seq_name"]

        dataset_dir = output_dir / dataset_name / algorithm

        weight_path = dataset_dir / f"{seq_name}_weight.txt"
        weights = pd.read_csv(weight_path, header=None).set_index(0)

        loss_path = dataset_dir / f"{seq_name}_loss.txt"
        loss = pd.read_csv(loss_path, header=None).set_index(0)
        cum_loss = loss.cumsum()

        vis_name = experts_name + ["Ours"]

        save_dir = result_dir / dataset_name / algorithm / seq_name
        os.makedirs(save_dir, exist_ok=True)

        fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10, 6))
        fig.add_subplot(111, frameon=False)

        weight_ax = axes[0]
        loss_ax = axes[1]

        # draw weight graph
        for i in weights.columns:
            weight = weights[i]
            weight_ax.plot(range(len(weight)), weight, label=vis_name[i - 1])
            weight_ax.set(
                ylabel="Weight", xlim=(0, len(seq)), ylim=(-0.05, 1.05,),
            )
        weight_ax.set_xticks([])

        # draw loss graph
        for i in cum_loss.columns:
            loss = cum_loss[i]
            loss_ax.plot(cum_loss.index, cum_loss, label=vis_name[i - 1])
            loss_ax.set(
                ylabel="Cum Loss", xlim=(0, len(seq)),
            )
        loss_ax.set_xticks([])

        # draw anchor line
        for i in loss.index:
            weight_ax.axvline(x=i, color="gray", linestyle="--", linewidth=1, alpha=0.3)
            loss_ax.axvline(x=i, color="gray", linestyle="--", linewidth=1, alpha=0.3)

        weight_ax.legend(
            ncol=len(vis_name),
            frameon=False,
            loc="upper center",
            bbox_to_anchor=(0.5, 1.3),
        )

        # hide tick and tick label of the big axes
        plt.axis("off")
        plt.grid(False)
        plt.subplots_adjust(wspace=0, hspace=0)
        plt.savefig(save_dir / "weight.pdf", bbox_inches="tight")
        plt.close()

if __name__ == "__main__":
    output_dir = Path("output")
    # eval_dir = Path("eval")
    result_dir = Path("visualize")
    dataset = MOT("/home/heonsong/Disk2/Dataset/MOT/MOT16")
    # algorithm = "{'use_gt': True, 'pre_cnn': True, 'pre_track': 'FRCNN'}, {'method': 'anchor', 'threshold': 0.5, 'score_mode': 'mvote', 'iou_mode': 'giou'}, {'type': 'stable', 'duration': 2, 'threshold': 1}, {'delayed': True, 'type': 'w_id', 'bound': 0.01}"
    # algorithm = "{'use_gt': False, 'pre_cnn': True, 'pre_track': 'FRCNN'}, {'method': 'anchor', 'threshold': 0.5, 'score_mode': 'mvote', 'iou_mode': 'giou'}, {'type': 'stable', 'duration': 70, 'threshold': 0.4}, {'delayed': True, 'type': 'w_id', 'bound': 1.0}"
    # experts_name = ["DeepMOT", "DeepSORT", "MOTDT", "Tracktor", "UMA"]
    # draw_rank(eval_dir, dataset, algorithm, experts_name, result_dir, in_al=False)
    # draw_weightNerror_graph(
    #     output_dir, eval_dir, dataset, algorithm, experts_name, result_dir
    # )

    mota_scores = OrderedDict({
        "AMIR15": [29.1475, 73.6842, 47.0387, 37.5716, 36.6835, 50.4409, 34.5465, 45.0926, 32.9013, 36.15, 40.5863, 24.9302][::-1],
        "AP_HWDPL_p": [39.1409, 61.2523, 38.8549, 38.4863, 34.2879, 40.682, 39.3729, 52.8971, 34.1974, 28.4415, 43.2815, 35.2568][::-1],
        "KCF": [35.7659, 76.0436, 51.8307, 38.8965, 33.7264, 53.0276, 28.1635, 50.4139, 31.5055, 40.0951, 43.0061, 25.5642][::-1],
        "STRN": [39.7765, 67.9673, 47.9203, 38.0615, 26.8389, 38.0952, 33.841, 51.6752, 32.1037, 27.9099, 36.5434, 40.3396][::-1],
        "Tracktor++v2": [45.8032, 77.7677, 46.4993, 46.5983, 47.5201, 51.7931, 49.944, 57.6271, 43.7687, 43.005, 47.0096, 38.3194][::-1],
        "TrctrD15": [36.0728, 77.4955, 47.0698, 44.0934, 49.4853, 49.7354, 49.0929, 57.4695, 43.8684, 34.2753, 45.4948, 34.4509][::-1],
    })
    draw_rank_test(mota_scores, result_dir / "MOT15")
    mota_scores = OrderedDict({
        "GSM_Tracktor": [33.4091, 49.0416, 35.538, 47.0714, 50.0433, 68.4016, 43.4715][::-1],
        "KCF16": [29.2864, 42.8933, 28.7925, 45.8584, 48.2059, 57.1751, 36.9195][::-1],
        "MLT": [30.6714, 47.0886, 42.4688, 46.2505, 50.312, 60.4308, 46.9116][::-1],
        "PV_16": [24.0708, 45.4732, 35.9324, 41.8576, 48.7173, 59.9172, 39.8749][::-1],
        "Tracktor++v2": [32.4893, 48.1977, 33.3931, 43.567, 54.8275, 67.6585, 42.2361][::-1],
        "TrctrD16": [28.2097, 48.1615, 34.6239, 43.0891, 53.6661, 66.2946, 37.7952][::-1],
    })
    draw_rank_test(mota_scores, result_dir / "MOT16")
    mota_scores = OrderedDict({
        "FAMNet": [37.0935, 28.0312, 33.0141, 44.906, 41.7561, 42.2984, 29.0428, 24.4556, 25.6864, 49.7603, 39.3891, 44.5096, 57.6205, 56.11, 49.6775, 72.5197, 58.7265, 56.9152, 50.6667, 41.907, 40.2326][::-1],
        "GSM_Tracktor": [37.034, 34.1503, 33.4091, 45.2983, 44.4098, 47.179, 28.5031, 27.2676, 28.4321, 46.321, 43.3434, 45.7882, 53.4114, 52.8598, 49.72, 72.5856, 68.6124, 68.5111, 43.8295, 43.7674, 43.1008][::-1],
        "SRF17": [35.9465, 33.4145, 32.3919, 45.9905, 45.0098, 46.429, 27.812, 26.5291, 26.9409, 44.8055, 42.1062, 42.5679, 55.7111, 55.5923, 52.6561, 72.364, 67.9131, 67.3332, 43.1783, 43.0543, 41.845][::-1],
        "Tracktor++v2": [36.1576, 34.0313, 32.4893, 45.9444, 45.0213, 46.3136, 27.4806, 26.2782, 26.7232, 44.5096, 42.2305, 42.2838, 58.4097, 58.2315, 54.2855, 73.2658, 68.7624, 67.915, 43.845, 43.7829, 41.876][::-1],
        "TrctrD17": [28.675, 28.4802, 27.4414, 45.1713, 43.406, 45.5406, 28.7351, 27.1113, 27.0593, 41.455, 39.5844, 40.7032, 56.2118, 55.9912, 52.4864, 69.8362, 66.2947, 65.5983, 38.5426, 35.8605, 37.1783][::-1],
        "YOONKJ17": [38.2081, 21.6794, 28.3828, 41.2253, 38.6524, 40.9253, 32.3755, 25.6628, 26.7705, 47.9252, 33.6412, 39.8923, 50.3904, 49.2872, 46.0709, 76.856, 60.2599, 54.4218, 46.0155, 25.2713, 36.8062][::-1],
    })
    draw_rank_test(mota_scores, result_dir / "MOT17")

    # algorithm = "{'use_gt': False, 'pre_cnn': True, 'pre_track': 'FRCNN'}, {'method': 'anchor', 'threshold': 0.5, 'score_mode': 'mvote', 'iou_mode': 'giou'}, {'type': 'stable', 'duration': 100, 'threshold': 0.4}, {'delayed': True, 'type': 'w_id', 'bound': 1.0}"
    # experts_name = ["AMIR15", "AP_HWDPL_p", "KCF", "STRN", "Tracktor++v2", "TrctrD15"]
    # experts_name = ["GSM_Tracktor", "KCF16", "MLT", "PV_16", "Tracktor++v2", "TrctrD16"]
    # experts_name = ["FAMNet", "GSM_Tracktor", "SRF17", "Tracktor++v2", "TrctrD17", "YOONKJ17"]
    # draw_weight_test(output_dir, dataset, algorithm, experts_name, result_dir)

max_stars_repo_name: songheony/AAA-multi

max_issues_repo_name: songheony/AAA-multi

max_forks_repo_name: songheony/AAA-multi


text:
# -*- Coding: utf-8 -*-
from pylanarian import SequentialGraph
from pylanarian import GraphManager
import math
import numpy as np

def main():
    times = [0 for i in range(10)]
    sinxs = [0 for i in range(10)]
    cosxs = [0 for i in range(10)]
    g1 = SequentialGraph(title="sin", margin=0.1)
    g2 = SequentialGraph(title="cos", option=".r", margin=0.1)
    GM = GraphManager(g1, g2)
    GM.init_plot()
    t = 0
    while True:
        t += 0.5
        times.append(t)
        times.pop(0)

        sinx = math.sin(t)
        sinxs.append(sinx)
        sinxs.pop(0)

        cosx = math.cos(t)
        cosxs.append(cosx)
        cosxs.pop(0)

        GM.set_alldata(times, sinxs, cosxs)
        GM.pause(0.1)

if __name__ == '__main__':
    main()


text:
from kernel_config import kernel_bounds
from numba import cuda
import rvl_cuda
from math import sqrt, ceil
import numpy as np
import cv2
import pyrealsense2 as rs
import socket
import time
from datetime import datetime

MAX_UDP_PACKET_BYTES = 65535

class UDPSource():
    # Receives and decodes UDP packets
    def __init__(self, host, port, dim, print_pd=2.0, show_output=False, mproc=1, mpthread=128, cpu_decode=False):
        self.dim = (args.dim[1], args.dim[0])
        self.deproject = False
        self.show_output=show_output
        if cpu_decode:
            raise Exception("UDP CPU decode unimplemented")

        # NOTE: we match the details of the sender so that we get the same block & segment dimensions/counts
        # Warp: 32        Multiprocessors: 1      Thread limit per mproc: 128
        self.KB, self.BLOCKS_PER_GRID, self.THREADS_PER_BLOCK, self.NUM_SECTOR = kernel_bounds(self.dim, warp=32, mproc=mproc, sm_cores=mpthread)
        rvl_cuda.configure(self.KB, self.NUM_SECTOR)
        self.sectors_per_packet = max(1, int(MAX_UDP_PACKET_BYTES / (rvl_cuda.SECTOR_LEN * np.dtype(np.uint64).itemsize)))
        self.debug("Sector length:", rvl_cuda.SECTOR_LEN, "\tSectors per packet:", self.sectors_per_packet, "\tPackets/frame:",  int(self.NUM_SECTOR / self.sectors_per_packet))
        self.sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
        self.debug("Resize receive buffer:", self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 10*MAX_UDP_PACKET_BYTES))
        self.sock.bind((args.host, args.port))
        print("UDP server listening on", args.host, "port", args.port)

        self.stats = dict([(n,0) for n in ['avg_recv_latency', 'avg_decode_latency', 'avg_viz_latency', 'avg_latency', 'avg_buf_recv', 'avg_peek_recv']])
        self.print_pd = print_pd
        self.last_print_stats = time.perf_counter()
        self.num_frames = 0
        self.invalid_packets = 0
        self.debug("Printing stats every", self.print_pd, "seconds")

        self.sinks = None
        self.color_sinks = None
        self.colorizer = rs.colorizer()
        self.colorizer.set_option(rs.option.color_scheme, 0)

    def debug(self, *args):
        if self.debug:
            print(*args)

    def avg_ms(self, f, n_sec):
        self.stats[f] = self.stats[f] * 0.9 + (n_sec * 1000) * 0.1

    def print_stats(self):
        hz = self.num_frames / self.print_pd
        invalid_hz = self.invalid_packets / self.print_pd
        self.debug(("{0:4.1f}hz ({1:4.1f} invalid packets/sec):" +
               "{avg_recv_latency:10.2f}ms recv ({avg_peek_recv:10.2f} peek, {avg_buf_recv:10.2f} transfer ms)" +
               "{avg_decode_latency:10.2f}ms decode" +
               "{avg_viz_latency:10.2f}ms viz" +
               "{avg_latency:10.2f}ms total").format(hz, invalid_hz, **self.stats))
        self.num_frames = 0
        self.invalid_packets = 0

    def append_sink(self, sink):
        if sink.COLORIZED_INPUT:
            if self.color_sinks is None:
                self.color_sinks = []
            self.color_sinks.append(sink.write)
        else:
            if self.sinks is None:
                self.sinks = []
            self.sinks.append(sink.write)

    def run_forever(self):
        encoded = {}
        decoded = {}
        while(True):
            start = time.perf_counter()
            nsec = 0
            bufrcv = 0
            peekrcv = 0
            while nsec < self.NUM_SECTOR:
                recv_num_sector = min(self.sectors_per_packet, self.NUM_SECTOR-nsec)
                packet_len = recv_num_sector * rvl_cuda.SECTOR_LEN * np.dtype(np.uint64).itemsize

                # Find out who's sending us the next packet
                peekstart = time.perf_counter()
                (_, src) = self.sock.recvfrom(1, socket.MSG_PEEK)
                src=src[0]
                if encoded.get(src) is None:
                    self.debug("Allocating mapped arrays for new client", src)
                    encoded[src] = cuda.mapped_array((self.NUM_SECTOR, rvl_cuda.SECTOR_LEN), dtype=np.uint64)
                    decoded[src] = cuda.mapped_array(self.dim, dtype=np.uint16)

                brstart = time.perf_counter()
                (nbytes) = self.sock.recv_into(encoded[src][nsec:nsec+recv_num_sector,:], packet_len)
                brend = time.perf_counter()
                bufrcv += brend - brstart
                peekrcv += brstart - peekstart
                if nbytes != packet_len:
                    self.invalid_packets += 1
                    continue

                nsec += self.sectors_per_packet

            decode_start = time.perf_counter()
            rvl_cuda.decode[self.BLOCKS_PER_GRID, self.THREADS_PER_BLOCK](encoded[src], decoded[src], self.deproject)
            # idxs = []
            for i in range(self.NUM_SECTOR):
                v = int(encoded[src][i,0])
                x = v & 0xffff
                y = (v >> 16) & 0xffff
                if x < decoded[src].shape[0] and y < decoded[src].shape[1]:
                    decoded[src][x,y] = 15208
                # idxs.append((x,y))
            # print(idxs)
            decode_end = time.perf_counter()

            if self.show_output:
                cv2.imshow(src + "dec", decoded[src] / max(1, np.max(decoded[src])))
                # cv2.imshow(src + "enc", encoded[src] / max(1, np.max(encoded[src])))
                # cv2.waitKey(10000)
                cv2.waitKey(1)
            if self.sinks is not None:
                frame_data = np.asanyarray(frame.get_depth_frame().get_data())
                for cb in self.sinks:
                    cb(frame.get_frame_number(), frame_data)
            if self.color_sinks is not None:
                frame_data = np.asanyarray(self.colorizer.colorize(frame.get_depth_frame()).get_data())
                for cb in self.color_sinks:
                    cb(frame.get_frame_number(), frame_data)

            end = time.perf_counter()
            self.avg_ms('avg_recv_latency', decode_start - start)
            self.avg_ms('avg_decode_latency', decode_end - decode_start)
            self.avg_ms('avg_viz_latency', end - decode_end)
            self.avg_ms('avg_latency', end - start)
            self.avg_ms('avg_buf_recv', bufrcv)
            self.avg_ms('avg_peek_recv', peekrcv)
            self.num_frames += 1
            if end - self.last_print_stats > self.print_pd:
                self.print_stats()
                self.last_print_stats = end

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Receive RVL-encoded depth camera frames over UDP.")
    parser.add_argument("--port", metavar='PORT', type=int, help="port number", default=19823)
    parser.add_argument("--host", type=str, help="ip address", default="0.0.0.0")
    parser.add_argument("--dim", nargs=2, metavar=('width', 'height'), help="Dimensions of depth image", default=(848, 480))
    parser.add_argument("--print_pd", type=float, help="Period (in seconds) to print performance stats", default=2.0)
    parser.add_argument("--deproject", type=bool, help="Whether to deproject depth frames into point clouds", default=False)
    parser.add_argument("--show_output", type=bool, help="show output in window", default=False)
    args = parser.parse_args()

    r = UDPSource(args.host, args.port, args.dim, args.deproject, print_pd=args.print_pd, show_output=args.show_output)
    try:
        r.run_forever()
    finally:
        cuda.close()

reason: import numpy,from numba


text:
from sklearn.neighbors import NearestNeighbors
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from scipy.linalg import eigh
from scipy.linalg import sqrtm
from scipy.linalg import inv

class LapMCCA:
  def __init__(self,n_components=2,p_neighbors=3,reg=0.001):
    self.h = n_components
    self.p = p_neighbors
    self.r = reg

  # function to create affinity graph
  def create_affinity_graph(self,X,label,n):
    wm=[[0 for k in range(n)] for k in range(n)]
    Xt=X.T
    nbrs=NearestNeighbors(self.p,algorithm='ball_tree').fit(Xt)
    distances, indices = nbrs.kneighbors(Xt,Xt.shape[0])
    for i in range(len(indices)):
      j=indices[i][0]
      lab=label[j]
      wm[j][j]=1
      count=0
      for k in range(len(indices[i])):
        if(count<Xt.shape[0] or count<self.p):
          if(lab==label[k] and j!=k):
            count+=1
            wm[j][k]=cosine_similarity(np.array([X[:,j].T]),np.array([X[:,k]]))[0][0].round(2)
        else:
          break
    return wm

  # function to create within-view covariance using D,W,L
  def D_within_view(self,W,n):
    D=np.zeros([n,n])
    for j in range(n):
      D[j][j]=np.sum(W[j])
    return D

  def L_within_view(self,D,W,n):
    L=np.subtract(D,W)
    return L

  def S_within_view(self,X,L,Xt,n):
    S=np.dot(np.array(X),np.array(L))
    S=np.dot(S,np.array(Xt))
    return (S/(n**2))

  # function to create between-view covariance using D,W,L
  def W_i_j(self,Wi,Wj,n):
    W=np.multiply(Wi,Wj)
    return W

  def D_i_j(self,W,n):
    D=np.zeros([n,n])
    for j in range(n):
      D[j][j]=np.sum(W[j])
    return D

  def L_i_j(self,D,W,n):
    L=np.subtract(D,W)
    return L

  def S_i_j(self,X,L,Yt,n):
    S=np.dot(X,L)
    S=np.dot(S,Yt)
    return (S/(n**2))

  # method to fit the model
  def fit(self,X,label):
    W=[]
    n=X[0].shape[1]
    m=X.shape[0]

    # creating p nearest neighbor affinity graph
    for i in range(m):
      w=self.create_affinity_graph(X[i],label[i],n)
      W.append(w)

    # calculating within view covariance matrix
    D_w_view=[]
    for i in range(m):
      D_w_view.append(self.D_within_view(W[i],n))

    L_w_view=[]
    for i in range(m):
      L_w_view.append(self.L_within_view(D_w_view[i],W[i],n))

    S_w_view=[]
    for i in range(m):
      S_w_view_i=[]
      for j in range(m):
        if(i==j):
          S_w_view_i.append(self.S_within_view(X[i],L_w_view[i],X[i].T,n))
        else:
          S_w_view_i.append(0)
      S_w_view.append(S_w_view_i)

    # regularizing the within-view covariance matrix
    for i in range(m):
      S_w_view[i][i]=np.add(S_w_view[i][i],np.dot(self.r,np.identity(S_w_view[i][i].shape[0])))

    # calculating between view covariance matrix
    Wij=[]
    for i in range(m):
      Wi=[]
      for j in range(m):
        if(i!=j):
          Wi.append((self.W_i_j(W[i],W[j],n)))
        else:
          Wi.append(0)
      Wij.append(Wi)

    Dij=[]
    for i in range(m):
      Di=[]
      for j in range(m):
        if(i!=j):
          Di.append(self.D_i_j(Wij[i][j],n))
        else:
          Di.append(0)
      Dij.append(Di)

    Lij=[]
    for i in range(m):
      Li=[]
      for j in range(m):
        if(i!=j):
          Li.append(self.L_i_j(Dij[i][j],Wij[i][j],n))
        else:
          Li.append(0)
      Lij.append(Li)

    Sij=[]
    for i in range(m):
      Si=[]
      for j in range(m):
        if(i!=j):
          Si.append(self.S_i_j(X[i],Lij[i][j],X[j].T,n))
        else:
          Si.append(0)
      Sij.append(Si)

    # finding the alpha corresponding to every view

    alpha=[]
    for i in range(m):
      alpha.append([np.random.rand(X[i].shape[0])])

    for i in range(self.h):
      for j in range(m):
        alp=np.dot(S_w_view[j][j],alpha[j][i].T)
        for k in range(m):
          if(j!=k):
            alp=np.add(alp,np.dot(Sij[j][k],alpha[k][i].T))
        deno=(np.dot(alp,alp.T))**(0.5)
        alp=(alp)/deno
        alpha[j]=np.vstack((alpha[j],alp))

    for i in range(m):
      alpha[i]=np.delete(alpha[i],0,0)
      alpha[i]=alpha[i].T
    return alpha

  # method to fit and transform the model
  def fit_transform(self,X,label):
    alpha=self.fit(X,label)
    Xnew=[]
    for i in range(X.shape[0]):
      Xnew.append(np.dot(alpha[i].T,X[i]))
    return Xnew

max_stars_repo_name: aryapushpa/DataScience

max_issues_repo_name: Gulnaz-Tabassum/DataScience

max_forks_repo_name: Gulnaz-Tabassum/DataScience

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-
"""Executable copy of Bus_Number_Detector.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/14_3pF-wLajUfmaBCN4X0IM1Fbs89zCWX

# Real Time Bus Number Detection using Tensorflow and Keras

#Table of contents:

###1.   [Project approach explained](#scrollTo=2yZ6eNWEKSws)
###2.   [Google colab setup (Optional)](#scrollTo=udrfzEjwQL_n&line=3&uniqifier=1)
### 3.   [Imports and path set](#scrollTo=kmI5eIakQ6A_)

###4.   [Tensorflow Object api set](#scrollTo=9QWx13qxQeFP)
###5.   [Loading the Bus Detector model](#scrollTo=fPxhCUuGSbrh)
###6.   [Loading the Digit Detector model](#scrollTo=ZyoEpSrASsRR)
###7.   [Integrating the models](#scrollTo=EMhGoTj1S7jI)
###8.  [Running the detection model](#scrollTo=WGpL03taTKtV)
###9.   [Hardware specs and performance](#scrollTo=RCE9mfKKXGZ6&line=3&uniqifier=1)
###10.   [Future Improvements](#scrollTo=9cblhZ5PcI7V)

# Project approach

##**Goal:** Detect and track the bus numbers in a HD video in real-time.

The detection is done two-fold:
1. First detect the buses in the video.
2. Then detect the numbers on those buses.

Benefits of two fold approach compared to direct digit detection:
* There is no robust dataset that has explicitly has bus numbers. This can be trained using publicly available datasets.
* The detection of buses in the first step, drastically reduces the search space for the digit detection (MSCOCO and SVHN).
* As the model is constrained to find digits within the bus, it won't detect any random digits elsewhere in the video.

#### 1. Detect the buses in the video:
I have used [YOLOv2 tiny](https://pjreddie.com/darknet/yolov2/) model trained on the [MSCOCO datset](http://cocodataset.org/#explore) to detect the buses. The model in the project has been translated from DarkNet to Keras.

#### 2. Detect the digits in the bus:
I have used SSD_MOBILENETv1 architecture and trained it from scratch on the SVHN dataset. I have used the Tensorflow Object Detection API in this part.

#Google collab setup

If running on Google Colab, uncommment below to mount the drive.
This will allow access to folders stored in Google Drive.
"""

# from google.colab import drive
# drive.mount('/content/drive')
# %reload_ext autoreload
# %autoreload 2
# %tensorflow_version 1.x

"""# Imports and path set"""

import os
# print(os.listdir)
dir_path_main = os.path.dirname(os.path.abspath(__file__))+'/'
os.sys.path.append(dir_path_main+'Bus_Detection/')
os.sys.path.append(dir_path_main+'Digit_Detection/')
if os.path.exists(os.sys.argv[1]) == False:
  raise ValueError('File does not exist')
print(os.sys.path)
### If on colab, UNCOMMENT BELOW
### Change the path to drive folder.

# ROOT_DIR='trail_ready/'
# dir_path_main = '/content/drive/My Drive/'+ROOT_DIR
# os.sys.path.append(dir_path_main+'Bus_Detection/')
# os.sys.path.append(dir_path_main+'Digit_Detection/')

import time
import pathlib
# import six.moves.urllib as urllib
# import tarfile
import copy

##Get tensorflow and keras
# %tensorflow_version 1.x
import tensorflow as tf
from keras import backend as K
from keras.models import load_model

##Get matplotlib for plotting
import numpy as np
import matplotlib.pyplot as plt
from PIL import ImageFont, ImageDraw, Image

# %matplotlib inline

#Get helper functions
from utils.yolo_postprocess import yolo_eval , yolo_filter_boxes , yolo_non_max_suppression , load_image_into_numpy_array
from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners
from utils.yolo_utils import *
from post_process import add_bus_numbers

import sys

# sys.argv[1] = dir_path_main+'data/Training_3.MOV'

import os.path
from os import path

## Take in the input file_name
# video_file = dir_path_main+'data/Training_3.MOV'
# print(sys.argv[1])
# if path.exists(sys.argv[1]) == False:
#   raise ValueError('File does not exist')

"""# Install Tensorflow Object Detection api"""

# # Install pycococ tools and clone in tensorflow Object detection api
# # We will need the object detection api
# !pip install pycocotools
# !git clone --depth 1 https://github.com/tensorflow/models

# # After successfully installing the required dependencies for Tensorflow Object Detection api
# # this command should return no error messages
# %%bash
# cd models/research/
# protoc object_detection/protos/*.proto --python_out=.

# # Install Tensorflow Object Detection api
# %%bash
# cd models/research
# pip install .

# from object_detection.utils import ops as utils_ops

## These are the functions from Tensorflow Object api that we will be using
##  utils.my_draw is a custom made bounding box plotting function, based off the Tensorflow Object Detection api
from object_detection.utils import label_map_util
import utils.my_draw as my_draw

"""# Loading Bus Detector Model"""

#Start Tensorflow session and Load the pre-trained YOLOv2-tiny model
#The class names, labels and anchors are the default ones
sess1 = K.get_session()
print(f"loading bus detector model")
yolo_model = load_model(dir_path_main+'Bus_Detection/'+"model_data/yolov2-tiny.h5", compile=False)
print(f"loaded bus detector model")

class_names = read_classes(dir_path_main+'Bus_Detection/'+"model_data/yolo_coco_classes.txt")
anchors = read_anchors(dir_path_main+'Bus_Detection/'+"model_data/yolo_anchors.txt")

# Generate colors for drawing bounding boxes.
colors = generate_colors(class_names)

#This function takes the Output of Yolo model and converts it into Bounding Box parameters
yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))

# Get Image size so that we can initiate the model
# We need this information to upscale the output bounding boxes
vcap = cv2.VideoCapture(sys.argv[1])
frame_width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
image_shape = np.float32([frame_height,frame_width])

#This function takes the Bounding Bounding Box parameters into Bounding Boxes with labels.
# Scores are how confident the model is about the prediction
# Boxes are the coordinates of the boxes that surround detected objects
# Classes are the labels of those detected objects

scores1, boxes1, classes1 = yolo_eval(yolo_outputs, image_shape=image_shape,score_threshold=.2)
print(f"initialised bus detector model")

def bus_detection(sess, image, colors):
  '''
  Input: Tensorflow session, Raw frame from the video , list of colors for the bounding boxes around buses.

  Function:
    1. Find the objects (MSCOCO classes) in the image.
    2. Filter out the non-bus classes/

  Output:
    Return the Bounding boxes and the cropped images of the buses found.

  '''
    # Resize, Change colorspace, Normalize and expand_dimensions of input image
  image, image_data = preprocess_image(image, model_image_size = (416, 416))

  # Detect all the objects in the image
  #This function takes the Bounding Bounding Box parameters into Bounding Boxes with labels.
  # Scores are how confident the model is about the prediction
  # Boxes are the coordinates of the boxes that surround detected objects
  # Classes are the labels of those detected objects

  out_scores, out_boxes, out_classes = sess.run([scores1, boxes1, classes1], feed_dict={yolo_model.input:image_data, K.learning_phase():0})

  if len(out_boxes) >0:
    # Filter for the bounding boxes of Buses
    # Bus class number is 5
    buses_found =  (list (zip(*((out_score, out_box, out_class) for out_score, out_box, out_class in zip(out_scores, out_boxes, out_classes)\
                              if out_class == 5))))

    #If any buses found get the bounding boxes with labels, else return Not Found
    if len(buses_found) > 0:
      out_scores, out_boxes, out_classes = buses_found
    else:
      return ['notfound','notfound','notfound','notfound']

  # Get cropped image of the buses
  cropped_images = get_cropped(image, out_scores, out_boxes, out_classes, class_names, colors)

  return out_scores, out_boxes, out_classes , cropped_images

"""# Loading Digit Detector model"""

digit_detection_graph = tf.Graph()

# Load Digit detection graph from Frozen Inference Graph
# This is the output of the Tensorflow Object Detection API
print(f"loading digit detector model")
with digit_detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(dir_path_main+'Digit_Detection/output_graph2/frozen_inference_graph.pb', 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')
print(f"loaded digit detector model")

Digit_categories = dir_path_main+'Digit_Detection/number.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(Digit_categories, use_display_name=True)

#Start Tensorflow session for the Digit Detection model

sess2 = tf.Session(graph=digit_detection_graph)
image_tensor = digit_detection_graph.get_tensor_by_name('image_tensor:0')

#This function takes the Bounding Bounding Box parameters into Bounding Boxes with labels.
# Scores are how confident the model is about the prediction
# Boxes are the coordinates of the boxes that surround detected objects
# Classes are the labels of those detected objects
# Number of detections is the number of objects found

boxes2 = digit_detection_graph.get_tensor_by_name('detection_boxes:0')
scores2 = digit_detection_graph.get_tensor_by_name('detection_scores:0')
classes2 = digit_detection_graph.get_tensor_by_name('detection_classes:0')
num_detections2 = digit_detection_graph.get_tensor_by_name('num_detections:0')

print(f"initialised digit detector model")

def digit_detection(image, threshold = 0.5):
  '''
  Input: cropped image of ONE BUS.

  Function:
    1. Detects the digits and its bounding boxes
    2. Orders the found digits (Left to Right) to make the bus number.
    3. Draws the bounding box and label around the digits

  Output:
    Image : Image of the bus with bounding boxes around the Bus Numbers
    Display_Label : List of Digits found from left to right
  '''
  image_shape = image.shape
  image_np_expanded = np.expand_dims(image, axis=0)

  (out_boxes2, out_scores2, out_classes2, out_num_detections2) = sess2.run([boxes2, scores2, classes2, num_detections2], feed_dict={image_tensor: image_np_expanded})

  display_label = []
  for (score,class_name,out_box) in zip(out_scores2,out_classes2,out_boxes2):
    xmin_list = out_box[:,1]
    idx   = np.argsort(xmin_list)
    score = np.array(score)[idx]
    class_name = np.array(class_name)[idx]
    for (one_score,one_class) in zip(score,class_name):
      if one_score > threshold:
        display_label.append(int(one_class))

  image = my_draw.visualize_boxes_and_labels_on_image_array(image,np.squeeze(out_boxes2),np.squeeze(out_classes2).astype(np.int32),\
                                                               np.squeeze(out_scores2),category_index,use_normalized_coordinates=True,line_thickness=10)
  return image, display_label

"""#Integrating models"""

def detect_bus_numbers(video_frame):
  '''
  Input:
    Raw Video frame for bus number detection

  Function:
    1. Find the buses in the image.
    2. Split the found buses into smaller patches to reduce search space.
    3. Run Digit Detection on those patches.
    4. Recombine those detected digits into the original image

  Output:
    1. Image with bounding boxes around all the bus numbers
    2. A list of all the bus numbers found.

  '''
  image = copy.copy(video_frame)

  #Detect Buses and their bounding boxes
  # These are already thresholded
  _,bus_boxes , _, bus_cropped = bus_detection(sess1, image, colors)

  #If nothing is found return original image
  if bus_boxes =='notfound':
    return (image,[])

  Bus_Numbers_List = []

  for (one_bus_cropped,box_of_one_bus) in zip(bus_cropped,bus_boxes):

    cropped = copy.copy(one_bus_cropped)

    # Here I am assuming that the bus number is in the top 1/3rd of the bus.
    # I have checked multiple websites for a variety of bus models in Finland and the bus number was always on top
    # This helps speeding up the process significantly
    shrink_height = 3
    top_cropped = cropped[0:int(np.round(cropped.shape[0]/shrink_height)),:,:]

    # Here we are split the cropped image (which is the top of the bus ) into n different parts
    # This also showed increase in detection of digits as the training set for images had very small images.
    split_number_vertically = 4
    x_step = int(np.round(top_cropped.shape[1]/split_number_vertically))
    one_bus_split = []
    for i in range(split_number_vertically):
      one_bus_split.append(top_cropped[:,i*x_step:(i+1)*x_step,:])

    # Now we can run the digit detection on those small patches.
    # We stitch the outputs again into 'one_bus_combined'
    # 'bus_number_label_list' will contain all the seperate digits of the bus
    # This is later combined to give the final bus number

    one_bus_combined =[]
    bus_number_label_list = []
    for one_part in one_bus_split:
      partial_image_output, display_label = digit_detection(one_part)
      bus_number_label_list.append(display_label)
      one_bus_combined.append(partial_image_output)

    # Add this Bus Number to the list of other Bus numbers
    Bus_Numbers_List.append(bus_number_label_list)

    # Stitch back the outputs into one image
    # Replace the parts of the input image with the detected digits outputs.
    for j in range(len(one_bus_combined)):
      top_cropped[:,j*x_step:(j+1)*x_step,:] = one_bus_combined[j]

    cropped[0:int(np.round(cropped.shape[0]/shrink_height)),:,:] = top_cropped
    top, left, bottom, right = box_of_one_bus
    try:
      image[int(np.round(top)):int(np.round(bottom)),int(np.round(left)):int(np.round(right))] = cropped
    except:
      image[int(np.round(top)):int(np.round(bottom)),int(np.round(left)):int(np.round(right))] = cv2.resize(cropped,(int(np.round(right))-int(np.round(left)),int(np.round(bottom))-int(np.round(top))))

  return (image , Bus_Numbers_List)

"""# Running the detection model"""

# filename1 = dir_path_main+"data/frame215.jpg"
# detected_digits ,labels = detect_bus_numbers(cv2.imread(filename1))
# output_image = add_bus_numbers(detected_digits,labels)
# cv2.imwrite('output_image.jpg',output_image)

# from IPython.display import Image
# Image("output_image.jpg", width=700, height=1500)

"""## ***VIDEO WRITING***"""

import cv2
import gc

# cap = cv2.VideoCapture(dir_path_main+'data/Training_3.MOV')
# print ('Reached here')
cap = cv2.VideoCapture(sys.argv[1])

## Declare codec used
### PLEASE CHANGE IF VIDEO IS NOT CODED PROPERLY
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
fh  = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fw  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

output_name = sys.argv[1].split('/')[-1].split(".")[0]
out = cv2.VideoWriter(f'{output_name}_processed.avi',fourcc, 10, (fw,fh))

count = 0

total_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

# While there are more frames
while cap.isOpened():
    ret,frame = cap.read()
    # print(ret)
    print(f'frame {count} is processed out of {total_count}')
    if ret:
      # Run Detection on one frame and write to video
      detected_digits ,labels = detect_bus_numbers(frame)
      output_image = add_bus_numbers(detected_digits,labels)
      out.write(output_image)

      cap.set(1, count)
      #Skipping one frame
      count = count + 2
      #collect garbage to make sure ram is not used up
      if count%50 ==0:
        gc.collect()
    else:
      cap.release()
      out.release()
      cv2.destroyAllWindows()

print ("Video conversion finished.")
print(f'Saved at {output_name}_processed.avi')
"""#Hardware specs and performance

In this project, I have used (2160x3840) pixel videos with an output rate of 8 fps.
I have implemented all the code on Google Colab, due to lack of my own GPU.
Here are the specs:

* GPU: 1xTesla K80 , having 2496 CUDA cores, compute 3.7, 12GB(11.439GB Usable) GDDR5 VRAM.
* CPU: 1xsingle core hyper threaded i.e (1 core, 2 threads) Xeon Processors @2.3Ghz (No Turbo Boost) , 45MB Cache.

# Future Improvements

1. Multi Threading during the video processing.
2. Find a better way to reduce search space in the number_detection, than manual cropping.
"""

# !ls '{dir_path_main}'

# !python3  '{dir_path_main}'executable_copy_of_bus_number_detector.py data/Training3.MOV


text:
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

N = np.array(range(4, 13))
stack=[0.0000736,0.0001495,0.0007673,0.0027767,0.0104837,
       0.048083,0.228105,1.2051,6.8581]
recursion=[0.0000035,0.0000092,0.0000458,0.0001814,0.0006057,
           0.0029883,0.0137117,0.0739021,0.421939]

def func(x, a, b):
    return a*np.exp(b*x)

stack_popt, stack_pcov = curve_fit(func, N, stack)
recursion_popt, recursion_pcov = curve_fit(func, N, recursion)

plt.figure(figsize=(9,9))
plt.subplot(1, 2, 1)
plt.scatter(N, stack, color='red')
x = np.linspace(0, 12, 100)
y = [func(t, *stack_popt) for t in x]
plt.plot(x, y, color='gray')
plt.legend(['{:.2e}*exp({:.2e}x)'.format(*stack_popt), 'stack'])
plt.subplot(1, 2, 2)
plt.scatter(N, recursion, color='blue')
x = np.linspace(0, 12, 100)
y = [func(t, *recursion_popt) for t in x]
plt.plot(x, y, color='gray')
plt.legend(['{:.2e}*exp({:.2e}x)'.format(*recursion_popt), 'recursion'])

max_stars_repo_path: CS/数据结构/第二次实验/N皇后问题/实验报告绘图程序.py

max_issues_repo_path: CS/数据结构/第二次实验/N皇后问题/实验报告绘图程序.py

max_issues_repo_name: Pengxiang-Hua/USTC-CS-Resources

max_forks_repo_path: CS/数据结构/第二次实验/N皇后问题/实验报告绘图程序.py

max_forks_repo_name: Pengxiang-Hua/USTC-CS-Resources


text:
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import six
import abc
import numpy as np

from ..fluid.data_feeder import check_variable_and_dtype
from ..fluid.layer_helper import LayerHelper
from ..fluid.layers.nn import topk
from ..fluid.framework import core, _varbase_creator, in_dygraph_mode
import paddle

__all__ = ['Metric', 'Accuracy', 'Precision', 'Recall', 'Auc', 'accuracy']

def _is_numpy_(var):
    return isinstance(var, (np.ndarray, np.generic))

@six.add_metaclass(abc.ABCMeta)
class Metric(object):
    r"""
    Base class for metric, encapsulates metric logic and APIs
    Usage:

        .. code-block:: text

            m = SomeMetric()
            for prediction, label in ...:
                m.update(prediction, label)
            m.accumulate()

    Advanced usage for :code:`compute`:

    Metric calculation can be accelerated by calculating metric states
    from model outputs and labels by build-in operators not by Python/NumPy
    in :code:`compute`, metric states will be fetched as NumPy array and
    call :code:`update` with states in NumPy format.
    Metric calculated as follows (operations in Model and Metric are
    indicated with curly brackets, while data nodes not):

        .. code-block:: text

                 inputs & labels              || ------------------
                       |                      ||
                    {model}                   ||
                       |                      ||
                outputs & labels              ||
                       |                      ||    tensor data
                {Metric.compute}              ||
                       |                      ||
              metric states(tensor)           ||
                       |                      ||
                {fetch as numpy}              || ------------------
                       |                      ||
              metric states(numpy)            ||    numpy data
                       |                      ||
                {Metric.update}               \/ ------------------

    Examples:

        For :code:`Accuracy` metric, which takes :code:`pred` and :code:`label`
        as inputs, we can calculate the correct prediction matrix between
        :code:`pred` and :code:`label` in :code:`compute`.
        For examples, prediction results contains 10 classes, while :code:`pred`
        shape is [N, 10], :code:`label` shape is [N, 1], N is mini-batch size,
        and we only need to calculate accurary of top-1 and top-5, we could
        calculate the correct prediction matrix of the top-5 scores of the
        prediction of each sample like follows, while the correct prediction
        matrix shape is [N, 5].

          .. code-block:: text

              def compute(pred, label):
                  # sort prediction and slice the top-5 scores
                  pred = paddle.argsort(pred, descending=True)[:, :5]
                  # calculate whether the predictions are correct
                  correct = pred == label
                  return paddle.cast(correct, dtype='float32')

        With the :code:`compute`, we split some calculations to OPs (which
        may run on GPU devices, will be faster), and only fetch 1 tensor with
        shape as [N, 5] instead of 2 tensors with shapes as [N, 10] and [N, 1].
        :code:`update` can be define as follows:

          .. code-block:: text

              def update(self, correct):
                  accs = []
                  for i, k in enumerate(self.topk):
                      num_corrects = correct[:, :k].sum()
                      num_samples = len(correct)
                      accs.append(float(num_corrects) / num_samples)
                      self.total[i] += num_corrects
                      self.count[i] += num_samples
                  return accs
    """

    def __init__(self):
        pass

    @abc.abstractmethod
    def reset(self):
        """
        Reset states and result
        """
        raise NotImplementedError("function 'reset' not implemented in {}.".
                                  format(self.__class__.__name__))

    @abc.abstractmethod
    def update(self, *args):
        """
        Update states for metric

        Inputs of :code:`update` is the outputs of :code:`Metric.compute`,
        if :code:`compute` is not defined, the inputs of :code:`update`
        will be flatten arguments of **output** of mode and **label** from data:
        :code:`update(output1, output2, ..., label1, label2,...)`

        see :code:`Metric.compute`
        """
        raise NotImplementedError("function 'update' not implemented in {}.".
                                  format(self.__class__.__name__))

    @abc.abstractmethod
    def accumulate(self):
        """
        Accumulates statistics, computes and returns the metric value
        """
        raise NotImplementedError(
            "function 'accumulate' not implemented in {}.".format(
                self.__class__.__name__))

    @abc.abstractmethod
    def name(self):
        """
        Returns metric name
        """
        raise NotImplementedError("function 'name' not implemented in {}.".
                                  format(self.__class__.__name__))

    def compute(self, *args):
        """
        This API is advanced usage to accelerate metric calculating, calulations
        from outputs of model to the states which should be updated by Metric can
        be defined here, where Paddle OPs is also supported. Outputs of this API
        will be the inputs of "Metric.update".

        If :code:`compute` is defined, it will be called with **outputs**
        of model and **labels** from data as arguments, all outputs and labels
        will be concatenated and flatten and each filed as a separate argument
        as follows:
        :code:`compute(output1, output2, ..., label1, label2,...)`

        If :code:`compute` is not defined, default behaviour is to pass
        input to output, so output format will be:
        :code:`return output1, output2, ..., label1, label2,...`

        see :code:`Metric.update`
        """
        return args

class Accuracy(Metric):
    """
    Encapsulates accuracy metric logic.

    Args:
        topk (int|tuple(int)): Number of top elements to look at
            for computing accuracy. Default is (1,).
        name (str, optional): String name of the metric instance. Default
            is `acc`.

    Example by standalone:

        .. code-block:: python

          import numpy as np
          import paddle

          x = paddle.to_tensor(np.array([
              [0.1, 0.2, 0.3, 0.4],
              [0.1, 0.4, 0.3, 0.2],
              [0.1, 0.2, 0.4, 0.3],
              [0.1, 0.2, 0.3, 0.4]]))
          y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))

          m = paddle.metric.Accuracy()
          correct = m.compute(x, y)
          m.update(correct)
          res = m.accumulate()
          print(res) # 0.75

    Example with Model API:

        .. code-block:: python

          import paddle
          from paddle.static import InputSpec
          import paddle.vision.transforms as T
          from paddle.vision.datasets import MNIST

          input = InputSpec([None, 1, 28, 28], 'float32', 'image')
          label = InputSpec([None, 1], 'int64', 'label')
          transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
          train_dataset = MNIST(mode='train', transform=transform)

          model = paddle.Model(paddle.vision.LeNet(), input, label)
          optim = paddle.optimizer.Adam(
              learning_rate=0.001, parameters=model.parameters())
          model.prepare(
              optim,
              loss=paddle.nn.CrossEntropyLoss(),
              metrics=paddle.metric.Accuracy())

          model.fit(train_dataset, batch_size=64)

    """

    def __init__(self, topk=(1, ), name=None, *args, **kwargs):
        super(Accuracy, self).__init__(*args, **kwargs)
        self.topk = topk
        self.maxk = max(topk)
        self._init_name(name)
        self.reset()

    def compute(self, pred, label, *args):
        """
        Compute the top-k (maxinum value in `topk`) indices.

        Args:
            pred (Tensor): The predicted value is a Tensor with dtype
                float32 or float64. Shape is [batch_size, d0, ..., dN].
            label (Tensor): The ground truth value is Tensor with dtype
                int64. Shape is [batch_size, d0, ..., 1], or
                [batch_size, d0, ..., num_classes] in one hot representation.

        Return:
            Tensor: Correct mask, a tensor with shape [batch_size, topk].
        """
        pred = paddle.argsort(pred, descending=True)
        pred = paddle.slice(
            pred, axes=[len(pred.shape) - 1], starts=[0], ends=[self.maxk])
        if (len(label.shape) == 1) or \
           (len(label.shape) == 2 and label.shape[-1] == 1):
            # In static mode, the real label data shape may be different
            # from shape defined by paddle.static.InputSpec in model
            # building, reshape to the right shape.
            label = paddle.reshape(label, (-1, 1))
        elif label.shape[-1] != 1:
            # one-hot label
            label = paddle.argmax(label, axis=-1, keepdim=True)
        correct = pred == label
        return paddle.cast(correct, dtype='float32')

    def update(self, correct, *args):
        """
        Update the metrics states (correct count and total count), in order to
        calculate cumulative accuracy of all instances. This function also
        returns the accuracy of current step.

        Args:
            correct: Correct mask, a tensor with shape [batch_size, topk].

        Return:
            Tensor: the accuracy of current step.
        """
        if isinstance(correct, paddle.Tensor):
            correct = correct.numpy()
        num_samples = np.prod(np.array(correct.shape[:-1]))
        accs = []
        for i, k in enumerate(self.topk):
            num_corrects = correct[..., :k].sum()
            accs.append(float(num_corrects) / num_samples)
            self.total[i] += num_corrects
            self.count[i] += num_samples
        accs = accs[0] if len(self.topk) == 1 else accs
        return accs

    def reset(self):
        """
        Resets all of the metric state.
        """
        self.total = [0.] * len(self.topk)
        self.count = [0] * len(self.topk)

    def accumulate(self):
        """
        Computes and returns the accumulated metric.
        """
        res = []
        for t, c in zip(self.total, self.count):
            r = float(t) / c if c > 0 else 0.
            res.append(r)
        res = res[0] if len(self.topk) == 1 else res
        return res

    def _init_name(self, name):
        name = name or 'acc'
        if self.maxk != 1:
            self._name = ['{}_top{}'.format(name, k) for k in self.topk]
        else:
            self._name = [name]

    def name(self):
        """
        Return name of metric instance.
        """
        return self._name

class Precision(Metric):
    """
    Precision (also called positive predictive value) is the fraction of
    relevant instances among the retrieved instances. Refer to
    https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers

    Noted that this class manages the precision score only for binary
    classification task.

    Args:
        name (str, optional): String name of the metric instance.
            Default is `precision`.

    Example by standalone:

        .. code-block:: python

          import numpy as np
          import paddle

          x = np.array([0.1, 0.5, 0.6, 0.7])
          y = np.array([0, 1, 1, 1])

          m = paddle.metric.Precision()
          m.update(x, y)
          res = m.accumulate()
          print(res) # 1.0

    Example with Model API:

        .. code-block:: python

          import numpy as np

          import paddle
          import paddle.nn as nn

          class Data(paddle.io.Dataset):
              def __init__(self):
                  super(Data, self).__init__()
                  self.n = 1024
                  self.x = np.random.randn(self.n, 10).astype('float32')
                  self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')

              def __getitem__(self, idx):
                  return self.x[idx], self.y[idx]

              def __len__(self):
                  return self.n

          model = paddle.Model(nn.Sequential(
              nn.Linear(10, 1),
              nn.Sigmoid()
          ))
          optim = paddle.optimizer.Adam(
              learning_rate=0.001, parameters=model.parameters())
          model.prepare(
              optim,
              loss=nn.BCELoss(),
              metrics=paddle.metric.Precision())

          data = Data()
          model.fit(data, batch_size=16)
    """

    def __init__(self, name='precision', *args, **kwargs):
        super(Precision, self).__init__(*args, **kwargs)
        self.tp = 0  # true positive
        self.fp = 0  # false positive
        self._name = name

    def update(self, preds, labels):
        """
        Update the states based on the current mini-batch prediction results.

        Args:
            preds (numpy.ndarray): The prediction result, usually the output
                of two-class sigmoid function. It should be a vector (column
                vector or row vector) with data type: 'float64' or 'float32'.
            labels (numpy.ndarray): The ground truth (labels),
                the shape should keep the same as preds.
                The data type is 'int32' or 'int64'.
        """
        if isinstance(preds, paddle.Tensor):
            preds = preds.numpy()
        elif not _is_numpy_(preds):
            raise ValueError("The 'preds' must be a numpy ndarray or Tensor.")

        if isinstance(labels, paddle.Tensor):
            labels = labels.numpy()
        elif not _is_numpy_(labels):
            raise ValueError("The 'labels' must be a numpy ndarray or Tensor.")

        sample_num = labels.shape[0]
        preds = np.floor(preds + 0.5).astype("int32")

        for i in range(sample_num):
            pred = preds[i]
            label = labels[i]
            if pred == 1:
                if pred == label:
                    self.tp += 1
                else:
                    self.fp += 1

    def reset(self):
        """
        Resets all of the metric state.
        """
        self.tp = 0
        self.fp = 0

    def accumulate(self):
        """
        Calculate the final precision.

        Returns:
            A scaler float: results of the calculated precision.
        """
        ap = self.tp + self.fp
        return float(self.tp) / ap if ap != 0 else .0

    def name(self):
        """
        Returns metric name
        """
        return self._name

class Recall(Metric):
    """
    Recall (also known as sensitivity) is the fraction of
    relevant instances that have been retrieved over the
    total amount of relevant instances

    Refer to:
    https://en.wikipedia.org/wiki/Precision_and_recall

    Noted that this class manages the recall score only for
    binary classification task.

    Args:
        name (str, optional): String name of the metric instance.
            Default is `recall`.

    Example by standalone:

        .. code-block:: python

          import numpy as np
          import paddle

          x = np.array([0.1, 0.5, 0.6, 0.7])
          y = np.array([1, 0, 1, 1])

          m = paddle.metric.Recall()
          m.update(x, y)
          res = m.accumulate()
          print(res) # 2.0 / 3.0

    Example with Model API:

        .. code-block:: python

          import numpy as np

          import paddle
          import paddle.nn as nn

          class Data(paddle.io.Dataset):
              def __init__(self):
                  super(Data, self).__init__()
                  self.n = 1024
                  self.x = np.random.randn(self.n, 10).astype('float32')
                  self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')

              def __getitem__(self, idx):
                  return self.x[idx], self.y[idx]

              def __len__(self):
                  return self.n

          model = paddle.Model(nn.Sequential(
              nn.Linear(10, 1),
              nn.Sigmoid()
          ))
          optim = paddle.optimizer.Adam(
              learning_rate=0.001, parameters=model.parameters())
          model.prepare(
              optim,
              loss=nn.BCELoss(),
              metrics=[paddle.metric.Precision(), paddle.metric.Recall()])

          data = Data()
          model.fit(data, batch_size=16)
    """

    def __init__(self, name='recall', *args, **kwargs):
        super(Recall, self).__init__(*args, **kwargs)
        self.tp = 0  # true positive
        self.fn = 0  # false negative
        self._name = name

    def update(self, preds, labels):
        """
        Update the states based on the current mini-batch prediction results.

        Args:
            preds(numpy.array): prediction results of current mini-batch,
                the output of two-class sigmoid function.
                Shape: [batch_size, 1]. Dtype: 'float64' or 'float32'.
            labels(numpy.array): ground truth (labels) of current mini-batch,
                the shape should keep the same as preds.
                Shape: [batch_size, 1], Dtype: 'int32' or 'int64'.
        """
        if isinstance(preds, paddle.Tensor):
            preds = preds.numpy()
        elif not _is_numpy_(preds):
            raise ValueError("The 'preds' must be a numpy ndarray or Tensor.")

        if isinstance(labels, paddle.Tensor):
            labels = labels.numpy()
        elif not _is_numpy_(labels):
            raise ValueError("The 'labels' must be a numpy ndarray or Tensor.")

        sample_num = labels.shape[0]
        preds = np.rint(preds).astype("int32")

        for i in range(sample_num):
            pred = preds[i]
            label = labels[i]
            if label == 1:
                if pred == label:
                    self.tp += 1
                else:
                    self.fn += 1

    def accumulate(self):
        """
        Calculate the final recall.

        Returns:
            A scaler float: results of the calculated Recall.
        """
        recall = self.tp + self.fn
        return float(self.tp) / recall if recall != 0 else .0

    def reset(self):
        """
        Resets all of the metric state.
        """
        self.tp = 0
        self.fn = 0

    def name(self):
        """
        Returns metric name
        """
        return self._name

class Auc(Metric):
    """
    The auc metric is for binary classification.
    Refer to https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve.
    Please notice that the auc metric is implemented with python, which may be a little bit slow.

    The `auc` function creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the AUC. To discretize the AUC curve, a linearly spaced set of
    thresholds is used to compute pairs of recall and precision values. The area
    under the ROC-curve is therefore computed using the height of the recall
    values by the false positive rate, while the area under the PR-curve is the
    computed using the height of the precision values by the recall.

    Args:
        curve (str): Specifies the mode of the curve to be computed,
            'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'.
        num_thresholds (int): The number of thresholds to use when
            discretizing the roc curve. Default is 4095.
            'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'.
        name (str, optional): String name of the metric instance. Default
            is `auc`.

    "NOTE: only implement the ROC curve type via Python now."

    Example by standalone:
        .. code-block:: python

          import numpy as np
          import paddle

          m = paddle.metric.Auc()

          n = 8
          class0_preds = np.random.random(size = (n, 1))
          class1_preds = 1 - class0_preds

          preds = np.concatenate((class0_preds, class1_preds), axis=1)
          labels = np.random.randint(2, size = (n, 1))

          m.update(preds=preds, labels=labels)
          res = m.accumulate()

    Example with Model API:

        .. code-block:: python

          import numpy as np
          import paddle
          import paddle.nn as nn

          class Data(paddle.io.Dataset):
              def __init__(self):
                  super(Data, self).__init__()
                  self.n = 1024
                  self.x = np.random.randn(self.n, 10).astype('float32')
                  self.y = np.random.randint(2, size=(self.n, 1)).astype('int64')

              def __getitem__(self, idx):
                  return self.x[idx], self.y[idx]

              def __len__(self):
                  return self.n

          model = paddle.Model(nn.Sequential(
              nn.Linear(10, 2), nn.Softmax())
          )
          optim = paddle.optimizer.Adam(
              learning_rate=0.001, parameters=model.parameters())

          def loss(x, y):
              return nn.functional.nll_loss(paddle.log(x), y)

          model.prepare(
              optim,
              loss=loss,
              metrics=paddle.metric.Auc())
          data = Data()
          model.fit(data, batch_size=16)
    """

    def __init__(self,
                 curve='ROC',
                 num_thresholds=4095,
                 name='auc',
                 *args,
                 **kwargs):
        super(Auc, self).__init__(*args, **kwargs)
        self._curve = curve
        self._num_thresholds = num_thresholds

        _num_pred_buckets = num_thresholds + 1
        self._stat_pos = np.zeros(_num_pred_buckets)
        self._stat_neg = np.zeros(_num_pred_buckets)
        self._name = name

    def update(self, preds, labels):
        """
        Update the auc curve with the given predictions and labels.

        Args:
            preds (numpy.array): An numpy array in the shape of
                (batch_size, 2), preds[i][j] denotes the probability of
                classifying the instance i into the class j.
            labels (numpy.array): an numpy array in the shape of
                (batch_size, 1), labels[i] is either o or 1,
                representing the label of the instance i.
        """
        if isinstance(labels, paddle.Tensor):
            labels = labels.numpy()
        elif not _is_numpy_(labels):
            raise ValueError("The 'labels' must be a numpy ndarray or Tensor.")

        if isinstance(preds, paddle.Tensor):
            preds = preds.numpy()
        elif not _is_numpy_(preds):
            raise ValueError("The 'preds' must be a numpy ndarray or Tensor.")

        for i, lbl in enumerate(labels):
            value = preds[i, 1]
            bin_idx = int(value * self._num_thresholds)
            assert bin_idx <= self._num_thresholds
            if lbl:
                self._stat_pos[bin_idx] += 1.0
            else:
                self._stat_neg[bin_idx] += 1.0

    @staticmethod
    def trapezoid_area(x1, x2, y1, y2):
        return abs(x1 - x2) * (y1 + y2) / 2.0

    def accumulate(self):
        """
        Return the area (a float score) under auc curve

        Return:
            float: the area under auc curve
        """
        tot_pos = 0.0
        tot_neg = 0.0
        auc = 0.0

        idx = self._num_thresholds
        while idx >= 0:
            tot_pos_prev = tot_pos
            tot_neg_prev = tot_neg
            tot_pos += self._stat_pos[idx]
            tot_neg += self._stat_neg[idx]
            auc += self.trapezoid_area(tot_neg, tot_neg_prev, tot_pos,
                                       tot_pos_prev)
            idx -= 1

        return auc / tot_pos / tot_neg if tot_pos > 0.0 and tot_neg > 0.0 else 0.0

    def reset(self):
        """
        Reset states and result
        """
        _num_pred_buckets = self._num_thresholds + 1
        self._stat_pos = np.zeros(_num_pred_buckets)
        self._stat_neg = np.zeros(_num_pred_buckets)

    def name(self):
        """
        Returns metric name
        """
        return self._name

def accuracy(input, label, k=1, correct=None, total=None, name=None):
    """
    accuracy layer.
    Refer to the https://en.wikipedia.org/wiki/Precision_and_recall

    This function computes the accuracy using the input and label.
    If the correct label occurs in top k predictions, then correct will increment by one.
    Note: the dtype of accuracy is determined by input. the input and label dtype can be different.

    Args:
        input(Tensor): The input of accuracy layer, which is the predictions of network. A Tensor with type float32,float64.
            The shape is ``[sample_number, class_dim]`` .
        label(Tensor): The label of dataset. Tensor with type int32,int64. The shape is ``[sample_number, 1]`` .
        k(int, optional): The top k predictions for each class will be checked. Data type is int64 or int32.
        correct(Tensor, optional): The correct predictions count. A Tensor with type int64 or int32.
        total(Tensor, optional): The total entries count. A tensor with type int64 or int32.
        name(str, optional): The default value is None. Normally there is no need for
            user to set this property. For more information, please refer to :ref:`api_guide_Name`

    Returns:
        Tensor, the correct rate. A Tensor with type float32.

    Examples:
        .. code-block:: python

            import paddle

            predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32')
            label = paddle.to_tensor([[2], [0]], dtype="int64")
            result = paddle.metric.accuracy(input=predictions, label=label, k=1)
            # [0.5]
    """
    if in_dygraph_mode():
        if correct is None:
            correct = _varbase_creator(dtype="int32")
        if total is None:
            total = _varbase_creator(dtype="int32")

        topk_out, topk_indices = topk(input, k=k)
        _acc, _, _ = core.ops.accuracy(topk_out, topk_indices, label, correct,
                                       total)
        return _acc

    helper = LayerHelper("accuracy", **locals())
    check_variable_and_dtype(input, 'input', ['float16', 'float32', 'float64'],
                             'accuracy')
    topk_out, topk_indices = topk(input, k=k)
    acc_out = helper.create_variable_for_type_inference(dtype="float32")
    if correct is None:
        correct = helper.create_variable_for_type_inference(dtype="int32")
    if total is None:
        total = helper.create_variable_for_type_inference(dtype="int32")
    helper.append_op(
        type="accuracy",
        inputs={
            "Out": [topk_out],
            "Indices": [topk_indices],
            "Label": [label]
        },
        outputs={
            "Accuracy": [acc_out],
            "Correct": [correct],
            "Total": [total],
        })
    return acc_out

max_stars_repo_name: OuyangChao/Paddle

max_issues_repo_name: AFLee/Paddle

max_forks_repo_name: AFLee/Paddle