randint() , numpy.sum() , Pandas DataFrame.loc[], numpy.argpartition()

时间:2021-05-25 09:11:39   收藏:0   阅读:33

some notes from coding practice, source from:  GeekforGeek


 

 

Syntax :

randint(start, end)

Parameters :

(start, end) : Both of them must be integer type values.

Returns :

A random integer in range [start, end] including the end points.

Errors and Exceptions :

ValueError : Returns a ValueError when floating
             point values are passed as parameters.

TypeError : Returns a TypeError when anything other than 
            numeric values are passed as parameters.
# importing randint function
# from random module
from random import randint

# Function which generates a new
# random number everytime it executes
def generator():
    return randint(1, 10)
    
# Function takes user input and returns
# true or false depending whether the
# user wins the lucky draw!
def rand_guess():
    random_number = generator()
    
    # defining the number of
    # guesses the user gets
    guess_left = 3

    flag = 0

    # looping the number of times
    # the user gets chances
    while guess_left > 0:

        # Taking a input from the user
        guess = int(input("Pick your number to "
                    "enter the lucky draw\n"))

        # checking whether user‘s guess
        # matches the generated win-condition
        if (guess == random_number):
            flag = 1
            break
        elif (guess < random_number):
            print("opps, try larger number")
        
        else:
            print("opps, try smaller number")

        # Decrementing number of
        # guesses left by 1
        guess_left -= 1

    # If win-condition is satisfied then,
    # the function rand_guess returns True
    if flag is 1:
        return True

    # Else the function returns False
    else:
        return False

# Driver code
if __name__ == __main__:
    if rand_guess() is True:
        print("Congrats!! You Win.")
    else :
        print("Sorry, You Lost!")

Syntax: DataFrame.loc

Parameter : None

Returns : Scalar, Series, DataFrame

技术分享图片

# return the value
result = df.loc[Row_2, Name]

# Print the result
print(result) #Andrea

 

 技术分享图片

# return the values.
result = df.loc[:, [A, D]]

# Print the result
print(result)

技术分享图片

Parameters :
arr : input array.
axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axis). axis = 0 means along the column and axis = 1 means working along the row.
out : Different array in which we want to place the result. The array must have same dimensions as expected output. Default is None.
initial : [scalar, optional] Starting value of the sum.

Return : Sum of the array elements (a scalar value if axis is none) or array with sum values along the specified axis.

import numpy as np
a = [0,1,2,3,4,5]
b = [1,2,3,4,5,6]
a = np.array(a)
b = np.array(b)
c = np.sum(np.square(a-b))

#output: c = 6
# Python Program illustrating
# numpy.sum() method
import numpy as np
    
# 1D array
arr = [20, 2, .2, 10, 4]

print("\nSum of arr : ", np.sum(arr))

print("Sum of arr(uint8) : ", np.sum(arr, dtype = np.uint8))
print("Sum of arr(float32) : ", np.sum(arr, dtype = np.float32))

print ("\nIs np.sum(arr).dtype == np.uint : ",
    np.sum(arr).dtype == np.uint)

print ("Is np.sum(arr).dtype == np.float : ",
    np.sum(arr).dtype == np.float)

output:
Sum of arr :  36.2
Sum of arr(uint8) :  36
Sum of arr(float32) :  36.2

Is np.sum(arr).dtype == np.uint :  False
Is np.sum(arr).dtype == np.uint :  True

Syntax : numpy.argpartition(arr, kth, axis=-1, kind=’introselect’, order=None)

Parameters :
arr : [array_like] Input array.
kth : [int or sequence of ints ] Element index to partition by.
axis : [int or None] Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.
kind : Selection algorithm. Default is ‘introselect’.
order : [str or list of str] When arr is an array with fields defined, this argument specifies which fields to compare first, second, etc.

Return : [index_array, ndarray] Array of indices that partition arr along the specified axis.

use argpartition to find top k maximum values or top k minimum values

x = np.array([3, 4, 2, 1, 0, 5])
print(x[np.argpartition(x, -5)[-5:]]) # find the top 5 maximum values
print(x[np.argpartition(x, 5)[:5]]) #find the top 5 minimum values

#output: 
[1 2 4 3 5]
[1 2 0 3 4]

原文:https://www.cnblogs.com/LilyLiya/p/14806977.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!