Friday, 29 August 2025

Python AI & ML

 Machine Learning in Python
-----------------------------------
-In Machine Learning it is common to work with very large data sets. 
-Machine Learning is for! Analyzing data and predicting the outcome!
-machine learning using Python’s powerful libraries like Scikit-learn, TensorFlow, and Keras

1) Scikit-Learn: A simple and efficient tool for data mining and data analysis.(Package : pip install -U scikit-learn)
2) TensorFlow: An open-source library for numerical computation and large-scale machine learning.
3) Keras: A high-level neural networks API, written in Python and capable of running on top of TensorFlow.
4) PyTorch: An open-source machine learning library based on the Torch library.

Basic Workflow:
---------------
-Here’s a typical workflow for a machine learning project in Python:

1) Data Collection: Gather the data you need for your model.
2) Data Preprocessing: Clean and prepare your data for analysis.
3) Model Selection: Choose the appropriate machine learning model.
4) Training: Train your model using your dataset.
5) Evaluation: Evaluate your model’s performance.
6) Prediction: Use the model to make predictions on new data.

Data Types
-----------
-To analyze data, it is important to know what type of data we are dealing with.
-We can split the data types into three main categories:

1) Numerical
2) Categorical
3) Ordinal

1) Numeical:
 ---------------
 - Numerical data are numbers, and can be split into two numerical categories:

a) Discrete Data : counted data that are limited to integers. Example: The number of cars passing by.
b) Continuous Data : measured data that can be any number. Example: The price of an item, or the size of an item

2) Categorical
------------------
-Categorical data are values that cannot be measured up against each other. Example: a color value, or any yes/no values.

3) Ordinal
-------------
-Ordinal data are like categorical data, but can be measured up against each other. Example: school grades where A is better than B and so on.
============================================
In Machine Learning (and in mathematics) there are often three values that interests us:

1) Mean - The average value
2) Median - The mid point value
3) Mode - The most common value
4) std - Standard deviation
5) percentile - Percentiles are used in statistics to give you a number that describes the value that a given percent of the values are lower than

Ex1:
---
import numpy
from scipy import stats

speed = [45,55,67,89,90]
x1 = numpy.mean(speed)
print(x1)

x2 = numpy.median(speed)
print(x2)

x3 = stats.mode(speed)
print(x3)

x4 = numpy.std(speed)
print(x4)
---------------------------
Ex2:
---
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
x = numpy.percentile(ages, 75) #What is the 75. percentile? The answer is 43, meaning that 75% of the people are 43 or younger.
print(x)
---------------------------
Data Distribution
-----------------
1. uniform - random array, of a given size, and between two given values.
2. normal -  create an array where the values are concentrated around a given value.

Ex1: uniform
---
import numpy
x = numpy.random.uniform(0.0, 5.0, 250)
print(x)

Ex2: uniform
---
import numpy
import matplotlib.pyplot as plt
x = numpy.random.uniform(0.0, 5.0, 250)
plt.hist(x, 5)
plt.show()

Ex3: uniform
---
import numpy
import matplotlib.pyplot as plt
x = numpy.random.uniform(0.0, 5.0, 100000)
plt.hist(x, 100)
plt.show()
---------------------------
Ex1: normal
---
import numpy
import matplotlib.pyplot as plt
x = numpy.random.normal(5.0, 1.0, 100000)
print(x)

Ex2: normal
---
import numpy
import matplotlib.pyplot as plt
x = numpy.random.normal(5.0, 1.0, 100000)
plt.hist(x, 100)
plt.show()

Ex3: normal using Scatterplot
---
import numpy
import matplotlib.pyplot as plt
x = numpy.random.normal(5.0, 1.0, 1000)
y = numpy.random.normal(10.0, 2.0, 1000)
plt.scatter(x, y)
plt.show()
==============================
1. Scikit-Learn (Sklearn)
 ------------------------
-Scikit-learn (Sklearn) is the most useful and robust library for machine learning in Python.
-It provides a selection of efficient tools for machine learning and statistical modeling including 
1. Classification(logistic regression, decision trees, random forests, support vector machines (SVMs) and gradient boosting),
2. Regression(predicting continuous outputs)
3. Clustering(grouping data points into similar clusters) and 
4. Dimensionality reduction(reducing the number of features in your data) via a consistence interface in Python.

a) classification(predicting categorical labels) - Logistic Regression Algorithm 
   --------------
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

# Load Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Splitting the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Standardizing features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Training the logistic regression model
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)

# Making predictions on the testing set
y_pred = log_reg.predict(X_test)

# Evaluating the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
--------------------------------
Ex: Classification - KNN Classifier Algorithm

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load the Iris dataset
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

# Initialize the KNN classifier
knn = KNeighborsClassifier(n_neighbors=3)

# Train the classifier
knn.fit(X_train, y_train)

# Make predictions on the test data
predictions = knn.predict(X_test)

# Evaluate the model
accuracy = knn.score(X_test, y_test)
print("Accuracy:", accuracy)
-------------------------------
Ex: Linear Regression Algorithm
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Load the California Housing dataset
housing = fetch_california_housing()
X_train, X_test, y_train, y_test = train_test_split(housing.data, housing.target, test_size=0.2, random_state=42)

# Initialize the Linear Regression model
lr = LinearRegression()

# Train the model
lr.fit(X_train, y_train)

# Make predictions on the test data
predictions = lr.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, predictions)
print("Mean Squared Error:", mse)
---------------------------------------
Ex: Clustering - KMeans Algorithm

from sklearn.datasets import load_iris
from sklearn.cluster import KMeans

# Load the Iris dataset
iris = load_iris()

# Initialize the KMeans clustering model
kmeans = KMeans(n_clusters=3)

# Fit the model to the data
kmeans.fit(iris.data)

# Get the cluster labels
cluster_labels = kmeans.labels_

print("Cluster Labels:", cluster_labels)
--------------------------------------
Ex: Dimensionality Reduction
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA

# Load the digits dataset
digits = load_digits()

# Initialize PCA for dimensionality reduction
pca = PCA(n_components=2)

# Apply PCA to the data
reduced_data = pca.fit_transform(digits.data)

print("Original data shape:", digits.data.shape)
print("Reduced data shape:", reduced_data.shape)
---------------------------------------
***********************************************
TensorFlow python
----------------
- TensorFlow is a Google product, which is one of the most famous deep learning tools widely used in the research area of machine learning and deep neural network. 
- TensorFlow is basically a software library for numerical computation using data flow graphs where:

1) nodes in the graph represent mathematical operations.
2) edges in the graph represent the multidimensional data arrays (called tensors) communicated between them.

Variables:
---------
-TensorFlow has Variable nodes too which can hold variable data. 
-They are mainly used to hold and update parameters of a training model. 
-Variables are in-memory buffers containing tensors. 

ex1:
---
import tensorflow as tf
a = tf.constant(10)
b = tf.constant(32)
print(a+b)
-------------------
===============================================
speech recognition using AI with Python
---------------------------------------
#ex1 : Analyze the audio file
import numpy as np
from scipy.io import wavfile

frequency_sampling, audio_signal = wavfile.read("1.wav")
print('\nSignal shape:', audio_signal.shape)
print('Signal Datatype:', audio_signal.dtype)
print('Signal duration:', round(audio_signal.shape[0] / float(frequency_sampling), 2), 'seconds')
---------------
#ex2: 
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile

frequency_sampling, audio_signal = wavfile.read("1.wav")
audio_signal = audio_signal / np.power(2, 15)
signal = audio_signal [:100]
time_axis = 1000 * np.arange(0, len(signal), 1) / float(frequency_sampling)
plt.plot(time_axis, signal, color='blue')
plt.xlabel('Time (milliseconds)')
plt.ylabel('Amplitude')
plt.title('Input audio signal')
plt.show()
------------------
NLTK (Natural Language Toolkit Package)
---------------------------------------
-Natural Language Toolkit
-It is used for classification, tokenization, stemming, tagging, parsing, and semantic reasoning

#ex1: download
import nltk

# Download essential datasets and models
nltk.download('punkt')  # Tokenizers for sentence and word tokenization
nltk.download('stopwords')  # List of common stop words
nltk.download('wordnet')  # WordNet lexical database for lemmatization
nltk.download('averaged_perceptron_tagger_eng')  # Part-of-speech tagger
nltk.download('maxent_ne_chunker_tab')  # Named Entity Recognition model
nltk.download('words')  # Word corpus for NER
nltk.download('punkt_tab')
------------------------------
Tokenization:
-------------
-Tokenization is one of the common preprocessing tasks. 
-It involves splitting text into smaller units—tokens. 
-These tokens can be words, sentences, or even sub-word units, depending on the task.

#ex2 :  Word Tokenize
from nltk.tokenize import word_tokenize
sentence = "NLTK makes natural language processing easy."
tokens = word_tokenize(sentence)
print(tokens)
-------------------------
#ex3 :  Send Tokensize
import string
from nltk.tokenize import word_tokenize, sent_tokenize
text = "Natural Language Processing (NLP) is cool! Let's explore it."

# Sentence Tokenization - splits the text into sentences
sentences = sent_tokenize(text)
print("Sentences:", sentences)
---------------------
#Stemming and lemmatization are techniques used to reduce words to their base or root form.
#ex4
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.tokenize import word_tokenize

word = "running"
stemmer = PorterStemmer()
stemmed_word = stemmer.stem(word)

lemmatizer = WordNetLemmatizer()
lemmatized_word = lemmatizer.lemmatize(word)

print("Stemmed Word:", stemmed_word)
print("Lemmatized Word:", lemmatized_word)
----------------------------------------
#Chunking is the process of grouping words together based on their part-of-speech tags.
#Parsing is the process of analyzing the grammatical structure of a sentence.
#ex5
import nltk
sentence=[("a","DT"),("clever","JJ"),("fox","NN"),("was","VBP"),
          ("jumping","VBP"),("over","IN"),("the","DT"),("wall","NN")]
grammar = "NP:{<DT>?<JJ>*<NN>}"
parser_chunking = nltk.RegexpParser(grammar)
parser_chunking.parse(sentence)
Output_chunk = parser_chunking.parse(sentence)
Output_chunk.draw()
------------------------
#chatbot
import nltk
import re

def chatbot():
    while True:
        user_input = input("User: ")
        user_input = user_input.lower()
        user_input = re.sub(r'[^\w\s]', '', user_input)
        tokens = nltk.word_tokenize(user_input)

        if 'hello' in tokens:
            print("Chatbot: Hi there!")
        elif 'bye' in tokens:
            print("Chatbot: Goodbye!")
            break
        else:
            print("Chatbot: Sorry, I didn't understand.")

chatbot()



Python chart

 #ex1
import matplotlib.pyplot as plt
names = []
marks = []

f = open('test1.txt','r')
for row in f:
    row = row.split(' ')
    names.append(row[0])
    marks.append(int(row[1]))

plt.bar(names, marks, color = 'g', label = 'File Data')

plt.xlabel('Student Names', fontsize = 12)
plt.ylabel('Marks', fontsize = 12)

plt.title('Students Marks', fontsize = 20)
plt.legend()
plt.show()
------------------
#ex2
import matplotlib.pyplot as plt

names = []
work = []

for line in open('test1.txt', 'r'):
    Data = [i for i in line.split()]
    names.append(Data[0])
    New_Data= [ j for j in Data[1].split('%')]
    
    work.append(New_Data[0])
colors = ['yellow', 'b', 'green', 'cyan','red'] 
  
# plotting pie chart 
plt.pie(work, labels = names, colors = colors, startangle = 90,
        shadow = True, radius = 1.2, autopct = '%1.1f%%') 
plt.show()
------------------------
#ex3
import matplotlib.pyplot as plt
x = []
y = []
for line in open('test1.txt', 'r'):
    lines = [i for i in line.split()]
    x.append(lines[0])
    y.append(int(lines[1]))
    
plt.title("Students Marks")
plt.xlabel('Name')
plt.ylabel('Marks')
plt.yticks(y)
plt.plot(x, y, marker = 'o', c = 'g')

plt.show()
--------------------------
#ex4
import pandas as pd
import matplotlib.pyplot as plt

# Read data from a CSV file
df = pd.read_csv('Book1.csv')

# Create a bar chart
df.plot(kind='bar', x='Name', y='Marks')
plt.xlabel('Name')
plt.ylabel('Marks')
plt.title('Bar Chart Example')
plt.show()

# Create a scatter plot
df.plot(kind='scatter', x='x_values', y='y_values')
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot Example')
plt.show()
---------------------------------------------------
import mysql.connector
import pandas as pd
import matplotlib.pyplot as plt

# Replace with your MySQL credentials
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="jahab"
)

# Read data into DataFrame
query = "SELECT name, marks FROM student"
df = pd.read_sql(query, mydb)

# Create a bar chart
plt.figure(figsize=(10, 6))
plt.bar(df['name'], df['marks'])
plt.xlabel('Name')
plt.ylabel('Marks')
plt.title('Student Marks')
plt.show()

# Close the database connection
mydb.close()


Python Pandas - 3

 A. Math Function
B. Date Function
C. Text Function
D. Aggregate Function
E. Group by, Having, Order by

A. Math Function
-----------------------
1.power()
2.round()
3.mod()
------------
Ex1: Using power() function
---
import pandas as pd
import numpy as np

s1 ={'a':10,'b':5,'c':3}
Data ={'Score':s1}

df= pd.DataFrame(Data) 
df['Score']=np.power((df['Score']),3)
print(df)

output:
--------
     Score
a   1000
b    125
c     27
===================================  
Ex2: Using round() function
---
import pandas as pd
import numpy as np

s1 ={'a':1.5,'b':2.6,'c':3.8}
Data ={'Score':s1}

df= pd.DataFrame(Data) 
df['Score']=np.round((df['Score']))
print(df)

output:
--------
  Score
a    2.0
b    3.0
c    4.0
===================================  
Ex3: Using mod() function
---
import pandas as pd
import numpy as np

s1 ={'a':10,'b':25,'c':30}
Data ={'Score':s1}

df= pd.DataFrame(Data) 
df['Score']=np.mod((df['Score']),3)
print(df)

output:
--------
    Score
a      1
b      1
c      0
===================================

B. Date Function:
---------------------
1. now() - Display current Date
2. date_range()  - display date range
3. day_name()  - day_name() function is used to get the day names of the Date
4. month_name() - get the month names of the Date
5. month - get the months of the Date
6. day - get the day of the Date
7. year - get the year of the Date
------------------------------------------------------------------
Ex1 : Display current Date
-----
import pandas as pd
date = pd.Timestamp(2020)
print(date.now())

Output:
---------
2020-08-18 17:22:17.661221
=====================================
Ex2:   Display Date Range
-----
import pandas as pd
date = pd.date_range(start='2020-01-01', freq='D', periods=5)
print(date)

output:
--------
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03',
                        '2020-01-04', '2020-01-05'],
===================================
Ex3: using day_name() function
-----
import pandas as pd
date = pd.date_range(start='2020-08-18', freq='D', periods=5)
print(date)
print(date.day_name())

output:
---------
DatetimeIndex(['2020-08-18', '2020-08-19', '2020-08-20', '2020-08-21',
               '2020-08-22'],dtype='datetime64[ns]', freq='D')
Index(['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], dtype='object')
====================================
Ex4: using month_name() function
-----
import pandas as pd
date = pd.date_range(start='2020-08-18', freq='M', periods=5)
print(date)
print(date.month_name())

output:
---------
DatetimeIndex(['2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30',
               '2020-12-31'],dtype='datetime64[ns]', freq='M')
Index(['August', 'September', 'October', 'November', 'December'], dtype='object')====================================
Ex5: using month function
-----
import pandas as pd
date = pd.date_range(start='2020-08-18', freq='M', periods=5)
print(date)
print(date.month)

output:
---------
DatetimeIndex(['2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30',
               '2020-12-31'], dtype='datetime64[ns]', freq='M')
Int64Index([8, 9, 10, 11, 12], dtype='int64')====================================
Ex6: using day function
-----
import pandas as pd
date = pd.date_range(start='2020-08-18', freq='M', periods=5)
print(date)
print(date.day)

output:
---------
DatetimeIndex(['2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30',
               '2020-12-31'],dtype='datetime64[ns]', freq='M')
Int64Index([31, 30, 31, 30, 31], dtype='int64')====================================
Ex7: using year function
-----
import pandas as pd
date = pd.date_range(start='2020-08-18', freq='Y', periods=5)
print(date)
print(date.year)

output:
---------
DatetimeIndex(['2020-12-31', '2021-12-31', '2022-12-31', '2023-12-31',
               '2024-12-31'],dtype='datetime64[ns]', freq='A-DEC')
Int64Index([2020, 2021, 2022, 2023, 2024], dtype='int64')====================================

C. Text Function:
  --------------------
1. upper() - uppercase
2. lower() - lowercase
3. len()     - find length of the string
4. lstrip()  - remove left space(LTRIM())
5. rstrip()  - remove right space(RTRIM())
6. strip()  - remove both left and right space(TRIM())
7. slice()  - substring(start to end) - MID(),SUBSTR(),SUBSTRING()
8. left()    - extract specific characters within a string(left to right)
9. right()  - extract specific characters within a string(right to left)
------------------------------
Ex1: using UPPER function
-----
import pandas as pd
s1 ={0:'jahab',1:'sudhir',2:'rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.upper()
print(df)

output:
--------
      Name
0   JAHAB
1   SUDHIR
2   RINO
================================
Ex2: using LOWER function
-----
import pandas as pd
s1 ={0:'JAHAB',1:'SUDHIR',2:'RINO'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.lower()
print(df)

output:
--------
      Name
0    jahab
1    sudhir
2    rino
================================
Ex3: using len() function
-----
import pandas as pd
s1 ={0:'jahab',1:'sudhir',2:'rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.len()
print(df)

output:
--------
      Name
0    5
1    6
2    4
================================
Ex4: using lstrip() function - Remove left space
-----
import pandas as pd
s1 ={0:'  jahab',1:'sudhir  ',2:'   rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.lstrip()
print(df)

output:
--------
          Name
0        jahab
1    sudhir
2        rino
================================
Ex5: using rstrip() function - Remove right space
-----
import pandas as pd
s1 ={0:'  jahab',1:'sudhir  ',2:'   rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.rstrip()
print(df)

output:
--------
          Name
0        jahab
1       sudhir
2          rino
================================
Ex6: using strip() function - Remove both right and left space
-----
import pandas as pd
s1 ={0:'  jahab',1:'sudhir  ',2:'   rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.strip()
print(df)

output:
--------
          Name
0        jahab
1        sudhir
2        rino
================================
Ex7: using slice() function - substring of the given string
-----
import pandas as pd
s1 ={0:'jahab',1:'sudhir',2:'rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str.slice(1,3)
print(df)

output:
--------
          Name
0        ah
1        ud
2        in
================================
Ex8: using left() function
-----
import pandas as pd

s1 ={0:'jahab',1:'sudhir',2:'rino'}
Data ={'Name':s1}

df= pd.DataFrame(Data) 
df['Name']=df['Name'].str[:3]
print(df)

output:
--------
          Name
0        jah
1        jud
2        rin
================================
Ex9: using right() function
-----
import pandas as pd
s1 ={0:'jahab',1:'sudhir',2:'rino'}
Data ={'Name':s1}
df= pd.DataFrame(Data) 
df['Name']=df['Name'].str[3:]
print(df)

output:
--------
          Name
0        hab
1        hir
2        ino
================================
D. Aggregate Function
-----------------------------
1. sum() - sum of the values
2. max() - max of the values
3. min() - min of the values
4. count() - count values
5. count(*) - count all values
------------------------------
Ex1: using sum() function
-----
import pandas as pd

s1 ={'a':10,'b':5,'c':3}
Data ={'Score':s1}
df= pd.DataFrame(Data) 
print(df.aggregate(['sum']))

Output:
---------
     Score
sum  18
=========================
Ex2: using max() function
-----
import pandas as pd

s1 ={'a':10,'b':5,'c':3}
Data ={'Score':s1}
df= pd.DataFrame(Data) 
print(df.aggregate(['max']))

Output:
---------
     Score
max  10
=========================
Ex3: using min() function
-----
import pandas as pd

s1 ={'a':10,'b':5,'c':3}
Data ={'Score':s1}
df= pd.DataFrame(Data) 
print(df.aggregate(['min']))

Output:
---------
     Score
min  3
=========================
Ex4: using count() function
-----
import pandas as pd

s1 ={'a':10,'b':5,'c':3}
Data ={'Score':s1}
df= pd.DataFrame(Data) 
print(df.aggregate(['count']))

Output:
---------
     Score
count  3
===============================
E. Group by Function: 
    -------------------------
   * Group by function is used to split the data into groups based on some criteria
   step1: Splitting the Object
   step2: Applying a function
   step3: Combining the results

Ex1:
    import pandas as pd
    s1={0:'Riders', 1:'Riders', 2:'Devils', 3:'Devils', 4:'Kings'}
    s2={0:1, 1:2,  2:2, 3:3, 4:3}
    s3={0:2015, 1:2016, 2:2017, 3:2018, 4:2019}
    s4={0:876, 1:789, 2:863, 3:673, 4:741}
    
    Data={'Team':s1, 'Place':s2, 'Year':s3, 'Points':s4}
    df = pd.DataFrame(Data)
    print(df)
    print(df.groupby(['Team']).groups)

Output:
----------
Team  Place  Year  Points
0  Riders      1  2015     876
1  Riders      2  2016     789
2  Devils      2  2017     863
3  Devils      3  2018     673
4   Kings      3  2019     741
{'Devils': [2, 3], 'Kings': [4], 'Riders': [0, 1]}
======================================
Ex2:
import pandas as pd
s1={0:'Riders',1:'Riders',2:'Devils',3:'Devils',4:'Kings'}
s2={0:1,1:2,2:2,3:3,4:3}
s3={0:2015,1:2016,2:2017,3:2018,4:2019}
s4={0:876,1:789,2:863,3:673,4:741}
Data={'Team':s1, 'Place':s2, 'Year':s3, 'Points':s4}
df = pd.DataFrame(Data)
print(df)
print(df.groupby('Team').groups)
print(df.groupby('Team').filter(lambda x: len(x) >= 3))

output:
---------
Team  Place  Year  Points
0  Riders      1  2015     876
1  Riders      2  2016     789
2  Devils      2  2017     863
3  Devils      3  2018     673
4   Kings      3  2019     741
{'Devils': [2, 3], 'Kings': [4], 'Riders': [0, 1]}
Empty DataFrame
Columns: [Team, Place, Year, Points]
Index: []
> Team  Place  Year  Points
0  Riders      1  2015     876
1  Riders      2  2016     789
2  Devils      2  2017     863
3  Devils      3  2018     673
4   Kings      3  2019     741
{'Devils': [2, 3], 'Kings': [4], 'Riders': [0, 1]}
Empty DataFrame
Columns: [Team, Place, Year, Points]

Python Pandas - 2

 DataFrame
---------
Functions:
--------
1. head()   - select row(top based)
2. tail()   - Select row(bottom based)
3. loc()    - select row(label based)
4. iloc()   - select row (index based)
5. ix()     - select row both label & index based
6. sort_values() - sort values
7. drop()   - delete both row and column
8. del()    - delete column
9. pop()    - delete column
10. rename() - rename column name
11. Boolean Index
12. merge()   - merge two dataframes based on left,right,outer,inner
13. concate() - combine dataframes
================================================
Ex1: (Creating a DataFrame using Dictionary)
----
import pandas as pd

s1 ={0:100,1:101,2:102}
s2 ={0:'jahab',1:'sudhir',2:'rino'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}
s= pd.DataFrame(Data) 
print(s)

output:
------
    Roll   Name     City
0   100    jahab    Salem
1   101    sudhir   coimbatore
2   102    rino     chennai
=======================================================
Ex3: (using head function)
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.head(2))

output:
------
    Roll    Name    City
0   100     jahab   Salem
1   101     sudhir  coimbatore
=======================================================
Ex4: (using tail function)
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.tail(2))

output:
------
    Roll    Name    City
2   102     rino    chennai
3   103     Jasmine chennai
=======================================================
Ex5: (Selecting Data using loc function) - Label based
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.loc[1])      

output:
------
 Roll           101
 Name        sudhir
 City    coimbatore
=======================================================
Ex6: (Selecting Data using loc function) - Label based(select Column)
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.loc[:,'Roll'])      

output:
------
0 100
1 101
2 102
3 103
===============================================
Ex7: (Selecting Data using iloc function) - Integer based
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.iloc[2])      

output:
------
 Roll        102
 Name       rino
 City    chennai
=========================================================
Ex8: (Selecting Data using sorting_values() function)
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.sort_values(by='Name'))

output:
------
    Roll    Name    City
0   100     jahab   Salem
3   103     Jasmine chennai
2   102     rino    chennai
1   101     sudhir  coimbatore
===========================================================
Ex9: (Deleting Data using drop() function) - delete row
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.drop(0))

output:
------
    Roll    Name    City
1   101     sudhir  coimbatore
2   102     rino    chennai
3   103     Jasmine chennai
=================================================================
Ex10: (Deleting Data using drop() function) - delete column
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
print(s.drop('Name',axis=1))

output:
------
    Roll  City
0   100   Salem
1   101   coimbatore
2   102   chennai
3   103   chennai
====================================================================
Ex11: (Deleting Data using del() function) - delete column
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
del s['Name']
print(s)

output:
------
    Roll  City
0   100   Salem
1   101   coimbatore
2   102   chennai
3   103   chennai
==============================================================
Ex12: (Deleting Data using pop() function) - delete column
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
s.pop('Name')
print(s)

output:
------
    Roll  City
0   100   Salem
1   101   coimbatore
2   102   chennai
3   103   chennai
==============================================================
Ex13: (Rename column using rename() function)
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
s=s.rename(columns={'Name':'First'})
print(s)

output:
------
    Roll    First    City
0   100     jahab   Salem
1   101     sudhir  coimbatore
2   102     rino    chennai
3   103     Jasmine chennai
==============================================================
Ex14: (Boolean index)
---
import pandas as pd

s1 ={0:100,1:101,2:102,3:103}
s2 ={0:'jahab',1:'sudhir',2:'rino', 3:'jasmine'}
s3 ={0:'Salem',1:'coimbatore',2:'chennai', 3:'chennai'}

Data ={'Roll':s1, 'Name':s2, 'City':s3}

s= pd.DataFrame(Data)
s= pd.DataFrame(Data,index=[True,False,True,True])
print(s)

output:
------
        Roll  Name    City
True    101   sudhir  coimbatore
False   100   jahab   Salem
True    101   sudhir  coimbatore
True    101   sudhir  coimbatore
=========================================================
Ex15: (merge() function)
----
import pandas as pd
s1 ={0:100,1:101}
s2 ={0:'jahab',1:'sudhir'}
s3 ={0:'Salem',1:'coimbatore'}
Data ={'Roll':s1, 'Name':s2, 'City':s3}
s= pd.DataFrame(Data)

s4 ={0:100,1:101}
s5 ={0:'Jasmine',1:'Rino'}
s6 ={0:'Salem',1:'coimbatore'}
Data1 ={'Roll':s4, 'Name':s5, 'City':s6}
s1= pd.DataFrame(Data1)
print(pd.merge(s,s1,on='Roll'))

output:
------
    Roll  Name_x      City_x      Name_y   City_y
0   100   jahab       Salem       Jasmine  Salem
1   101   sudhir      coimbatore  Rino     coimbatore
=======================================================================
Ex16: (using Concat function)
----
import pandas as pd

s1 ={0:100,1:101}
s2 ={0:'jahab',1:'sudhir'}
s3 ={0:'Salem',1:'coimbatore'}
Data ={'Roll':s1, 'Name':s2, 'City':s3}
s= pd.DataFrame(Data)

s4 ={0:100,1:101}
s5 ={0:'Jasmine',1:'Rino'}
s6 ={0:'Salem',1:'coimbatore'}
Data1 ={'Roll':s4, 'Name':s5, 'City':s6}
s1= pd.DataFrame(Data1)
print(pd.concat([s,s1]))

output:
------
    Roll   Name    City
0   100    jahab   Salem
1   101    sudhir  coimbatore
0   100    Jasmine Salem
1   101    Rino    coimbatore   
===================================================================
      (Binary Operator Functions)
      -----------------------------
-Combine two values to produce a new value.
1. add() - Adding of two DataFrames
2. sub() - subtracting DataFrame from another DataFrame
3. mul() - multiplying DataFrame with another DataFrame
4. div() - divison DataFrame from another DataFrame
5. mod() - modulo divison DataFrame from another DataFrame(get remainder value)

Ex17: Adding of two DataFrames using add()
====
import pandas as pd

s1 =[[2, 3, 4]] 
Data1 =s1
res1= pd.DataFrame(Data1) 

s2 =[[1, 3, 5]] 
Data2=s2
res2=pd.DataFrame(Data2)

res=res1.add(res2)

print('Output')
print(res)

output:
------
   0  1  2
0  3  6  9
========================================
Ex18: subtracting DataFrame from another DataFrame using sub()
====
import pandas as pd

s1 =[[2, 3, 4]] 
Data1 =s1
res1= pd.DataFrame(Data1) 

s2 =[[1, 3, 5]] 
Data2=s2
res2=pd.DataFrame(Data2)

res=res1.sub(res2)

print('Output')
print(res)

output:
------
   0  1  2
0  1  0 -1
============================================
Ex19: multiplying DataFrame with another DataFrame using mul()
====
import pandas as pd

s1 =[[2, 3, 4]] 
Data1 =s1
res1= pd.DataFrame(Data1) 

s2 =[[1, 3, 5]] 
Data2=s2
res2=pd.DataFrame(Data2)

res=res1.mul(res2)

print('Output')
print(res)

output:
------
   0  1   2
0  2  9  20
==============================================
Ex20: divison DataFrame from another DataFrame using div()
====
import pandas as pd

s1 =[[2, 3, 4]] 
Data1 =s1
res1= pd.DataFrame(Data1) 

s2 =[[1, 3, 5]] 
Data2=s2
res2=pd.DataFrame(Data2)

res=res1.div(res2)

print('Output')
print(res)

output:
------
    0    1    2
0  2.0  1.0  0.8
==========================================
Ex21: modulo divison DataFrame from another DataFrame using mod()
====
import pandas as pd

s1 =[[2, 3, 4]] 
Data1 =s1
res1= pd.DataFrame(Data1) 

s2 =[[1, 3, 5]] 
Data2=s2
res2=pd.DataFrame(Data2)

res=res1.mod(res2)

print('Output')
print(res)

output:
------
   0  1  2
0  0  0  4
===========================================

Python Pandas - 1

 Pandas
------
-Most Popular Python library
-It is used for Data Analysis

Two Types of Data Analysis
-------------------------
1. Series - 1D (One Dimensional array or single dimensional array)
2. DataFrames - 2D(table format)

1. Series:
----------
-Series is a one Dimensional Array(1-D)
-Store any data type(int,float,char)
-data stored on seq order

Creating Series:
----------------
import pandas as pd
a = pd.Series(Data,Index)
print(a)

Note:
-----
 data - int,float,char
 Index - 0,1....9

Ex1:
---
import pandas as pd

Data =[1, 3, 4, 5, 6, 2, 9]  #Numeric data 
s = pd.Series(Data) 

print(s)

output:
------
0    1
1    3
2    4
3    5
4    6
5    2
6    9
=============================================
Ex2:
---

import pandas as pd

Data =[10.5, 30.6, 40.5, 5.5, 6.76, 2.3, 0.9] 
s = pd.Series(Data) 

print(s)

output:
------
0    10.50
1    30.60
2    40.50
3     5.50
4     6.76
5     2.30
6     0.90
=============================================
Ex3:
---
import pandas as pd

Data =[a,b,c,r,f] 
s = pd.Series(Data) 
print(s)

output:
------
 Error
=============================================
Ex4:
---
import pandas as pd

Data =['a','b','c','r','f'] 
s = pd.Series(Data) 

print(s)

output:
------
0    a
1    b
2    c
3    r
4    f
==========================================
Ex5:
---
import pandas as pd

Data =[1, 3, 4, 5, 6, 2, 9]
Index =['a', 'b', 'c', 'd', 'e', 'f', 'g']
s = pd.Series(Data,Index) 
print(s)

output:
------
a    1
b    3
c    4
d    5
e    6
f    2
g    9
=========================================
Ex6:
---
import pandas as pd

Data =[1, 3,10.5,3.14,'a','b','c']
s = pd.Series(Data)
print(s)

output:
------
0       1
1       3
2    10.5
3    3.14
4       a
5       b
6       c
===========================================
Ex7: (Data contains Dictionary(Numeric values))
---
import pandas as pd

Data ={'a':10, 'b':20, 'c':30, 'd':80, 'e':70}  
s = pd.Series(Data)  
print(s)

output:
------
a    10
b    20
c    30
d    80
e    70
================================================
Ex8: (Data contains Dictionary(Numeric values))
---
import pandas as pd

Data ={1:10, 2:20, 3:30, 4:80, 5:70}  
s = pd.Series(Data)  
print(s)

output:
------
1    10
2    20
3    30
4    80
5    70
================================================
Ex9: (Data contains Dictionary(floating point values))
---
import pandas as pd

Data ={'a':10.5, 'b':20.5, 'c':3.14,'e':12.5}
s = pd.Series(Data)  
print(s)

output:
------
a    10.50
b    20.50
c     3.14
e    12.50
==============================================
Ex10: (Data contains Dictionary(string values))
---
import pandas as pd

Data ={'a':'jahab','b':'sudhir','c':'coimbatore'}
s = pd.Series(Data)  
print(s)

output:
------
a   jahab
b   sudhir
c   coimbatore
============================================
Ex11: (Data contains Ndarray)
---
import pandas as pd

Data =[[2, 3, 4], [5, 6, 7]]  # Defining 2darray 
s = pd.Series(Data)
print(s)

output:
------
0    [2, 3, 4]
1    [5, 6, 7]
============================================


***********************************************************
2. DataFrames:
-------------
-DataFrames is a two-dimensional Array(2-D)
-Its consists of rows and columns
 
Creating DataFrames:
----------------
import pandas as pd
s = pd.DataFrames(Data)

Note:
-----
 data - one or more Series,one or more dictionaries,2D-numpy Ndarray

Ex1: (Data is Series)
---
import pandas as pd 

s1 = pd.Series([100, 101, 102])            
s2 = pd.Series(['jahab', 'sudir', 'rino'])

Data ={'Roll':s1, 'Name':s2}
s= pd.DataFrame(Data) 
print(s)

output:
------
    Roll   Name
0   100    jahab
1   101    sudir
2   102    rino
=====================================================
Ex2: (Data is Dictionaries)
---
import pandas as pd

s1={'a':100,'b':101,'c':102}
s2 ={'a':'jahab','b':'sudhir','c':'rino'}

Data ={'Roll':s1, 'Name':s2}
s= pd.DataFrame(Data) 
print(s)

output:
------
    Roll   Name
a   100    jahab
b   101    sudir
c   102    rino
======================================================
Ex3: (Data is 2D-numpy Ndarray)
---
import pandas as pd

s1 =[[2, 3, 4], [5, 6, 7]] 
s2 =[[1, 3, 5], [6, 9, 0]] 

Data ={'First':s1, 'Second':s2}
s= pd.DataFrame(Data) 
print(s)

output:
------
     First     Second
0  [2, 3, 4]  [1, 3, 5]
1  [5, 6, 7]  [6, 9, 0]
=================================================
                   
                  Practical (DataFrame)
                    =============
A) Write a python program to Display 5 roll,name,city... using DataFrame with series

output:
------
    Roll   Name     city  
0   101    jahab    Coimbatore
1   102    sudir    Coimbatore
2   103    rino     Salem
3   104    aswin    Erode
4   105    jasmine  chennai

B) Write a python program to Display 5 roll,name,city... using DataFrame with Dictionaries

output:
------
    Roll   Name     city  
a   101    jahab    Coimbatore
b   102    sudir    Coimbatore
c   103    rino     Salem
d   104    aswin    Erode
e   105    jasmine  chennai

C) Write a python program to following output using DataFrame with Ndarray

output:
------
          A                    B                    C
0  [10, 3, 5, 8, 10]  [10, 13, 50, 8, 0]  [20, 63, 80, 5, 9]
1  [5,  6, 7, 6, 13]  [68, 90, 20, 5, 2]  [78, 80, 50, 1, 8]  
2  [8,  0, 3, 2, 16]  [90, 10, 26, 2, 9]  [34, 32, 70, 3, 7]  
3  [2,  3, 5, 8, 99]  [23, 45, 67, 3, 6]  [56, 79, 12, 7, 9]  
------------------------------------------------------------------
             (Series)
             -------
D) Define series, creating series
E) Integer Value series,Floating value series,String value Series
F) Dictionary(Integer,float,string) value series
G) Ndarray value series
-------------------------------------------------------
H) Define Python pandas, use of Python Pandas
I) Difference between Series and DataFrame
------------------------------------------------------

CSS

 CSS 
-----
->CSS stands for Cascading Style Sheets
->CSS describes how HTML elements are to be displayed on screen, paper, or in other media
->CSS saves a lot of work. It can control the layout of multiple web pages all at once
Why CSS
-----------
->CSS is used to define styles for your web pages
->including the design, layout and variations in display for 
->different devices and screen sizes.

CSS Syntax
---------------
-A CSS rule-set consists of a selector and a declaration block:
 
  Syntax:   Selector{declaration;.....;...}
  Ex:
           h1{color : blue; font-size:12px;}
Three ways to insert CSS
-------------------------------
1.  External style sheet - link in the head section
2.  Internal style sheet - in the head section
3.  Inline style - inside as html elements

1. External Style sheet
------------------------------
Prg1 : mystyle.html
-----
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
  <h1>Welcome to CSS</h1>
</body>
</html> 
------------------------------------
Prg2 : mystyle.css
-----
body {
    background-color: lightblue;
}

h1 {
    color: navy;
    margin-left: 20px;
}
===============================================
2. Internal Style Sheet
-----------------------------
Prg1 : mystyle.html
-----
<html>
<head>
<style>
body {
    background-color: lightblue;
}

h1 {
    color: maroon;
    margin-left: 40px;
} 
</style>
</head>
<body>
  <h1>Welcome to CSS</h1>
</body>
</html> 
====================================================
3. Inline Style Sheet
-------------------------
<html>
<head></head>
<body>
  <h1  style="color:blue;margin-left:30px;">Welcome to CSS</h1>
</body>
</html> 

-------------------------------------------------------------------- 

Saturday, 24 May 2025

chat bot using python

 #chatbot

import nltk

import re


def chatbot():

    while True:

        user_input = input("User: ")

        user_input = user_input.lower()

        user_input = re.sub(r'[^\w\s]', '', user_input)

        tokens = nltk.word_tokenize(user_input)


        if 'hello' in tokens:

            print("Chatbot: Hi there!")

        elif 'bye' in tokens:

            print("Chatbot: Goodbye!")

            break

        else:

            print("Chatbot: Sorry, I didn't understand.")


chatbot()

ReactJS

ReactJS(React): ------- *ReactJS (React) is a popular JavaScript library for building user interfaces,  *Especially for single-page web appl...