ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-25 02:05:06  阅读:181  来源: 互联网

标签:loc arr randint sum random number np array numpy


some notes from coding practice, source from:  GeekforGeek


 

 

  • randint(): return a random integer by given the specific range [start, end],  edge included 
  •  1 # Python3 program explaining work
     2 # of randint() function
     3 
     4 # imports random module
     5 import random
     6 
     7 # Generates a random number between
     8 # a given positive range
     9 r1 = random.randint(0, 10)
    10 print("Random number between 0 and 10 is % s" % (r1))
    11 
    12 # Generates a random number between
    13 # two given negative range
    14 r2 = random.randint(-10, -1)
    15 print("Random number between -10 and -1 is % d" % (r2))
    16 
    17 # Generates a random number between
    18 # a positive and a negative range
    19 r3 = random.randint(-5, 5)
    20 print("Random number between -5 and 5 is % d" % (r3))

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.
  • guess number game
# 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!")
  • Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas.

    Pandas DataFrame.loc attribute access a group of rows and columns by label(s) or a boolean array in the given DataFrame.

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)

  • numpy.sum(): numpy.sum(arr, axis, dtype, out) : This function returns the sum of array elements over the specified axis.

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
  • numpy.argpartition() function is used to create a indirect partitioned copy of input array with its elements rearranged in such a way that the value of the element in k-th position is in the position it would be in a sorted array. All elements smaller than the k-th element are moved before this element and all equal or greater are moved behind it. The ordering of the elements in the two partitions is undefined.It returns an array of indices of the same shape as arr, i.e arr[index_array] yields a partition of arr.

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]

标签:loc,arr,randint,sum,random,number,np,array,numpy
来源: https://www.cnblogs.com/LilyLiya/p/14806977.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有