Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Test2/P01_Introduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Author: OMKAR PATHAK

# NumPy (Numeric Python) is a Python package used for building multi dimensional arrays and performing
# various operations

# In this program we will walk through various concepts and see available functions in the NumPy package.

# For installing: pip3 install numpy

import numpy as np

# we have a function arange() which makes an array of the specified dimension. Example:
myArray = np.arange(20)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 82%

JAS - Just a suggestion

Non-Standard Variable Abbreviation

The variable name 'myArray' uses a generic term 'Array'. While functional, it is better to use a more descriptive name like 'numbers_array' or 'sequence_array' to reflect the data it holds.

Suggested change
myArray = np.arange(20)
numbers_array = np.arange(20)

print(myArray) # [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

# an array from 10 to 20
myArray = np.arange(10, 20) # [10 11 12 13 14 15 16 17 18 19]
print(myArray)

# an array from 10 to 20 with 2 steps
myArray = np.arange(10, 20, 2)
print(myArray) # [10 12 14 16 18]

# reshape() helps to reshape our NumPy array
myArray = np.arange(20)
# syntax: reshape(number_of_rows, number_of_columns)
myArray = myArray.reshape(4, 5)
print(myArray)

# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]
#  [15 16 17 18 19]]

myArray = myArray.reshape(10, 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning Confidence Score: 85%

Potential Incompatible Reshape Operation

I noticed that we are reshaping myArray to (10, 2) on line 35. While this works for the current array size of 20, if the preceding arange or reshape logic changes, this hardcoded value will cause a ValueError. It's safer to use -1 for one of the dimensions to let NumPy calculate it automatically based on the total number of elements.

Suggested change
myArray = myArray.reshape(10, 2)
myArray = myArray.reshape(-1, 2)
Reasons & Gaps

Reasons

  1. Hardcoded dimensions in reshape() can cause runtime failures
  2. Incompatible shapes lead to ValueError exceptions
  3. Using automatic dimension inference improves code robustness

Gaps

  1. The script is currently static, so the error only occurs if the array size is modified in earlier lines.
  2. The developer's intent is educational, where hardcoded values are common for clarity.

print(myArray)

# [[ 0  1]
#  [ 2  3]
#  [ 4  5]
#  [ 6  7]
#  [ 8  9]
#  [10 11]
#  [12 13]
#  [14 15]
#  [16 17]
#  [18 19]]

# shape returns the shape of the array. The length of shape tuple is called as rank (or dimension)
print(myArray.shape) # (10, 2)

# ndim returns the dimension (rank) of the array
print(myArray.ndim) # 2

# size returns the total number of elements in the array
print(myArray.size) # 20

# to check the data we have dtype.
print(myArray.dtype) # int64

# zeros creates an array will all zeros
myArray = np.zeros((3, 4))
print(myArray)

# [[ 0.  0.  0.  0.]
#  [ 0.  0.  0.  0.]
#  [ 0.  0.  0.  0.]]

# ones creates an array with all ones
myArray = np.ones((3, 4))
print(myArray)

# [[ 1.  1.  1.  1.]
#  [ 1.  1.  1.  1.]
#  [ 1.  1.  1.  1.]]

# numpy random module helps to initialize array with random values
myArray = np.random.rand(3, 4)
print(myArray)

# [[ 0.54808903  0.08750717  0.23886267  0.93589283]
#  [ 0.90750146  0.31197039  0.54013725  0.91092763]
#  [ 0.38827674  0.04647878  0.15997665  0.94909537]]
34 changes: 34 additions & 0 deletions Test2/P02_NumpyDataTypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Author: OMKAR PATHAK

# Data type Description
# bool_ Boolean (True or False) stored as a byte
# int_ Default integer type (same as C long; normally either int64 or int32)
# intc Identical to C int (normally int32 or int64)
# intp Integer used for indexing (same as C ssize_t; normally either int32 or int64)
# int8 Byte (-128 to 127)
# int16 Integer (-32768 to 32767)
# int32 Integer (-2147483648 to 2147483647)
# int64 Integer (-9223372036854775808 to 9223372036854775807)
# uint8 Unsigned integer (0 to 255)
# uint16 Unsigned integer (0 to 65535)
# uint32 Unsigned integer (0 to 4294967295)
# uint64 Unsigned integer (0 to 18446744073709551615)
# float_ Shorthand for float64.
# float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
# float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
# float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
# complex_ Shorthand for complex128.
# complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
# complex128 Complex number, represented by two 64-bit floats (real and imaginary components)

import numpy as np

# while creating a numpy array, any data type from above can be explicitly specified.
myArray = np.arange(10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 85%

JAS - Just a suggestion

Non-Descriptive Variable Name

The variable name 'myArray' uses a generic term and camelCase, which is non-standard for Python. A more descriptive snake_case name like 'numbers_array' or 'sequence_array' would improve clarity.

Suggested change
myArray = np.arange(10)
numbers_array = np.arange(10)

print(myArray) # [0 1 2 3 4 5 6 7 8 9]

myArray = np.array(myArray, dtype = np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 100%

Inefficient Array Re-creation for Type Casting

JAS - Just a suggestion
I notice we're using np.array(myArray, dtype=...) to change the data type of an existing array. While this works, it actually creates a full copy of the array in memory. A more efficient and idiomatic way to do this in NumPy is using the .astype() method, which is specifically designed for casting and is generally faster.

Suggested change
myArray = np.array(myArray, dtype = np.float32)
myArray = myArray.astype(np.float32)

print(myArray) # [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

myArray = np.array(myArray, dtype = np.complex64)
print(myArray) # [ 0.+0.j  1.+0.j  2.+0.j  3.+0.j  4.+0.j  5.+0.j  6.+0.j  7.+0.j  8.+0.j 9.+0.j]
24 changes: 24 additions & 0 deletions Test2/P03_NumpyAttributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Author: OMKAR PATHAK

# These are the various attributes provided by NumPy.

import numpy as np

myArray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 85%

JAS - Just a suggestion

Variable Naming Improvement

The variable name 'myArray' uses a generic 'my' prefix and camelCase, which is non-standard for Python. Renaming it to 'matrix' or 'data_array' in snake_case would improve clarity and consistency.

Suggested change
myArray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix = np.array([,,])

print(myArray)

# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

# ndarray.size returns the number of items in the array
print(myArray.size) # 9

# ndarray.shape returns a tuple consisting of array dimensions
print(myArray.shape) # (3, 3)

# ndarray.ndim returns the number of array dimensions
print(myArray.ndim) # 2

# ndarray.itemsize returns the memory size of each element in the array
print(myArray.itemsize) # 8
43 changes: 43 additions & 0 deletions Test2/P04_ArrayFromNumericalRanges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Author: OMKAR PATHAK

# This program illustrates how to create an adarray from numerical ranges

import numpy as np

# ndarray.arange(start, stop, step, dtype)
# Creates a numpy array from 1 to 20
myArray = np.arange(1, 21)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 85%

JAS - Just a suggestion

Variable Naming Improvement

The variable name 'myArray' uses a generic prefix and camelCase, which is non-standard for Python. Renaming it to something more descriptive like 'numerical_range_array' improves clarity.

Suggested change
myArray = np.arange(1, 21)
numerical_range_array = np.arange(1, 21)

print(myArray) # [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

# Specifying data type of each element
myArray = np.arange(10, dtype = 'float')
print(myArray) # [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

# Specifying steps to jump in between two elements
myArray = np.arange(1, 21, 2)
print(myArray) # [ 1  3  5  7  9 11 13 15 17 19]

# ndarray.linspace(start, stop, num, endpoint, retstep, dtype)
# Shows 5 equal intervals between 10 to 20
myArray = np.linspace(10, 20, 5)
print(myArray) # [ 10.   12.5  15.   17.5  20. ]

# if endpoint is set to false the last number inn STOP parameter is not executed
myArray = np.linspace(10, 20, 5, endpoint = False)
print(myArray) # [ 10.  12.  14.  16.  18.]

# ndarray.lopspace returns an ndarray object that contains the numbers that are evenly spaced
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 100%

Typo in Function Name Comment

JAS - Just a suggestion
I noticed a small typo in the comment here. It says ndarray.lopspace, but the actual NumPy function is np.logspace. While it's just a comment, fixing it will help avoid confusion for anyone learning from this script.

Suggested change
# ndarray.lopspace returns an ndarray object that contains the numbers that are evenly spaced
# ndarray.logspace returns an ndarray object that contains the numbers that are evenly spaced

# on a log scale.
# ndarray.logscale(start, stop, num, endpoint, base, dtype)
# default base is 10
myArray = np.logspace(1.0, 3.0, num = 10)
print(myArray)

# [   10.            16.68100537    27.82559402    46.41588834    77.42636827
#    129.1549665    215.443469     359.38136638   599.48425032  1000.        ]

myArray = np.logspace(1.0, 3.0, num = 10, base = 2)
print(myArray)

# [ 2.          2.33305808  2.72158     3.1748021   3.70349885  4.32023896
#   5.0396842   5.87893797  6.85795186  8.        ]
83 changes: 83 additions & 0 deletions Test2/P05_NumpyArrayManipulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Author: OMKAR PATHAK

# This example shows various array manipulation operations
import numpy as np

# numpy.reshape(array_to_reshape, tuple_of_new_shape) gives new shape (dimension) to our array
myArray = np.arange(0, 30, 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 85%

JAS - Just a suggestion

Variable Naming Improvement

The variable name 'myArray' uses a generic 'my' prefix which adds no semantic value. Renaming it to 'even_numbers_sequence' or 'sequence_array' would better describe the data it contains.

Suggested change
myArray = np.arange(0, 30, 2)
even_numbers_sequence = np.arange(0, 30, 2)

print(myArray) # [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28]

myArrayReshaped = myArray.reshape(5, 3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 82%

JAS - Just a suggestion

Variable Naming Improvement

The name 'myArrayReshaped' contains the redundant prefix 'my' and describes the operation rather than the data. A name like 'reshaped_matrix' or 'grid_data' would be more descriptive.

Suggested change
myArrayReshaped = myArray.reshape(5, 3)
reshaped_matrix = even_numbers_sequence.reshape(5, 3)

print(myArrayReshaped)

# [[ 0  2  4]
#  [ 6  8 10]
#  [12 14 16]
#  [18 20 22]
#  [24 26 28]]

# numpy.ndarray.flat() returns an 1-D iterator
print(myArray.flat[5]) # 10

# numpy.ndarray.flatten() restores the reshaped array into a 1-D array
print(myArrayReshaped.flatten())

# numpy.tranpose() this helps to find the tranpose of the given array
print(myArrayReshaped.transpose())

# [[ 0  6 12 18 24]
#  [ 2  8 14 20 26]
#  [ 4 10 16 22 28]]

# numpy.swapaxes(array, axis1, axis2) interchanges the two axes of an array
originalArray = np.arange(8).reshape(2,2,2)
print(originalArray)

# [[[0 1]
#   [2 3]]
#  
#  [[4 5]
#   [6 7]]]

print(np.swapaxes(originalArray, 2, 0))

# [[[0 4]
#   [2 6]]
#  
#  [[1 5]
#   [3 7]]]

# numpy.rollaxis(arr, axis, start) rolls the specified axis backwards, until it lies in a specified position
print(np.rollaxis(originalArray, 2))

# [[[0 2]
#   [4 6]]
#  
#  [[1 3]
#   [5 7]]]

# numpy.resize(arr, shape) returns a new array with the specified size. If the new size is greater than
# the original, the repeated copies of entries in the original are contained

myArray = np.array([[1,2,3],[4,5,6]])
print(myArray)

# [[1 2 3]
#  [4 5 6]]

print(np.resize(myArray, (3, 2)))

# [[1 2]
#  [3 4]
#  [5 6]]

# numpy.append(array, values, axis)
myArray = np.array([[1,2,3],[4,5,6]])
print(myArray)

# [[1 2 3]
#  [4 5 6]]

print(np.append(myArray, [7, 8, 9]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 100%

Inconsistent Axis Handling in np.append

JAS - Just a suggestion
I notice we're using np.append on a 2D array without specifying an axis. By default, this flattens the array, which might not be what we expect when working with matrices. If we want to maintain the 2D structure, we should specify axis=0 for rows or axis=1 for columns.

Suggested change
print(np.append(myArray, [7, 8, 9]))
print(np.append(myArray, [], axis=0))


# [1 2 3 4 5 6 7 8 9]
43 changes: 43 additions & 0 deletions Test2/P06_NumpyStringFunctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Author: OMKAR PATHAK

import numpy as np

abc = ['abc']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 82%

JAS - Just a suggestion

Single-Character Variable Name

The variable name 'abc' is non-descriptive. While it contains multiple letters, it acts as a placeholder. Using a name like 'string_array_one' or 'first_word_list' would better communicate the variable's purpose.

Suggested change
abc = ['abc']
first_word_list = ['abc']

xyz = ['xyz']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 82%

JAS - Just a suggestion

Non-Descriptive Variable Name

The variable name 'xyz' is a generic placeholder. Replacing it with a more descriptive name like 'second_word_list' or 'suffix_array' would improve the readability of the string operations.

Suggested change
xyz = ['xyz']
second_word_list = ['xyz']


# string concatenation
print(np.char.add(abc, xyz)) # ['abcxyz']

print(np.char.add(abc, 'pqr')) # ['abcpqr']

# string multiplication
print(np.char.multiply(abc, 3)) # ['abcabcabc']

# numpy.char.center: This function returns an array of the required width so that the input string is
# centered and padded on the left and right with fillchar.

print(np.char.center(abc, 20, fillchar = '*')) # ['********abc*********']

# numpy.char.capitalize(): This function returns the copy of the string with the first letter capitalized.
print(np.char.capitalize('hello world')) # Hello world

# numpy.char.title(): This function returns a title cased version of the input string with the first letter
# of each word capitalized.
print(np.char.title('hello how are you?')) # Hello How Are You?

# numpy.char.lower(): This function returns an array with elements converted to lowercase. It calls
# str.lower for each element.
print(np.char.lower(['HELLO','WORLD'])) # ['hello' 'world']

# numpy.char.upper(): This function calls str.upper function on each element in an array to return
# the uppercase array elements.
print(np.char.upper('hello')) # HELLO

# numpy.char.split(): This function returns a list of words in the input string. By default, a whitespace
# is used as a separator
print(np.char.split('Omkar Pathak')) # ['Omkar', 'Pathak']
print(np.char.split('2017-02-11', sep='-')) # ['2017', '02', '11']

# numpy.char.join(): This method returns a string in which the individual characters are joined by
# separator character specified.
print(np.char.join(':','dmy')) # d:m:y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 100%

Incorrect Argument Order in np.char.join

JAS - Just a suggestion
I noticed a small mix-up in the np.char.join call. In NumPy, the separator comes first and the target string/array comes second, but the current code passes them in an order that might be confusing or lead to unexpected results if used with arrays. While it works for a single string, following the standard sep, seq pattern is safer.

Suggested change
print(np.char.join(':','dmy')) # d:m:y
print(np.char.join(':', 'dmy')) # d:m:y

30 changes: 30 additions & 0 deletions Test2/P07_NumpyMathematicalFunctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Author: OMKAR PATHAK

import numpy as np

angles = np.array([0, 30, 45, 60, 90, 180, 360])

# Convert to radians by multiplying with pi/180
# for getting sine of angles
print(np.sin(angles * np.pi/180))

# for getting cosine of angles
print(np.cos(angles * np.pi/180))

# for getting tangent of angles
print(np.tan(angles * np.pi/180))

# for computing inverse of trigonometric functions
sine = np.sin(angles * np.pi/180)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 72%

JAS - Just a suggestion

Single-Character Variable Name

The variable name 'sine' is partially descriptive but 'sine_values' would better represent the array of results. However, 'sine' itself is a common mathematical term and is acceptable in this context.

Suggested change
sine = np.sin(angles * np.pi/180)
sine_values = np.sin(angles * np.pi/180)

sineinv = np.arcsin(sine)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning Confidence Score: 88%

Inaccurate Inverse Trigonometry Logic

I notice we're calculating the inverse sine of a range that includes 360 degrees. Since sin(360°) is 0, arcsin(0) returns 0°, not the original 360°. This might be confusing for someone learning how these functions map back to angles. We should probably limit the input range to [-90°, 90°] if the goal is to demonstrate a perfect round-trip conversion.

Reasons & Gaps

Reasons

  1. The np.arcsin() function returns values only within the principal range of [-π/2, π/2]
  2. Trigonometric functions are periodic, leading to information loss during inverse operations
  3. Floating point precision errors near boundaries can cause unexpected results in inverse functions

Gaps

  1. The specific educational goal of the script is not explicitly stated in the PR description
  2. User might intentionally be showing the periodic nature of trigonometric functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 85%

JAS - Just a suggestion

Cryptic Abbreviation in Variable Name

The variable name 'sineinv' uses a cryptic abbreviation 'inv'. Expanding this to 'inverse_sine' or 'sine_inverse' would significantly improve readability and follow standard naming conventions.

Suggested change
sineinv = np.arcsin(sine)
inverse_sine = np.arcsin(sine)

# computing angle from inverse
print(np.degrees(sineinv))

# for rounding the values
print(np.around(sine, 4)) # [ 0.      0.5     0.7071  0.866   1.      0.     -0.    ]

# for rounding to previous integer
print(np.floor(sine)) # [ 0.  0.  0.  0.  1.  0. -1.]

# for rounding to next integer
print(np.ceil(sine)) # [ 0.  1.  1.  1.  1.  1. -0.]
25 changes: 25 additions & 0 deletions Test2/P08_NumpyArithmeticOperations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Author: OMKAR PATHAK

import numpy as np

firstArray = np.arange(12).reshape(3, 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 90%

JAS - Just a suggestion

Variable Naming Improvement

The variable name 'firstArray' is functional but could be more expressive. In Python, 'snake_case' is the standard convention for variable names.

Suggested change
firstArray = np.arange(12).reshape(3, 4)
first_array = np.arange(12).reshape(3, 4)

print(firstArray)

secondArray = np.arange(4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 90%

JAS - Just a suggestion

Variable Naming Improvement

The variable name 'secondArray' uses camelCase. Following Python's PEP 8 style guide, 'snake_case' is preferred for variable names.

Suggested change
secondArray = np.arange(4)
second_array = np.arange(4)

print(secondArray)

# adding above two arrays (NOTE: array shapes should be same)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 100%

Incompatible Array Shapes for Arithmetic

JAS - Just a suggestion
I see you're trying to perform arithmetic between a (3, 4) array and a (4,) array. While NumPy broadcasting usually handles this, your comment on line 11 incorrectly states that shapes should be the same. More importantly, if the intended operation was element-wise addition of two identical shapes, secondArray needs to be reshaped or defined differently to avoid confusion or potential broadcasting errors in more complex logic.

Suggested change
# adding above two arrays (NOTE: array shapes should be same)
# adding above two arrays using NumPy broadcasting

print(np.add(firstArray, secondArray))

# subtracting above two arrays
print(np.subtract(firstArray, secondArray))

# multiplying above two arrays
print(np.multiply(firstArray, secondArray))

# dividing the above two arrays
print(np.divide(firstArray, secondArray))

# numpy.power(): returns array element raised to the specified value result
array = np.array([1, 2, 3])
print(np.power(array, 2)) # [1 4 9]
Loading
Loading