From 86e86e76ad5316aa2beac2bec72f9abe805603cd Mon Sep 17 00:00:00 2001 From: black shadow Date: Sun, 20 Oct 2024 00:34:44 +0530 Subject: [PATCH 1/3] Added the morse code --- morse.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 morse.py diff --git a/morse.py b/morse.py new file mode 100644 index 0000000..666ec9d --- /dev/null +++ b/morse.py @@ -0,0 +1,74 @@ +# Morse Code Dictionary +MORSE_CODE_DICT = { + 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', + 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', + 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', + 'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', + '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----', ',': '--..--', '.': '.-.-.-', + '?': '..--..', '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-', ' ': '/' +} + +# Function to encode a message to Morse code +def encode_to_morse(message): + try: + morse_code = '' + for char in message.upper(): + if char not in MORSE_CODE_DICT: + raise ValueError(f"Character '{char}' cannot be encoded into Morse code.") + morse_code += MORSE_CODE_DICT[char] + ' ' + return morse_code.strip() + except ValueError as ve: + print(ve) + return None + except Exception as e: + print(f"An unexpected error occurred: {e}") + return None + +# Function to decode Morse code to a message +def decode_from_morse(morse_message): + try: + morse_words = morse_message.split(' / ') # Words are separated by '/' + decoded_message = '' + for word in morse_words: + for morse_char in word.split(): + if morse_char not in MORSE_CODE_DICT.values(): + raise ValueError(f"Morse code '{morse_char}' is invalid.") + decoded_message += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT.values()).index(morse_char)] + decoded_message += ' ' + return decoded_message.strip() + except ValueError as ve: + print(ve) + return None + except Exception as e: + print(f"An unexpected error occurred: {e}") + return None + +# Main program loop +if __name__ == "__main__": + while True: + print("\nMorse Code Encoder/Decoder") + print("1. Encode a message to Morse code") + print("2. Decode Morse code to a message") + print("3. Exit") + + try: + choice = int(input("Select an option (1/2/3): ")) + if choice == 1: + message = input("Enter a message to encode: ") + result = encode_to_morse(message) + if result: + print(f"Encoded Morse Code: {result}") + elif choice == 2: + morse_message = input("Enter Morse code to decode (use '/' for space between words): ") + result = decode_from_morse(morse_message) + if result: + print(f"Decoded Message: {result}") + elif choice == 3: + print("Goodbye!") + break + else: + print("Invalid option. Please select 1, 2, or 3.") + except ValueError: + print("Please enter a valid number for selection.") + except Exception as e: + print(f"An unexpected error occurred: {e}") From fdc257168940a12ad35fbb02dd168b52fd1f4599 Mon Sep 17 00:00:00 2001 From: black shadow Date: Sun, 20 Oct 2024 00:39:37 +0530 Subject: [PATCH 2/3] Added the Data science Learning Projects folder in data doodles --- Data Doodles/DataScience/Learning_pandas.csv | 4 + .../Learning_pandas_IndexFalse.csv | 4 + Data Doodles/DataScience/NumpyBasic.ipynb | 534 +++ Data Doodles/DataScience/Train_data.csv | 5 + Data Doodles/DataScience/pandas.ipynb | 2887 +++++++++++++++++ Data Doodles/DataScience/pythonOneShot.ipynb | 894 +++++ 6 files changed, 4328 insertions(+) create mode 100644 Data Doodles/DataScience/Learning_pandas.csv create mode 100644 Data Doodles/DataScience/Learning_pandas_IndexFalse.csv create mode 100644 Data Doodles/DataScience/NumpyBasic.ipynb create mode 100644 Data Doodles/DataScience/Train_data.csv create mode 100644 Data Doodles/DataScience/pandas.ipynb create mode 100644 Data Doodles/DataScience/pythonOneShot.ipynb diff --git a/Data Doodles/DataScience/Learning_pandas.csv b/Data Doodles/DataScience/Learning_pandas.csv new file mode 100644 index 0000000..0f03421 --- /dev/null +++ b/Data Doodles/DataScience/Learning_pandas.csv @@ -0,0 +1,4 @@ +,name,marks,city +0,harsh,100,Rampur +1,hani,58,kolkata +2,harshit,91,dholpur diff --git a/Data Doodles/DataScience/Learning_pandas_IndexFalse.csv b/Data Doodles/DataScience/Learning_pandas_IndexFalse.csv new file mode 100644 index 0000000..95fe3b5 --- /dev/null +++ b/Data Doodles/DataScience/Learning_pandas_IndexFalse.csv @@ -0,0 +1,4 @@ +name,marks,city +harsh,100,Rampur +hani,58,kolkata +harshit,91,dholpur diff --git a/Data Doodles/DataScience/NumpyBasic.ipynb b/Data Doodles/DataScience/NumpyBasic.ipynb new file mode 100644 index 0000000..79dd0e8 --- /dev/null +++ b/Data Doodles/DataScience/NumpyBasic.ipynb @@ -0,0 +1,534 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Arrays\n", + "A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative\n", + "integers. \n", + "The number of dimensions is the rank of the array; \n", + "The shape of an array is a tuple of\n", + "integers giving the size of the array along each dimension" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (3,) 1 2 3\n", + "[5 2 3]\n", + "\n", + "[[1 2 3]\n", + " [4 5 6]]\n", + "(2, 3)\n", + "1 2 4\n" + ] + } + ], + "source": [ + "a = np.array([1, 2, 3]) # Create a rank 1 array\n", + "print(type(a), a.shape, a[0], a[1], a[2])\n", + "a[0] = 5 # Change an element of the array\n", + "print(a)\n", + "\n", + "b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array\n", + "print(f'\\n{b}')\n", + "print(b.shape)\n", + "print(b[0, 0], b[0, 1], b[1, 0])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\n", + "[[1. 1.]\n", + " [1. 1.]]\n", + "[[7 7]\n", + " [7 7]]\n", + "[[1. 0. 0.]\n", + " [0. 1. 0.]\n", + " [0. 0. 1.]]\n", + "[[0.6942243 0.97200486]\n", + " [0.05464648 0.88103149]]\n", + "[[0 1]\n", + " [4 7]]\n" + ] + } + ], + "source": [ + "#Creating Numpyarray of zeros\n", + "a = np.zeros((2,2)) # Create an array of all zeros\n", + "print(a)\n", + "\n", + "#Creating Numpyarray of ones\n", + "b = np.ones((2,2)) # Create an array of all ones\n", + "print(b)\n", + "\n", + "#Creating Numpyarray of const value\n", + "c = np.full((2,2), 7) # Create a constant array\n", + "print(c)\n", + "\n", + "#Creating Numpyarray of identity matrix\n", + "d = np.eye(3,3) # Create a 2x2 identity matrix\n", + "print(d)\n", + "\n", + "#Creating Numpyarray of random matrix\n", + "e = np.random.random((2,2)) # Create an array filled with random values\n", + "print(e)\n", + "\n", + "#Creating Numpyarray of random int matrix\n", + "f = np.random.randint(10,size=(2,2)) # Create an array filled with random values\n", + "print(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## iterating np array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "a = np.array([[1, 2], [3, 4], [5, 6]])\n", + "\n", + "# Iterate through each element in the array\n", + "for row in a:\n", + " for element in row:\n", + " print(element)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Array Indexing\n", + "Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional,\n", + "you must specify a slice for each dimension of the array:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 3]\n", + " [6 7]]\n" + ] + } + ], + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "b = a[:2, 1:-1]\n", + "print(b)\n", + "\n", + "#A slice of an array is a view into the same data, so modifying it will modify the original array." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## np.arnage\n", + "The numpy.arange() function is used to generate an array with evenly spaced values within a specified interval. The function returns a one-dimensional array of type numpy.ndarray." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Parameters : \n", + "\n", + "1) start : [optional] start of interval range. By default start = 0\n", + "2) stop : end of interval range\n", + "3) step : [optional] step size of interval. By default step size = 1, \n", + "For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. \n", + "4) dtype : type of output array\n", + "\n", + "Return: \n", + "\n", + "1) Array of evenly spaced values.\n", + "2) Length of array being generated = Ceil((Stop - Start) / Step) " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A\n", + " [[0 1]\n", + " [2 3]] \n", + "\n", + "A\n", + " [4 5 6 7 8 9] \n", + "\n", + "A\n", + " [ 4 7 10 13 16 19] \n", + "\n" + ] + } + ], + "source": [ + "# numpy.arange method\n", + " \n", + "import numpy as geek\n", + " \n", + "print(\"A\\n\", geek.arange(4).reshape(2, 2), \"\\n\")\n", + "print(\"A\\n\", geek.arange(4, 10), \"\\n\")\n", + "print(\"A\\n\", geek.arange(4, 20, 3), \"\\n\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[False False]\n", + " [ True True]\n", + " [ True True]]\n", + "[[False False]\n", + " [ True True]\n", + " [ True False]]\n" + ] + } + ], + "source": [ + "a = np.array([[1,2], [3, 4], [5, 6]])\n", + "bool_idx = (a > 2) # Find the elements of a that are bigger than 2;\n", + "# this returns a numpy array of Booleans of the same\n", + "# shape as a, where each slot of bool_idx tells\n", + "# whether that element of a is > 2.\n", + "print (bool_idx)\n", + "\n", + "#arr[arr>5]=0" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 0]\n", + " [3 4]\n", + " [5 6]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "a = np.array([[1, 2], [3, 4], [5, 6]])\n", + "\n", + "# Use NumPy's vectorized operations to compare and assign values\n", + "a[a > 2] = 1\n", + "a[a <= 2] = 0 # this is overwriting above value\n", + "\n", + "print(a)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 0]\n", + " [1 1]\n", + " [1 1]]\n" + ] + } + ], + "source": [ + "a = np.array([[1, 2], [3, 4], [5, 6]])\n", + "\n", + "# Use np.where to conditionally assign values\n", + "a = np.where(a > 2, 1, 0)\n", + "\n", + "print(a)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Forcing datatype in numpy arrya" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.array([1, 2]) # Let numpy choose the datatype\n", + "y = np.array([1.0, 2.0]) # Let numpy choose the datatype\n", + "z = np.array([1, 2], dtype=np.int64) # Force a particular datatype\n", + "print (x.dtype, y.dtype, z.dtype)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## operations on the numpy array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.array([[1,2],[3,4]], dtype=np.float64)\n", + "y = np.array([[5,6],[7,8]], dtype=np.float64)\n", + "# Elementwise sum; both produce the array\n", + "print x + y\n", + "print np.add(x, y)\n", + "\n", + "# Elementwise difference; both produce the array\n", + "print x - y\n", + "print np.subtract(x, y)\n", + "\n", + "# Elementwise product; both produce the array\n", + "print x * y\n", + "print np.multiply(x, y)\n", + "\n", + "# Elementwise division; both produce the array\n", + "# [[ 0.2 0.33333333]\n", + "# [ 0.42857143 0.5 ]]\n", + "print x / y\n", + "print np.divide(x, y)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original array: [9 5 2 7 1 8 3 6 4]\n", + "Sorted array: [1 2 3 4 5 6 7 8 9]\n", + "Original 2D array:\n", + "[[9 5 2]\n", + " [7 1 8]\n", + " [3 6 4]]\n", + "\n", + "Sorted 2D array along rows:\n", + "[[2 5 9]\n", + " [1 7 8]\n", + " [3 4 6]]\n", + "\n", + "Sorted 2D array along columns:\n", + "[[3 1 2]\n", + " [7 5 4]\n", + " [9 6 8]]\n", + "\n", + "Sorted 2D array flaten:\n", + "[1 2 3 4 5 6 7 8 9]\n" + ] + } + ], + "source": [ + "arr = np.array([9, 5, 2, 7, 1, 8, 3, 6, 4])\n", + "# Sort the array in ascending order\n", + "sorted_arr = np.sort(arr)\n", + "print(\"Original array:\", arr)\n", + "print(\"Sorted array:\", sorted_arr)\n", + "\n", + "\n", + "# Create a 2D NumPy array\n", + "arr_2d = np.array([[9, 5, 2],\n", + "[7, 1, 8],\n", + "[3, 6, 4]])\n", + "# Sort the 2D array along rows (axis=1)\n", + "sorted_arr_row = np.sort(arr_2d, axis=1)\n", + "# Sort the 2D array along columns (axis=0)\n", + "sorted_arr_col = np.sort(arr_2d, axis=0)\n", + "10\n", + "# sort the flattened array\n", + "sorted_arr_flat = np.sort(arr_2d, axis=None)\n", + "print(\"Original 2D array:\")\n", + "print(arr_2d)\n", + "print(\"\\nSorted 2D array along rows:\")\n", + "print(sorted_arr_row)\n", + "print(\"\\nSorted 2D array along columns:\")\n", + "print(sorted_arr_col)\n", + "print(\"\\nSorted 2D array flaten:\")\n", + "print(sorted_arr_flat)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[8 8]\n", + " [8 4]]\n", + "\n", + " [[2 8]\n", + " [8 4]]]\n" + ] + }, + { + "data": { + "text/plain": [ + "array([[[2, 8],\n", + " [8, 4]]])" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr1 = np.random.randint(10,size=(2,2))\n", + "arr2 = np.random.randint(10,size=(2,2))\n", + "\n", + "arr3 = np.stack((arr1,arr2),axis=0)\n", + "print(arr3)\n", + "\n", + "arr4 = np.array_split(arr3,2)\n", + "arr4[1]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Transpose" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 8 7]\n", + " [0 7 5]]\n" + ] + } + ], + "source": [ + "arr = np.random.randint(10,size=(3,2))\n", + "print(arr)\n", + "arr2 = np.transpose(arr)\n", + "print(arr2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# diagonal" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 5 9]\n" + ] + } + ], + "source": [ + "m = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", + "n = np.diagonal(m)\n", + "print(n)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Data Doodles/DataScience/Train_data.csv b/Data Doodles/DataScience/Train_data.csv new file mode 100644 index 0000000..efcca37 --- /dev/null +++ b/Data Doodles/DataScience/Train_data.csv @@ -0,0 +1,5 @@ +,Unnamed: 0,Train no,Speed,City +first,0,12322,100,Rampur +second,1,12534,123,kolkata +third,2,125654,67,dholpur +fourth,3,564523,87,gwalior diff --git a/Data Doodles/DataScience/pandas.ipynb b/Data Doodles/DataScience/pandas.ipynb new file mode 100644 index 0000000..5184fec --- /dev/null +++ b/Data Doodles/DataScience/pandas.ipynb @@ -0,0 +1,2887 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Pandas\n", + "ref: code with harry" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dict1 = {\n", + " \"name\":['harsh','hani','harshit'],\n", + " \"marks\":[100,58,91],\n", + " \"city\":['Rampur','kolkata','dholpur']\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namemarkscity
0harsh100Rampur
1hani58kolkata
2harshit91dholpur
\n", + "
" + ], + "text/plain": [ + " name marks city\n", + "0 harsh 100 Rampur\n", + "1 hani 58 kolkata\n", + "2 harshit 91 dholpur" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = pd.DataFrame(dict1)\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "df.to_csv('Learning_pandas.csv')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "df.to_csv('Learning_pandas_IndexFalse.csv', index = False)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namemarkscity
1hani58kolkata
2harshit91dholpur
\n", + "
" + ], + "text/plain": [ + " name marks city\n", + "1 hani 58 kolkata\n", + "2 harshit 91 dholpur" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head(2) #first two data\n", + "df.tail(2) #last two data" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
marks
count3.000000
mean83.000000
std22.113344
min58.000000
25%74.500000
50%91.000000
75%95.500000
max100.000000
\n", + "
" + ], + "text/plain": [ + " marks\n", + "count 3.000000\n", + "mean 83.000000\n", + "std 22.113344\n", + "min 58.000000\n", + "25% 74.500000\n", + "50% 91.000000\n", + "75% 95.500000\n", + "max 100.000000" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0Train noSpeedCity
0012322100Rampur
1112534123kolkata
2212565467dholpur
3356452387gwalior
\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 Train no Speed City\n", + "0 0 12322 100 Rampur\n", + "1 1 12534 123 kolkata\n", + "2 2 125654 67 dholpur\n", + "3 3 564523 87 gwalior" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = pd.read_csv('Train_data.csv')\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 100\n", + "1 123\n", + "2 67\n", + "3 87\n", + "Name: Speed, dtype: int64" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data['Speed']" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data['Speed'][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\harsh\\AppData\\Local\\Temp\\ipykernel_30328\\2189471887.py:1: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " data['Speed'][0]= 100\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0Train noSpeedCity
0012322100Rampur
1112534123kolkata
2212565467dholpur
3356452387gwalior
\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 Train no Speed City\n", + "0 0 12322 100 Rampur\n", + "1 1 12534 123 kolkata\n", + "2 2 125654 67 dholpur\n", + "3 3 564523 87 gwalior" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data['Speed'][0]= 100\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "data.to_csv('Train_data.csv')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0Train noSpeedCity
first012322100Rampur
second112534123kolkata
third212565467dholpur
fourth356452387gwalior
\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 Train no Speed City\n", + "first 0 12322 100 Rampur\n", + "second 1 12534 123 kolkata\n", + "third 2 125654 67 dholpur\n", + "fourth 3 564523 87 gwalior" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.index = ['first','second','third','fourth']\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "data.to_csv('Train_data.csv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "pandas has two type of data structures \n", + "1. Series: 1D array\n", + "2. Dataframes: tabular spread sheet like data structure" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "pandas.core.series.Series" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ser = pd.Series(np.random.rand(34))\n", + "type(ser)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
00.7333520.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
20.4832870.8787850.5784620.0995460.528282
30.4328570.9949030.6384040.8146920.176334
40.9754390.3956700.1102420.6279800.617755
..................
3290.0178930.7956980.6994190.9214140.643540
3300.0908970.5841910.8744070.2163120.529868
3310.0450630.6114230.2195440.6693190.342805
3320.9764530.2247660.0876560.1943150.074177
3330.2752670.4028110.6557250.7144090.480700
\n", + "

334 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "0 0.733352 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "2 0.483287 0.878785 0.578462 0.099546 0.528282\n", + "3 0.432857 0.994903 0.638404 0.814692 0.176334\n", + "4 0.975439 0.395670 0.110242 0.627980 0.617755\n", + ".. ... ... ... ... ...\n", + "329 0.017893 0.795698 0.699419 0.921414 0.643540\n", + "330 0.090897 0.584191 0.874407 0.216312 0.529868\n", + "331 0.045063 0.611423 0.219544 0.669319 0.342805\n", + "332 0.976453 0.224766 0.087656 0.194315 0.074177\n", + "333 0.275267 0.402811 0.655725 0.714409 0.480700\n", + "\n", + "[334 rows x 5 columns]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf = pd.DataFrame(np.random.rand(334,5), index = np.arange(334))\n", + "newdf" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "pandas.core.frame.DataFrame" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(newdf)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
count334.000000334.000000334.000000334.000000334.000000
mean0.4800680.4986100.5230620.4932960.497099
std0.2895490.2915190.2865340.2802730.285276
min0.0045930.0058330.0027130.0018480.015047
25%0.2326890.2447590.2690590.2570420.242569
50%0.4605120.4931140.5337290.4677140.500282
75%0.7275330.7552130.7846110.7343240.727029
max0.9967460.9994280.9970780.9977520.989904
\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "count 334.000000 334.000000 334.000000 334.000000 334.000000\n", + "mean 0.480068 0.498610 0.523062 0.493296 0.497099\n", + "std 0.289549 0.291519 0.286534 0.280273 0.285276\n", + "min 0.004593 0.005833 0.002713 0.001848 0.015047\n", + "25% 0.232689 0.244759 0.269059 0.257042 0.242569\n", + "50% 0.460512 0.493114 0.533729 0.467714 0.500282\n", + "75% 0.727533 0.755213 0.784611 0.734324 0.727029\n", + "max 0.996746 0.999428 0.997078 0.997752 0.989904" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 float64\n", + "1 float64\n", + "2 float64\n", + "3 float64\n", + "4 float64\n", + "dtype: object" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.dtypes" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\harsh\\AppData\\Local\\Temp\\ipykernel_30328\\687807849.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value 'harsh' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n", + " newdf[0][0]= 'harsh'\n" + ] + } + ], + "source": [ + "newdf[0][0]= 'harsh'" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 object\n", + "1 float64\n", + "2 float64\n", + "3 float64\n", + "4 float64\n", + "dtype: object" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.dtypes" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n", + " ...\n", + " 324, 325, 326, 327, 328, 329, 330, 331, 332, 333],\n", + " dtype='int32', length=334)" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.index" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "RangeIndex(start=0, stop=5, step=1)" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.columns" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([['harsh', 0.5748147773043275, 0.538166223898115,\n", + " 0.11668898175504716, 0.6998871928862739],\n", + " [0.0804029706862206, 0.24207642306722377, 0.6438955787102314,\n", + " 0.4122599030300176, 0.7903908546552326],\n", + " [0.48328749382786074, 0.8787851432789862, 0.5784621718485413,\n", + " 0.09954626773466257, 0.5282818145340306],\n", + " ...,\n", + " [0.04506305300067559, 0.6114229378127994, 0.21954395084577671,\n", + " 0.6693192648038008, 0.34280493983066507],\n", + " [0.9764532630754699, 0.22476583561785723, 0.08765642736610613,\n", + " 0.19431533833336423, 0.07417677034112735],\n", + " [0.27526711921905045, 0.4028110864681531, 0.6557253020962932,\n", + " 0.7144092691966278, 0.48069963424364004]], dtype=object)" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.to_numpy()" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "newdf[0][0]= 0.111" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
00.1110.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
20.4832870.8787850.5784620.0995460.528282
30.4328570.9949030.6384040.8146920.176334
40.9754390.3956700.1102420.6279800.617755
\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "0 0.111 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "2 0.483287 0.878785 0.578462 0.099546 0.528282\n", + "3 0.432857 0.994903 0.638404 0.814692 0.176334\n", + "4 0.975439 0.395670 0.110242 0.627980 0.617755" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Transposing" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0123456789...324325326327328329330331332333
00.1110.0804030.4832870.4328570.9754390.3919510.8771270.047150.2145230.023785...0.7012850.208530.1571540.0800310.3727450.0178930.0908970.0450630.9764530.275267
10.5748150.2420760.8787850.9949030.395670.8141330.9994280.4727240.9858370.553832...0.0910880.7835340.8634220.8020060.4948760.7956980.5841910.6114230.2247660.402811
20.5381660.6438960.5784620.6384040.1102420.5630420.3676390.3605110.5699160.599692...0.6836720.6727880.6256210.2161020.6663920.6994190.8744070.2195440.0876560.655725
30.1166890.412260.0995460.8146920.627980.1985520.5918360.7717620.2350330.660796...0.2902420.3981670.1041580.6409090.6177970.9214140.2163120.6693190.1943150.714409
40.6998870.7903910.5282820.1763340.6177550.6303650.8623570.8640360.4824420.064887...0.2476810.938290.315170.3797640.8809870.643540.5298680.3428050.0741770.4807
\n", + "

5 rows × 334 columns

\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4 5 6 \\\n", + "0 0.111 0.080403 0.483287 0.432857 0.975439 0.391951 0.877127 \n", + "1 0.574815 0.242076 0.878785 0.994903 0.39567 0.814133 0.999428 \n", + "2 0.538166 0.643896 0.578462 0.638404 0.110242 0.563042 0.367639 \n", + "3 0.116689 0.41226 0.099546 0.814692 0.62798 0.198552 0.591836 \n", + "4 0.699887 0.790391 0.528282 0.176334 0.617755 0.630365 0.862357 \n", + "\n", + " 7 8 9 ... 324 325 326 327 \\\n", + "0 0.04715 0.214523 0.023785 ... 0.701285 0.20853 0.157154 0.080031 \n", + "1 0.472724 0.985837 0.553832 ... 0.091088 0.783534 0.863422 0.802006 \n", + "2 0.360511 0.569916 0.599692 ... 0.683672 0.672788 0.625621 0.216102 \n", + "3 0.771762 0.235033 0.660796 ... 0.290242 0.398167 0.104158 0.640909 \n", + "4 0.864036 0.482442 0.064887 ... 0.247681 0.93829 0.31517 0.379764 \n", + "\n", + " 328 329 330 331 332 333 \n", + "0 0.372745 0.017893 0.090897 0.045063 0.976453 0.275267 \n", + "1 0.494876 0.795698 0.584191 0.611423 0.224766 0.402811 \n", + "2 0.666392 0.699419 0.874407 0.219544 0.087656 0.655725 \n", + "3 0.617797 0.921414 0.216312 0.669319 0.194315 0.714409 \n", + "4 0.880987 0.64354 0.529868 0.342805 0.074177 0.4807 \n", + "\n", + "[5 rows x 334 columns]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.T" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
00.1110.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
20.4832870.8787850.5784620.0995460.528282
30.4328570.9949030.6384040.8146920.176334
40.9754390.3956700.1102420.6279800.617755
\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "0 0.111 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "2 0.483287 0.878785 0.578462 0.099546 0.528282\n", + "3 0.432857 0.994903 0.638404 0.814692 0.176334\n", + "4 0.975439 0.395670 0.110242 0.627980 0.617755" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
00.1110.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
20.4832870.8787850.5784620.0995460.528282
30.4328570.9949030.6384040.8146920.176334
40.9754390.3956700.1102420.6279800.617755
..................
3290.0178930.7956980.6994190.9214140.643540
3300.0908970.5841910.8744070.2163120.529868
3310.0450630.6114230.2195440.6693190.342805
3320.9764530.2247660.0876560.1943150.074177
3330.2752670.4028110.6557250.7144090.480700
\n", + "

334 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "0 0.111 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "2 0.483287 0.878785 0.578462 0.099546 0.528282\n", + "3 0.432857 0.994903 0.638404 0.814692 0.176334\n", + "4 0.975439 0.395670 0.110242 0.627980 0.617755\n", + ".. ... ... ... ... ...\n", + "329 0.017893 0.795698 0.699419 0.921414 0.643540\n", + "330 0.090897 0.584191 0.874407 0.216312 0.529868\n", + "331 0.045063 0.611423 0.219544 0.669319 0.342805\n", + "332 0.976453 0.224766 0.087656 0.194315 0.074177\n", + "333 0.275267 0.402811 0.655725 0.714409 0.480700\n", + "\n", + "[334 rows x 5 columns]" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.sort_index(axis=0,ascending=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "pandas.core.series.Series" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(newdf[0])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Views in Pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\harsh\\AppData\\Local\\Temp\\ipykernel_30328\\3654261728.py:2: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " newdf2[0][0] = 2003\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
020030.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
20.4832870.8787850.5784620.0995460.528282
30.4328570.9949030.6384040.8146920.176334
40.9754390.3956700.1102420.6279800.617755
..................
3290.0178930.7956980.6994190.9214140.643540
3300.0908970.5841910.8744070.2163120.529868
3310.0450630.6114230.2195440.6693190.342805
3320.9764530.2247660.0876560.1943150.074177
3330.2752670.4028110.6557250.7144090.480700
\n", + "

334 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "0 2003 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "2 0.483287 0.878785 0.578462 0.099546 0.528282\n", + "3 0.432857 0.994903 0.638404 0.814692 0.176334\n", + "4 0.975439 0.395670 0.110242 0.627980 0.617755\n", + ".. ... ... ... ... ...\n", + "329 0.017893 0.795698 0.699419 0.921414 0.643540\n", + "330 0.090897 0.584191 0.874407 0.216312 0.529868\n", + "331 0.045063 0.611423 0.219544 0.669319 0.342805\n", + "332 0.976453 0.224766 0.087656 0.194315 0.074177\n", + "333 0.275267 0.402811 0.655725 0.714409 0.480700\n", + "\n", + "[334 rows x 5 columns]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf2 = newdf\n", + "newdf2[0][0] = 2003\n", + "newdf2" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "newdf2 = newdf.copy()\n", + "# now a copy is created" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
020050.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4\n", + "0 2005 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.loc[0,0] = 2005\n", + "newdf.head(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCDE
020050.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
\n", + "
" + ], + "text/plain": [ + " A B C D E\n", + "0 2005 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.columns = list(\"ABCDE\")\n", + "newdf.head(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCDE
020130.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
\n", + "
" + ], + "text/plain": [ + " A B C D E\n", + "0 2013 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.loc[0,'A'] = '2013'\n", + "newdf.head(2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Removing DATA in pandas\n", + "\n", + "df.drop(0, asix =1) # column\n", + "\n", + "df.drop(0, asix =0) # row" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CD
10.6438960.412260
20.5784620.099546
\n", + "
" + ], + "text/plain": [ + " C D\n", + "1 0.643896 0.412260\n", + "2 0.578462 0.099546" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.loc[[1,2], ['C','D']]" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCDE
10.0804030.2420760.6438960.4122600.790391
20.4832870.8787850.5784620.0995460.528282
\n", + "
" + ], + "text/plain": [ + " A B C D E\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "2 0.483287 0.878785 0.578462 0.099546 0.528282" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.loc[:, ['C','D']] # Saari rows\n", + "newdf.loc[[1,2], :] # Saari column" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Queries\n", + "DataFrame.loc[Query]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Accoding to CWH video but it doesn't work\n", + "newdf.loc[(newdf['A']<0.3)]" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCDE
10.0804030.2420760.6438960.4122600.790391
70.0471500.4727240.3605110.7717620.864036
80.2145230.9858370.5699160.2350330.482442
90.0237850.5538320.5996920.6607960.064887
100.0626430.5717420.7157360.4621840.695964
..................
3270.0800310.8020060.2161020.6409090.379764
3290.0178930.7956980.6994190.9214140.643540
3300.0908970.5841910.8744070.2163120.529868
3310.0450630.6114230.2195440.6693190.342805
3330.2752670.4028110.6557250.7144090.480700
\n", + "

109 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " A B C D E\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391\n", + "7 0.047150 0.472724 0.360511 0.771762 0.864036\n", + "8 0.214523 0.985837 0.569916 0.235033 0.482442\n", + "9 0.023785 0.553832 0.599692 0.660796 0.064887\n", + "10 0.062643 0.571742 0.715736 0.462184 0.695964\n", + ".. ... ... ... ... ...\n", + "327 0.080031 0.802006 0.216102 0.640909 0.379764\n", + "329 0.017893 0.795698 0.699419 0.921414 0.643540\n", + "330 0.090897 0.584191 0.874407 0.216312 0.529868\n", + "331 0.045063 0.611423 0.219544 0.669319 0.342805\n", + "333 0.275267 0.402811 0.655725 0.714409 0.480700\n", + "\n", + "[109 rows x 5 columns]" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf['A'] = pd.to_numeric(newdf['A'], errors='coerce') # Convert 'A' column to numeric, setting non-numeric values to NaN\n", + "result = newdf.loc[newdf['A'] < 0.3]\n", + "result\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCDE
02013.0000000.5748150.5381660.1166890.699887
10.0804030.2420760.6438960.4122600.790391
\n", + "
" + ], + "text/plain": [ + " A B C D E\n", + "0 2013.000000 0.574815 0.538166 0.116689 0.699887\n", + "1 0.080403 0.242076 0.643896 0.412260 0.790391" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.head(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.6998871928862739" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.iloc[0,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CD
10.6438960.412260
20.5784620.099546
\n", + "
" + ], + "text/plain": [ + " C D\n", + "1 0.643896 0.412260\n", + "2 0.578462 0.099546" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#iloc me numbers se kaam ho jaega\n", + "newdf.iloc[[1,2],[2,3]]\n", + "#Loc me row or column ke naam dene padenge" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CDE
00.5381660.1166890.699887
10.6438960.4122600.790391
20.5784620.0995460.528282
30.6384040.8146920.176334
40.1102420.6279800.617755
............
3290.6994190.9214140.643540
3300.8744070.2163120.529868
3310.2195440.6693190.342805
3320.0876560.1943150.074177
3330.6557250.7144090.480700
\n", + "

334 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " C D E\n", + "0 0.538166 0.116689 0.699887\n", + "1 0.643896 0.412260 0.790391\n", + "2 0.578462 0.099546 0.528282\n", + "3 0.638404 0.814692 0.176334\n", + "4 0.110242 0.627980 0.617755\n", + ".. ... ... ...\n", + "329 0.699419 0.921414 0.643540\n", + "330 0.874407 0.216312 0.529868\n", + "331 0.219544 0.669319 0.342805\n", + "332 0.087656 0.194315 0.074177\n", + "333 0.655725 0.714409 0.480700\n", + "\n", + "[334 rows x 3 columns]" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf.drop(['A','B'], axis=1 , inplace=True)\n", + "#inplace will make the change to original data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "newdf.reset_index() #Resets the index\n", + "newdf.reset_index(drop = true) #Resets the index and ddrops previos index\n", + "newdf.reset_index(drop = true, inplace= true) #Resets the index and ddrops previos index also change the original data" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 False\n", + "1 False\n", + "2 False\n", + "3 False\n", + "4 False\n", + " ... \n", + "329 False\n", + "330 False\n", + "331 False\n", + "332 False\n", + "333 False\n", + "Name: B, Length: 334, dtype: bool" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf['B'].isnull() #finds whiich index values holds the null valuuue" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame({\"name\": ['divyat','arshita','arin'],\n", + "\"toy\": [np.nan,'Batmobile','bullwhip'],\n", + "\"Born\":[pd.NaT, pd.Timestamp(\"2005,06,03\"),pd.NaT]})" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
nametoyBorn
0divyatNaNNaT
1arshitaBatmobile2005-03-01
2arinbullwhipNaT
\n", + "
" + ], + "text/plain": [ + " name toy Born\n", + "0 divyat NaN NaT\n", + "1 arshita Batmobile 2005-03-01\n", + "2 arin bullwhip NaT" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
nametoyBorn
1arshitaBatmobile2005-03-01
\n", + "
" + ], + "text/plain": [ + " name toy Born\n", + "1 arshita Batmobile 2005-03-01" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.dropna()\n", + "df.dropna(how='all') #removes if all field null" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.drop_duplicates(subset=['Column/row name'],keep=\"first|last|false\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.shape()\n", + "df.info()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#counting unique\n", + "df['name'].value_counts(dropna=False)\n", + "#seting false count NA \n", + "#seting true skips NA " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.isnull()\n", + "df.notnull()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Data Doodles/DataScience/pythonOneShot.ipynb b/Data Doodles/DataScience/pythonOneShot.ipynb new file mode 100644 index 0000000..b93d2c9 --- /dev/null +++ b/Data Doodles/DataScience/pythonOneShot.ipynb @@ -0,0 +1,894 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# This notebook contains basic python" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Basics" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world\n", + "**********\n" + ] + } + ], + "source": [ + "#first code \n", + "print(\"Hello world\")\n", + "print(\"*\" * 10) # this is expression inside the paranthesis" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "harsh 4.9 is smart True\n" + ] + } + ], + "source": [ + "#variables \n", + "price = 10 #first it conveted into binary to store in memory\n", + "rating = 4.9\n", + "name = \"harsh\"\n", + "is_smart = True\n", + "print(price)\n", + "print(name, rating,\"is smart\",is_smart )" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hi harsh\n" + ] + } + ], + "source": [ + "#Inputs\n", + "name = input('what is your name? ')\n", + "print(\"Hi \"+ name)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "16\n" + ] + } + ], + "source": [ + "#type conversion \\\n", + "brith_year = input('Birth year: ')\n", + "print(type(brith_year))\n", + "age = 2023 - int(brith_year)\n", + "print(type(age))\n", + "print(age)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "166.66666666666666\n" + ] + } + ], + "source": [ + "#exercise one \n", + "weight_Kg = int(input(\"enter your weight in kg \"))\n", + "weight_lbs = weight_Kg / 0.45\n", + "print(weight_lbs)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Strings" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Hi everyone \n", + "This is harsh shrivastava\n", + "\n", + "r\n", + "t\n", + "\n", + "Hi eve\n", + "\n", + "Hi everyone \n", + "This is harsh shrivastava\n", + "\n", + "\n", + "Hi every\n", + "ars\n" + ] + } + ], + "source": [ + "#strings - use case of '' and \"\"\n", + "string = \"arin's girlfriend\"\n", + "string = 'girlfriend of \"arin\"'\n", + "\n", + "#string in multiline\n", + "string= '''\n", + "Hi everyone \n", + "This is harsh shrivastava\n", + "'''\n", + "#indexing goes like 0,1,2,3,4,5,6,...\n", + "print(string)\n", + "print(string[7])\n", + "#negative indexing - since index of first is 0 hence last index is known as -1 index\n", + "print(string[-5])\n", + "#using [0:2] type indexing \n", + "#this start the indexing from one all the way to index 1 but exclude 2\n", + "print(string[0:7])\n", + "print(string[0:]) # print all the char upto the end\n", + "print(string[:9]) #assume start as 0 and take that upto 9\n", + "#note \n", + "#print(course)==print(course[:])\n", + "string='harsh'\n", + "print(name[1:-1])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Formatted string" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "harsh [shrivastava] is a coder\n", + "harsh [shrivastava] is a coder\n" + ] + } + ], + "source": [ + "first = \"harsh\"\n", + "last = \"shrivastava\"\n", + "message = first + ' [' + last +'] is a coder'\n", + "print(message) \n", + "msg = f'{first} [{last}] is a coder'\n", + "print(msg)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## String Methods" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n", + "DOES ARIN HAVE A GIRLFRIEND\n", + "does arin have a girlfriend\n", + "17\n", + "yes, arin have a girlfriend\n", + "does Arin hAve A girlfriend\n", + "True\n" + ] + } + ], + "source": [ + "string = \"does arin have a girlfriend\"\n", + "print(len(string)) #gives lenght \n", + "print(string.upper()) #return string in uuper case \n", + "print(string.lower()) #return sting in lower case\n", + "#this does't change original string infact return a duplicate string\n", + "print(string.find('girlfriend'))\n", + "print(string.replace('does','yes,'))\n", + "print(string.replace('a', 'A'))\n", + "print('arin' in string) #CASE SENSITIVE\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Arithemtic Operations " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''\n", + "+\n", + "-\n", + "*\n", + "/ # returns a float\n", + "// # returns an int\n", + "% # returns the remainder of division\n", + "** # exponentiation - x ** y = x to the power of y\n", + "\n", + "Augmented assignment operator:\n", + "x = x + 10\n", + "x += 10\n", + "\n", + "Operator precedence:\n", + "1. parenthesis\n", + "2. exponentiation\n", + "3. multiplication / division\n", + "4. addition / subtraction\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Basic of Math" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "-3\n", + "2.9\n", + "3\n", + "2\n", + "9.0\n", + "10\n" + ] + } + ], + "source": [ + "#math functions\n", + "import math\n", + "x = 4.5\n", + "print(round(x)) \n", + "x= -2.9\n", + "print(round(x)) #round off\n", + "print(abs(x)) #absolute\n", + "print(math.ceil(2.9)) # ceil\n", + "print(math.floor(2.9)) # floor\n", + "x=3\n", + "print(math.pow(x, 2)) # power\n", + "print(math.comb(5,3)) # combinations\n", + "\n", + "#more func in math module" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# If statement" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It's a hot day\n", + "Eligible for loan\n" + ] + } + ], + "source": [ + "is_hot =True\n", + "is_cold = True\n", + "if is_hot:\n", + " print(\"It's a hot day\")\n", + "elif is_cold:\n", + " print(\"It's a cold day\")\n", + "else:\n", + " print(\"It's a rainy day\")\n", + "\n", + "#logical operator\n", + "has_high_income = True\n", + "has_good_credit = True\n", + "\n", + "if has_high_income and has_good_credit:\n", + " print(\"Eligible for loan\")\n", + "\n", + "#other operator \n", + "# OR anyone\n", + "# NOT alterone\n", + "# AND Both" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Loops" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "1\n", + "2\n", + "3\n", + "4\n", + "(0,0)\n", + "(0,1)\n", + "(1,0)\n", + "(1,1)\n", + "(2,0)\n", + "(2,1)\n" + ] + } + ], + "source": [ + "# While Loop\n", + "i = 1\n", + "while i < 5:\n", + " print(i)\n", + " i += 1\n", + "\n", + "# WHile loop can also have else part\n", + "'''\n", + "while condition\n", + " code\n", + " if condition\n", + " break\n", + "else\n", + " code\n", + "\n", + "if while loop executed fully without the break then the else partt is executed\n", + "'''\n", + "\n", + "\n", + "# for loops\n", + "for i in range(1, 5):\n", + " print(i)\n", + "'''\n", + "• range(5): generates 0, 1, 2, 3, 4\n", + "• range(1, 5): generates 1, 2, 3, 4\n", + "• range(1, 5, 2): generates 1, 3 \n", + "'''\n", + "\n", + "# Nested Loop\n", + "for i in range(3):\n", + " for o in range(2):\n", + " print(f'({i},{o})')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# List\n", + "A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.\n", + "A list in Python CAN contain different types of data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "numbers = [1, 2, 3, 4, 5]\n", + "numbers[0] # returns the first item\n", + "numbers[1] # returns the second item\n", + "numbers[-1] # returns the first item from the end\n", + "numbers[-2] # returns the second item from the end\n", + "\n", + "numbers.append(6) # adds 6 to the end\n", + "numbers.insert(0, 6) # adds 6 at index position of 0\n", + "numbers.remove(6) # removes 6\n", + "numbers.pop() # removes the last item\n", + "numbers.clear() # removes all the items\n", + "numbers.index(8) # returns the index of first occurrence of 8\n", + "numbers.sort() # sorts the list\n", + "numbers.reverse() # reverses the list\n", + "numbers.copy() # returns a copy of the list\n", + "\n", + "matrix = [\n", + " [1,2,3],\n", + " [4,5,6],\n", + " [7,8,9]\n", + "]\n", + "matrix[0][1]\n", + "\n", + "#declaring an empty list\n", + "empty = []\n", + "empty = list()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Unpacking\n", + "We can unpack a list or a tuple into separate variables: " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3413 1231 123 [12, 2, 2, 2, 2, 2, 2, 2, 2]\n" + ] + } + ], + "source": [ + "coordinates = (1,2,3)\n", + "x,y,z = coordinates\n", + "\n", + "#important eg\n", + "a,b,c, *other = [3413,1231,123,12,2,2,2,2,2,2,2,2]\n", + "print(a,b,c,other)\n", + "# here othercontains all value starting form 12 upto end" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tuple\n", + "They are like read-only lists. We use them to store a list of items. But once we define a tuple, we cannot add or remove items or change the existing items. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tuple = (1,2,3)\n", + "\n", + "# creating an empty tuple\n", + "empty = ()\n", + "empty = tuple()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dictionary\n", + "We use dictionaries to store key/value pairs.\n", + "We can use strings or numbers to define keys. They should be unique. We can use\n", + "any types for the values. \n", + "\n", + "### Is dictionary orderded?\n", + "As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.\n", + "\n", + "When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.\n", + "\n", + "Unordered means that the items does not have a defined order, you cannot refer to an item by using an index." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John Smith\n", + "True\n" + ] + } + ], + "source": [ + "customer = {\n", + " 'name': 'John Smith',\n", + " 'age': 30,\n", + " 'is_verified': True\n", + "}\n", + "'''\n", + "dictinooary=(key:value)\n", + "name[key]\n", + "name.get[key]\n", + "'''\n", + "print(customer['name'])\n", + "print('name' in customer)\n", + "\n", + "#creating an empty dictionary\n", + "empty=dict()\n", + "empty={}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# sets\n", + "A Set in Python programming is an unordered collection data type that is iterable, mutable and has no duplicate elements." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 31}\n", + "{1, 2, 3, 6, 31}\n", + "{1, 3}\n", + "{1, 2, 3, 6, 31}\n" + ] + } + ], + "source": [ + "#intialisation\n", + "a1 = {1,3} # with curly braces\n", + "\n", + "# even if we try to initialise set and insert duplicate values in it we will have diff values and print values without repetetion\n", + "a2 = {3,3,3,3,31,1,1,1,1,2,2,2,2}\n", + "print(a2)\n", + "a2.add(6)\n", + "print(a2)\n", + "\n", + "print(a1&a2) #and of sets\n", + "print(a1|a2) #or of sets" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# functions" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hi arin\n", + "congratulations! on pulling a girl\n", + "hi abhishek\n", + "congratulations! on pulling a girl\n", + "hi divyat\n", + "congratulations! on pulling a girl\n" + ] + } + ], + "source": [ + "def greet_user(name):#here it is parameter\n", + " print(f'hi {name}')\n", + " print(\"congratulations! on pulling a girl\")\n", + "\n", + "\n", + "greet_user(\"arin\")#here it argument \n", + "greet_user(\"abhishek\")\n", + "greet_user(\"divyat\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#keryword argument\n", + "'''\n", + "function(name,surname)\n", + "\n", + "while calling a function \n", + "function(surname=shrivastava,name=harsh)\n", + "now the position doesn't matter\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "81" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#return\n", + "def square(number):\n", + " return number*number\n", + "square(9)\n", + "print(square(8))\n", + "\n", + "#funciton can also be Return None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# exception" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " age = int(input('Age: '))\n", + " income = 2000\n", + " risk = income / age\n", + " print(age)\n", + "except ZeroDivisionError:\n", + " print(\"age can't be zero\")\n", + "except ValueError:\n", + " print('invalid Value')\n", + "\n", + "#0 means successful execution \n", + "#1 means error" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SLicing" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "nums = range(5) # range is a built-in function that creates a list of␣\n", + "#,→integers\n", + "print nums # Prints \"[0, 1, 2, 3, 4]\"\n", + "print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints \"[2, 3]\"\n", + "print nums[2:] # Get a slice from index 2 to the end; prints \"[2, 3, 4]\"\n", + "print nums[:2] # Get a slice from the start to index 2 (exclusive); prints␣\n", + "#,→\"[0, 1]\"\n", + "print nums[:] # Get a slice of the whole list; prints [\"0, 1, 2, 3, 4]\"\n", + "print nums[:-1] # Slice indices can be negative; prints [\"0, 1, 2, 3]\"\n", + "nums[2:4] = [8, 9] # Assign a new sublist to a slice\n", + "print nums # Prints \"[0, 1, 8, 9, 4]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Enumerate\n", + "The enumerate () method adds a counter to an iterable and returns it in the form of an enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() function." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Return type: \n", + "[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]\n", + "[(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]\n" + ] + } + ], + "source": [ + "l1 = [\"eat\", \"sleep\", \"repeat\"]\n", + "s1 = \"geek\"\n", + " \n", + "# creating enumerate objects\n", + "obj1 = enumerate(l1)\n", + "obj2 = enumerate(s1)\n", + " \n", + "print (\"Return type:\", type(obj1))\n", + "print (list(enumerate(l1)))\n", + " \n", + "# changing start index to 2 from 0\n", + "print (list(enumerate(s1, 2)))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 'eat')\n", + "(1, 'sleep')\n", + "(2, 'repeat')\n", + "100 eat\n", + "101 sleep\n", + "102 repeat\n", + "0\n", + "eat\n", + "1\n", + "sleep\n", + "2\n", + "repeat\n" + ] + } + ], + "source": [ + "l1 = [\"eat\", \"sleep\", \"repeat\"]\n", + " \n", + "# printing the tuples in object directly\n", + "for ele in enumerate(l1):\n", + " print (ele)\n", + " \n", + "# changing index and printing separately\n", + "for count, ele in enumerate(l1, 100):\n", + " print (count, ele)\n", + " \n", + "# getting desired output from tuple\n", + "for count, ele in enumerate(l1):\n", + " print(count)\n", + " print(ele)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# classes" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, Fred\n", + "HELLO, FRED!\n" + ] + } + ], + "source": [ + "class Greeter:\n", + " # Constructor\n", + " def __init__(self, name):\n", + " self.name = name # Create an instance variable\n", + " # Instance method\n", + " def greet(self, loud=False):\n", + " if loud:\n", + " print('HELLO, %s!' % self.name.upper())\n", + " else:\n", + " print('Hello, %s' % self.name)\n", + "g = Greeter('Fred') # Construct an instance of the Greeter class\n", + "g.greet() # Call an instance method; prints \"Hello, Fred\"\n", + "g.greet(loud=True) # Call an instance method; prints \"HELLO, FRED!\"\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 894b3523f21cf85dc9f632d11d26e7b03306518d Mon Sep 17 00:00:00 2001 From: black shadow Date: Sun, 20 Oct 2024 00:43:32 +0530 Subject: [PATCH 3/3] Added the javascript Projects required in Machine Learning --- .../JavaScript/Factorialusingclass.js | 23 ++ .../JavaScript/bestFitAlgorithm/.gitignore | 1 + .../bestFitAlgorithm/bestFitAlgorithm.js | 365 ++++++++++++++++++ .../bestFitAlgorithm/package-lock.json | 17 + .../JavaScript/bestFitAlgorithm/package.json | 16 + Data Doodles/JavaScript/diamondPattern.js | 51 +++ Data Doodles/JavaScript/radixsort.js | 40 ++ 7 files changed, 513 insertions(+) create mode 100644 Data Doodles/JavaScript/Factorialusingclass.js create mode 100644 Data Doodles/JavaScript/bestFitAlgorithm/.gitignore create mode 100644 Data Doodles/JavaScript/bestFitAlgorithm/bestFitAlgorithm.js create mode 100644 Data Doodles/JavaScript/bestFitAlgorithm/package-lock.json create mode 100644 Data Doodles/JavaScript/bestFitAlgorithm/package.json create mode 100644 Data Doodles/JavaScript/diamondPattern.js create mode 100644 Data Doodles/JavaScript/radixsort.js diff --git a/Data Doodles/JavaScript/Factorialusingclass.js b/Data Doodles/JavaScript/Factorialusingclass.js new file mode 100644 index 0000000..8a772f6 --- /dev/null +++ b/Data Doodles/JavaScript/Factorialusingclass.js @@ -0,0 +1,23 @@ +class Factorial { + constructor(n) { + this.n = n; + } + + calculate() { + if (this.n < 0) { + throw new Error('Factorial is not defined for negative numbers'); + } + return this._factorial(this.n); + } + + _factorial(num) { + if (num <= 1) return 1; + return num * this._factorial(num - 1); + } + } + + // Sample input + const number = 5; + const factorialObj = new Factorial(number); + console.log(`Factorial of ${number} is: ${factorialObj.calculate()}`); + \ No newline at end of file diff --git a/Data Doodles/JavaScript/bestFitAlgorithm/.gitignore b/Data Doodles/JavaScript/bestFitAlgorithm/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/Data Doodles/JavaScript/bestFitAlgorithm/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/Data Doodles/JavaScript/bestFitAlgorithm/bestFitAlgorithm.js b/Data Doodles/JavaScript/bestFitAlgorithm/bestFitAlgorithm.js new file mode 100644 index 0000000..1865f64 --- /dev/null +++ b/Data Doodles/JavaScript/bestFitAlgorithm/bestFitAlgorithm.js @@ -0,0 +1,365 @@ +const Decimal = require('decimal.js'); + +// Constants +const RotationType = { + RT_WHD: 0, + RT_HWD: 1, + RT_HDW: 2, + RT_DHW: 3, + RT_DWH: 4, + RT_WDH: 5, + ALL: [0, 1, 2, 3, 4, 5], +}; + +const Axis = { + WIDTH: 0, + HEIGHT: 1, + DEPTH: 2, + ALL: [0, 1, 2], +}; + +// Helper functions +function rectIntersect(item1, item2, x, y) { + const d1 = item1.getDimension(); + const d2 = item2.getDimension(); + + const cx1 = item1.position[x] + d1[x] / 2; + const cy1 = item1.position[y] + d1[y] / 2; + const cx2 = item2.position[x] + d2[x] / 2; + const cy2 = item2.position[y] + d2[y] / 2; + + const ix = Math.max(cx1, cx2) - Math.min(cx1, cx2); + const iy = Math.max(cy1, cy2) - Math.min(cy1, cy2); + + return ix < (d1[x] + d2[x]) / 2 && iy < (d1[y] + d2[y]) / 2; +} + +function intersect(item1, item2) { + return ( + rectIntersect(item1, item2, Axis.WIDTH, Axis.HEIGHT) && + rectIntersect(item1, item2, Axis.HEIGHT, Axis.DEPTH) && + rectIntersect(item1, item2, Axis.WIDTH, Axis.DEPTH) + ); +} + +function getLimitNumberOfDecimals(numberOfDecimals) { + return new Decimal('1.' + '0'.repeat(numberOfDecimals)); +} + +function setToDecimal(value, numberOfDecimals) { + const limit = getLimitNumberOfDecimals(numberOfDecimals); + return new Decimal(value).toDecimalPlaces(numberOfDecimals); +} + + +// Item class +class Item { + constructor(name, width, height, depth, weight) { + this.name = name; + this.width = width; + this.height = height; + this.depth = depth; + this.weight = weight; + this.rotationType = 0; + this.position = START_POSITION; + this.numberOfDecimals = DEFAULT_NUMBER_OF_DECIMALS; + } + + formatNumbers(numberOfDecimals) { + this.width = setToDecimal(this.width, numberOfDecimals); + this.height = setToDecimal(this.height, numberOfDecimals); + this.depth = setToDecimal(this.depth, numberOfDecimals); + this.weight = setToDecimal(this.weight, numberOfDecimals); + this.numberOfDecimals = numberOfDecimals; + } + + string() { + return `${this.name}(${this.width}x${this.height}x${this.depth}, weight: ${this.weight}) pos(${this.position}) rt(${this.rotationType}) vol(${this.getVolume()})`; + } + + getVolume() { + return setToDecimal(this.width * this.height * this.depth, this.numberOfDecimals); + } + + getDimension() { + let dimension = []; + switch (this.rotationType) { + case RotationType.RT_WHD: + dimension = [this.width, this.height, this.depth]; + break; + case RotationType.RT_HWD: + dimension = [this.height, this.width, this.depth]; + break; + case RotationType.RT_HDW: + dimension = [this.height, this.depth, this.width]; + break; + case RotationType.RT_DHW: + dimension = [this.depth, this.height, this.width]; + break; + case RotationType.RT_DWH: + dimension = [this.depth, this.width, this.height]; + break; + case RotationType.RT_WDH: + dimension = [this.width, this.depth, this.height]; + break; + } + return dimension; + } +} + +// Bin class +class Bin { + constructor(name, width, height, depth, maxWeight) { + this.name = name; + this.width = width; + this.height = height; + this.depth = depth; + this.maxWeight = maxWeight; + this.items = []; + this.unfittedItems = []; + this.numberOfDecimals = DEFAULT_NUMBER_OF_DECIMALS; + } + + formatNumbers(numberOfDecimals) { + this.width = setToDecimal(this.width, numberOfDecimals); + this.height = setToDecimal(this.height, numberOfDecimals); + this.depth = setToDecimal(this.depth, numberOfDecimals); + this.maxWeight = setToDecimal(this.maxWeight, numberOfDecimals); + this.numberOfDecimals = numberOfDecimals; + } + + string() { + return `${this.name}(${this.width}x${this.height}x${this.depth}, max_weight:${this.maxWeight}) vol(${this.getVolume()})`; + } + + getVolume() { + return setToDecimal(this.width * this.height * this.depth, this.numberOfDecimals); + } + + getTotalWeight() { + let totalWeight = 0; + for (const item of this.items) { + totalWeight += item.weight; + } + return setToDecimal(totalWeight, this.numberOfDecimals); + } + + putItem(item, pivot) { + let fit = false; + const validItemPosition = item.position; + item.position = pivot; + + for (let i = 0; i < RotationType.ALL.length; i++) { + item.rotationType = RotationType.ALL[i]; + const dimension = item.getDimension(); + if ( + this.width < pivot[0] + dimension[0] || + this.height < pivot[1] + dimension[1] || + this.depth < pivot[2] + dimension[2] + ) { + continue; + } + + fit = true; + + for (const currentItemInBin of this.items) { + if (intersect(currentItemInBin, item)) { + fit = false; + break; + } + } + + if (fit) { + if (this.getTotalWeight() + item.weight > this.maxWeight) { + fit = false; + return fit; + } + this.items.push(item); + } + + if (!fit) { + item.position = validItemPosition; + } + return fit; + } + + if (!fit) { + item.position = validItemPosition; + } + return fit; + } +} + +// Packer class +class Packer { + constructor() { + this.bins = []; + this.items = []; + this.unfitItems = []; + this.totalItems = 0; + } + + addBin(bin) { + this.bins.push(bin); + } + + addItem(item) { + this.totalItems = this.items.length + 1; + this.items.push(item); + } + + packToBin(bin, item) { + let fitted = false; + + if (bin.items.length === 0) { + const response = bin.putItem(item, START_POSITION); + if (!response) { + bin.unfittedItems.push(item); + } + return; + } + + for (let axis = 0; axis < 3; axis++) { + const itemsInBin = bin.items; + for (const ib of itemsInBin) { + let pivot = [0, 0, 0]; + const [w, h, d] = ib.getDimension(); + if (axis === Axis.WIDTH) { + pivot = [ib.position[0] + w, ib.position[1], ib.position[2]]; + } else if (axis === Axis.HEIGHT) { + pivot = [ib.position[0], ib.position[1] + h, ib.position[2]]; + } else if (axis === Axis.DEPTH) { + pivot = [ib.position[0], ib.position[1], ib.position[2] + d]; + } + + if (bin.putItem(item, pivot)) { + fitted = true; + break; + } + } + if (fitted) { + break; + } + } + + if (!fitted) { + bin.unfittedItems.push(item); + } + } + + pack(biggerFirst = false, distributeItems = false, numberOfDecimals = DEFAULT_NUMBER_OF_DECIMALS) { + for (const bin of this.bins) { + bin.formatNumbers(numberOfDecimals); + } + + for (const item of this.items) { + item.formatNumbers(numberOfDecimals); + } + + this.bins.sort((a, b) => { + return b.getVolume() - a.getVolume(); + }); + + this.items.sort((a, b) => { + return b.getVolume() - a.getVolume(); + }); + + for (const bin of this.bins) { + for (const item of this.items) { + this.packToBin(bin, item); + } + + if (distributeItems) { + for (const item of bin.items) { + this.items.splice(this.items.indexOf(item), 1); + } + } + } + } +} + +// Constants +const DEFAULT_NUMBER_OF_DECIMALS = 3; +const START_POSITION = [0, 0, 0]; + + +// Function to find a box that can accommodate all items at once +function findBoxForAllItems(items, bins) { + for (const bin of bins) { + let canFitAllItems = true; + + // Check if all items can fit in the current box + for (const item of items) { + let itemFits = false; + + // Try all possible rotations of the item + for (let i = 0; i < RotationType.ALL.length; i++) { + const originalRotationType = item.rotationType; + item.rotationType = RotationType.ALL[i]; + const dimension = item.getDimension(); + + console.log(dimension) + + if ( + bin.width >= dimension[0] && + bin.height >= dimension[1] && + bin.depth >= dimension[2] + ) { + // Check if the item can fit in the box without overlapping other items + let itemFitsInBox = true; + for (const existingItem of bin.items) { + if (intersect(existingItem, item)) { + itemFitsInBox = false; + break; + } + } + + if (itemFitsInBox) { + itemFits = true; + break; // The item fits in this rotation, move to the next item + } + } + item.rotationType = originalRotationType; + } + + if (!itemFits) { + canFitAllItems = false; + break; // The current box cannot accommodate all items + } + } + + if (canFitAllItems) { + return bin; // Return the first box that can fit all items + } + } + + return null; // No box can fit all items +} + +// Example usage: +const items = [ + new Item('Item1', 8, 5, 2, 3), + new Item('Item2', 15, 10, 2, 5), + new Item('Item3', 3, 2, 1, 1), + new Item('Item4', 7, 6, 2, 4), +]; + +const bins = [ + new Bin("Small Box", 30, 30, 30, 1000), + new Bin("Medium Box", 40, 40, 40, 2000), + new Bin("Large Box", 50, 50, 50, 5000), + new Bin("Extra-Large Box", 60, 60, 60, 10000), + new Bin("Book Box", 30, 20, 20, 1300), + new Bin("Wardrobe Box", 60, 50, 120, 4000), + new Bin("Dish-Pack Box", 60, 40, 30, 2000), + new Bin("File Box", 40, 30, 30, 1000), +] +const selectedBox = findBoxForAllItems(items, bins); + +console.log(selectedBox) + +if (selectedBox) { + console.log(`Items can fit in ${selectedBox.name}`); +} else { + console.log("No box can fit all items."); +} diff --git a/Data Doodles/JavaScript/bestFitAlgorithm/package-lock.json b/Data Doodles/JavaScript/bestFitAlgorithm/package-lock.json new file mode 100644 index 0000000..7a59d9e --- /dev/null +++ b/Data Doodles/JavaScript/bestFitAlgorithm/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "bestFitAlgorithm", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "decimal.js": "^10.4.3" + } + }, + "node_modules/decimal.js": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.3.tgz", + "integrity": "sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==" + } + } +} diff --git a/Data Doodles/JavaScript/bestFitAlgorithm/package.json b/Data Doodles/JavaScript/bestFitAlgorithm/package.json new file mode 100644 index 0000000..6bc3c7e --- /dev/null +++ b/Data Doodles/JavaScript/bestFitAlgorithm/package.json @@ -0,0 +1,16 @@ +{ + "name": "nodejs", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start":"node bestFitAlgorithm.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "decimal.js": "^10.4.3" + } +} diff --git a/Data Doodles/JavaScript/diamondPattern.js b/Data Doodles/JavaScript/diamondPattern.js new file mode 100644 index 0000000..5200243 --- /dev/null +++ b/Data Doodles/JavaScript/diamondPattern.js @@ -0,0 +1,51 @@ +const readline = require("readline"); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +rl.question("Enter width of diamond: ", function (input) { + const diamondWidth = parseInt(input); + + // Check input constraints + if (diamondWidth < 1 || diamondWidth > 200) { + console.log("Invalid input. Please enter a value between 1 and 200."); + rl.close(); + return; + } + + // Function to generate spaces + const generateSpaces = (count) => " ".repeat(count); + + // Function to generate hash symbols + const generateHashes = (count) => "# ".repeat(count); + + // Print upper half of diamond + for (let i = 0; i < diamondWidth; i++) { + const spacesCount = diamondWidth - i - 1; + const hashesCount = i + 1; + console.log(generateSpaces(spacesCount) + generateHashes(hashesCount)); + } + + // Print lower half of diamond + for (let i = diamondWidth - 2; i >= 0; i--) { + const spacesCount = diamondWidth - i - 1; + const hashesCount = i + 1; + console.log(generateSpaces(spacesCount) + generateHashes(hashesCount)); + } + + rl.close(); +}); + + +/* +Sample output for width 3: + + # + # # + # # # + # # + # + +*/ \ No newline at end of file diff --git a/Data Doodles/JavaScript/radixsort.js b/Data Doodles/JavaScript/radixsort.js new file mode 100644 index 0000000..bd40af5 --- /dev/null +++ b/Data Doodles/JavaScript/radixsort.js @@ -0,0 +1,40 @@ + +//function to find the no of digits in a number +function digitCount(n){ + if(n===0) return 1 + return Math.floor(Math.log10(Math.abs(n)))+1 +} + +//function to find the largest nunmber digit in the list of numbers +function mostDigits(nums){ + var maxDigits=0 + for(let i=0;i[]) + //lets the get the digits at the k place and sort them into the buckets + for(let i=0;i