Bijan Krishna Paul

VILL- DEBANDI, AMTA, HOWRAH PIN-711410 Phone-9836357266

Total Pageviews

Monday, August 17, 2026

HOW TO TRAIN A MODEL STEP BY STEP IN PYTHON

 

HOW TO TRAIN A MODEL STEP BY STEP IN PYTHON:

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error, r2_score


data = {

'Hours': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

'Scores': [10, 20, 30, 40, 50, 60, 70, 80, 85, 95]

}


df = pd.DataFrame(data)

print(df.head())


   Hours  Scores
0      1      10
1      2      20
2      3      30
3      4      40
4      5      50




plt.scatter(df['Hours'], df['Scores']) plt.xlabel('Hours Studied') plt.ylabel('Scores') plt.title('Hours vs Scores') plt.show()





X = df[['Hours']] # Independent variable y = df['Scores'] # Dependent variable #Now split into training and testing sets: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


model = LinearRegression() model.fit(X_train, y_train)

Parameters
fit_interceptTrue
copy_XTrue
tol1e-06
n_jobsNone
positiveFalse
Fitted attributes
NameTypeValue
coef_ndarray[float64](1,)[9.61]
feature_names_in_ndarray[object](1,)['Hours']
intercept_float641.509
n_features_in_int1
rank_int1
singular_ndarray[float64](1,)[7.62]
y_pred = model.predict(X_test)

print(y_pred)

[88.01724138 20.73275862]
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))

print("R2 Score:", r2_score(y_test, y_pred))
Mean Squared Error: 4.820340368608807
R2 Score: 0.9954363641480627
y_pred = model.predict(X_test) print(y_pred)


[88.01724138 20.73275862]

print("Mean Squared Error:", mean_squared_error(y_test, y_pred)) print("R2 Score:", r2_score(y_test, y_pred))

Mean Squared Error: 4.820340368608807
R2 Score: 0.9954363641480627


plt.scatter(X, y, color='blue') plt.plot(X, model.predict(X), color='red') plt.xlabel('Hours Studied') plt.ylabel('Scores') plt.title('Linear Regression - Hours vs Scores') plt.show()




hours = np.array([[7.5]]) predicted_score = model.predict(hours) print(f"Predicted Score for 7.5 hours: {predicted_score[0]}")
Predicted Score for 7.5 hours: 73.59913793103448
Posted by bijan krishna paul at 12:55 PM No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Python Online frame

Posted by bijan krishna paul at 10:58 AM No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Thursday, August 13, 2026

mglearn

Posted by bijan krishna paul at 1:50 PM No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Pandas for data science

 

  • Handles missing data (NaN, NA, NaT easily).
  • Flexible size mutability (insert/delete columns).
  • Automatic data alignment (aligns labels automatically).
  • Powerful groupby engine (split-apply-combine operations).
  • Easy data conversion (converts structures to DataFrames).
  • Smart indexing (label-based slicing and subsetting).
  • Intuitive data merging (easy joining of datasets).
  • Flexible reshaping (pivoting and restructuring data).
  • Hierarchical axis labeling (multiple labels per tick).
  • Robust I/O tools (loads CSV, Excel, HDF5).
  • Time-series ready (date shifting and frequency conversion).

  • Series: One-dimensional array-like structure.
  • DataFrame: Two-dimensional, size-mutable tabular structure.

1.
import pandas as pd
s = pd.Series([1, 3, 5, 7, 9])
print(s)


0    1
1    3
2    5
3    7
4    9
dtype: int64




2.

data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],
        'Age': [28, 34, 29, 32]}
df = pd.DataFrame(data)
print(df)


    Name  Age
0   John   28
1   Anna   34
2  Peter   29
3  Linda   32



3.

# Creating a DataFrame from a dictionary. import pandas as pd data = { 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'Paris', 'London'] } df = pd.DataFrame(data) print(df)

      Name  Age      City
0    Alice   25  New York
1      Bob   30     Paris
2  Charlie   35    London



4. # To look at the first few rows of a DataFrame. print(df.head()) # First five rows

      Name  Age      City
0    Alice   25  New York
1      Bob   30     Paris
2  Charlie   35    London



5.
# To access a column in the DataFrame.
ages = df['Age']
print(ages)
print(df['Name']) # Displays the 'Name' column

0    25
1    30
2    35
Name: Age, dtype: int64
0      Alice
1        Bob
2    Charlie
Name: Name, dtype: object


6.

subset = df[0:2] # First two rows print(subset)


    Name  Age      City
0  Alice   25  New York
1    Bob   30     Paris


7.


filtered_df = df[df['Age'] > 30] # Rows where age is greater than 30 print(filtered_df)

      Name  Age    City
2  Charlie   35  London



8.

df['AgeInTenYears'] = df['Age'] + 10 print(df)

      Name  Age      City  AgeInTenYears
0    Alice   25  New York             35
1      Bob   30     Paris             40
2  Charlie   35    London             45



9.
print(df.describe())

        Age  AgeInTenYears
count   3.0            3.0
mean   30.0           40.0
std     5.0            5.0
min    25.0           35.0
25%    27.5           37.5
50%    30.0           40.0
75%    32.5           42.5
max    35.0           45.0


10.
f.rename(columns={'AgeYears': 'Age'}, inplace=True) #print(df['Age'].mean()) # Average age


11.
df print(df['Age'].mean()) # Average age

30.0


12.
df.plot(kind='line')
<Axes: >





df['Age'].plot(kind='bar')
<Axes: >



# Select rows where 'Age' is greater than 30 print(df[df['Age'] > 30]) filtered_df = df[df['Age'] > 30] # Rows where age is greater than 30 print(filtered_df)

     Name   Age    City  AgeInTenYears
2  Charlie  35.0  London             45
      Name   Age    City  AgeInTenYears
2  Charlie  35.0  London             45















#DATA CLEANING #Drop Rows With Missing Values import pandas as pd # Define a dictionary with Indian employee data containing missing values (None) data = { "Employee": ["Aarav", "Priya", "Rahul", "Ananya", "Kabir"], "Salary_LPA": [12.0, None, 18.5, 9.0, 15.0], # Priya's salary is missing "City": ["Mumbai", "Delhi", None, "Bangalore", None], # Rahul and Kabir have missing cities } df = pd.DataFrame(data) print("Original Data:\n", df) print() # Use dropna() to remove rows that contain any missing values df_cleaned = df.dropna() print("Cleaned Data (Rows with any missing values removed):\n", df_cleaned)


Original Data:
   Employee  Salary_LPA       City
0    Aarav        12.0     Mumbai
1    Priya         NaN      Delhi
2    Rahul        18.5       None
3   Ananya         9.0  Bangalore
4    Kabir        15.0       None

Cleaned Data (Rows with any missing values removed):
   Employee  Salary_LPA       City
0    Aarav        12.0     Mumbai
3   Ananya         9.0  Bangalore




#Fill Missing Values import pandas as pd # define a dictionary with sample data which includes some missing values data = { 'A': [1, 2, 3, None, 5], 'B': [None, 2, 3, 4, 5], 'C': [1, 2, None, None, 5] } df = pd.DataFrame(data) print("Original Data:\n", df) # filling NaN values with 0 df.fillna(0, inplace=True) print("\nData after filling NaN with 0:\n", df)



Original Data:
      A    B    C
0  1.0  NaN  1.0
1  2.0  2.0  2.0
2  3.0  3.0  NaN
3  NaN  4.0  NaN
4  5.0  5.0  5.0

Data after filling NaN with 0:
      A    B    C
0  1.0  0.0  1.0
1  2.0  2.0  2.0
2  3.0  3.0  0.0
3  0.0  4.0  0.0
4  5.0  5.0  5.0




import pandas as pd # define a dictionary with sample data which includes some missing values data = { 'A': [1, 2, 3, None, 5], 'B': [None, 2, 3, 4, 5], 'C': [1, 2, None, None, 5] } df = pd.DataFrame(data) print("Original Data:\n", df) # filling NaN values with the mean of each column df.fillna(df.mean(), inplace=True) print("\nData after filling NaN with mean:\n", df)



Original Data:
      A    B    C
0  1.0  NaN  1.0
1  2.0  2.0  2.0
2  3.0  3.0  NaN
3  NaN  4.0  NaN
4  5.0  5.0  5.0

Data after filling NaN with mean:
       A    B         C
0  1.00  3.5  1.000000
1  2.00  2.0  2.000000
2  3.00  3.0  2.666667
3  2.75  4.0  2.666667
4  5.00  5.0  5.000000



import pandas as pd # sample data data = { 'A': [1, 2, 2, 3, 3, 4], 'B': [5, 6, 6, 7, 8, 8] } df = pd.DataFrame(data) print("Original DataFrame:\n", df.to_string(index=False)) # detect duplicates print("\nDuplicate Rows:\n", df[df.duplicated()].to_string(index=False)) # remove duplicates based on column 'A' df.drop_duplicates(subset=['A'], keep='first', inplace=True) print("\nDataFrame after removing duplicates based on column 'A':\n", df.to_string(index=False))

Original DataFrame: A B 1 5 2 6 2 6 3 7 3 8 4 8 Duplicate Rows: A B 2 6 DataFrame after removing duplicates based on column 'A': A B 1 5 2 6 3 7 4 8















Posted by bijan krishna paul at 1:49 PM No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Search This Blog

WELCOME

WEST BENGAL STATE UNIVERSITY COMPUTER SCIENCE NEP NEW SYLLABUS COMPUTER SCIENCE 2023 NEP

SEM 1SEM 2SEM 3
SEM 4SEM 5SEM 6

UNIVERSITY OF CALCUTTA

SEM 1 SEM 2 SEM 3
SEM 4 SEM 5 SEM 6

WEST BENGAL STATE UNIVERSITY CBCS SYLLABUS(2018-2022)

SEM 1SEM 2SEM 3
SEM 4SEM 5SEM 6

SCHOOL

CLASS-4CLASS-5CLASS-6
CLASS-7CLASS-8CLASS-9
CLASS10CLASS11 applicationCLASS12 application
CLASS11 scienceCLASS12 science

core subject

C C++ CORE JAVA SQL PYTHON
MS OFFICE HTML VISUAL BASIC advanced java 8085
PROLOG ASSEMBLY LANGUAGE JAVA SCRIPT SHELL PROGRAMMING R
DIGITAL ELECTRONICS COMPUTER ARCHITECTURE DATA STRUCTURE OPERATING SYSTEM GRAPH THEORY
DISCRETE MATHEMATICS NUMERICAL ALGORITHM AUTOMATA MICROPROCESSOR NETWORKING
GRAPHICS SOFTWARE ENGINEERING DATABSE ANALYSIS OF ALGORITHM IMAGE PROCESSING
ARTIFICIAL INTELLIGENCE BIG DATA CLOUD COMPUTING DATA MINING INTERNET TECHNOLOGY

list

  1. students gallery
  2. course
    1. west bengal CLASS 12 COMPUTER SCIENCE
    2. west bengal CLASS 12 COMPUTER APPLICATION
    3. west bengal CLASS 11 COMPUTER SCIENCE
    4. west bengal CLASS 11 COMPUTER APPLICATION
    5. CBCS COMPUTER SCIENCE NEW SYLLABUS( UNIVERSITY OF CALCUTTA )
    6. SEM 1 SEM 2 SEM 3
      SEM 4 SEM 5 SEM 6
    7. CBCS COMPUTER SCIENCE NEW SYLLABUS ( WEST BENGAL STATE UNIVERSITY )
    8. SEM 1SEM 2SEM 3
      SEM 4SEM 5SEM 6
    9. BCA
    10. MCA
    11. CBSE 12
    12. ISC 12
    13. CBSE 11
    14. ISC 11
    15. WEST BENGAL BOARD
    16. CLASS-4CLASS-5CLASS-6
      CLASS-7CLASS-8CLASS-9
      CLASS10CLASS11 applicationCLASS12 application
      CLASS11 scienceCLASS12 science
    17. CBSE BOARD
    18. CLASS 4CLASS 5CLASS 6
      CLASS 7CLASS 8CLASS 9
      CLASS 10CLASS11CLASS12
    19. ISCE & ISC
    20. CLASS 4CLASS 5CLASS 6
      CLASS 7CLASS 8CLASS 9
      CLASS 10CLASS11CLASS12
  3. language AND SOFTWARE
    1. MS OFFICE
    2. c
    3. c++
    4. core java
    5. advanced java
    6. visual basic 6
    7. sql
    8. python
    9. R
  4. java
    1. core java
    2. advanced java
  5. microprocessor
    • 8085 theory
    • 8085 question set
      1. 8085 question set (Gaonkar)
    • program code
      1. 8085 assignment list for BSC, BCA,MCA
      2. 8085 assignment code
      3. 8085 op code
      4. 8086
  6. digital
    • theory
      1. Theory question set - CLICK HERE
      2. sequential question set 1
      3. register question
      4. counter question
    • practical
  7. basic electronics
    • theory
      1. question set 1
      2. question set 2
    • practical
    • question set
  8. architecture/organization
    • theory
    • practical
      1. assembly languiage
    • question set
      1. question set 1
  9. data structure
    • theory
    • practical
    • question set
      1. sorting question
  10. system software
    • theory
    • practical
    • question set
  11. operating system
    • theory
    • question set
      1. question set
    • practical
  12. c language
    • theory
      1. theory
    • practical
      1. basic program
      2. data structure
      3. numerical analysis
      4. graph theory
      5. sorting & searching
      6. file programming
      7. bitwise operator
      8. recursion
    • question set
  13. graph theory
    • theory
    • practical
    • question set
  14. discrete mathematics
    • theory
    • practical
    • question set
  15. numerical analysis
    • theory
    • practical
      1. bisection
    • question set
  16. formal languages and automata
    • theory
    • practical
    • question set
  17. networking
    • theory
    • practical
    • question set
      1. question set
  18. graphics
    • theory
      1. question set
        1. practiical
        2. question set
  19. c++
    • theory
      1. question set
      2. practical
        1. this function
        2. UGC cbcs assignment
        3. array insertion and deletion
    • question set
  20. sofware engineering
    • theory
    • practical
    • question set
      1. question set
  21. dbms
    • theory
      1. question set
    • practical
      1. SQL
    • question set
  22. image processing
    • theory
    • practical
    • question set
  23. information retrieval
    • theory
    • practical
    • question set
  24. unix
    1. unix command - set1
    2. unix command - set2
    3. unix command - set3
    4. shell program
  25. dos
    1. dos command - set1
  26. vb
    • theory
    • practical
      1. basic program
      2. vb-oracle connection
    • question set
  27. syllabus
  28. CU BSC computer science old syllabus WBSU BSC computer science old syllabus
    CU cbcs BSC computer science HONOURS syllabus 2018 WBSU cbcs BSc computer science HONOURS syllabus 2018
    CU cbcs BSC computer science GENERAL syllabus 2018 WBSU cbcs BSC computer science GENERAL syllabus 2018

Blog Archive

  • ▼  2026 (76)
    • ▼  August (18)
      • HOW TO TRAIN A MODEL STEP BY STEP IN PYTHON
      • Python Online frame
      • mglearn
      • Pandas for data science
      • matplotlib
      • SciPy
      • NumPy
      • scikit-learn
      • 4. image input and display (gray scale)
      • 8. Image Thresholding using OpenCV (or Image Binar...
      • 7. Image Thresholding using OpenCV (or Image Binar...
      • 6. OpenCV image reading and writing
      • 5. OpenCV image reading and writing
      • 3. Get number of pixel, dimension of image
      • 2. image input and display (color)
      • 1. image input and display
      • MACHINE LEARNING USING PYTHON
      • Image Processing using Python OpenCV
    • ►  July (11)
    • ►  June (26)
    • ►  April (4)
    • ►  February (8)
    • ►  January (9)
  • ►  2025 (23)
    • ►  December (3)
    • ►  November (4)
    • ►  March (11)
    • ►  February (5)
  • ►  2024 (134)
    • ►  November (13)
    • ►  August (12)
    • ►  July (1)
    • ►  June (5)
    • ►  March (47)
    • ►  February (50)
    • ►  January (6)
  • ►  2023 (105)
    • ►  December (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (13)
    • ►  July (64)
    • ►  March (1)
    • ►  January (23)
  • ►  2022 (31)
    • ►  October (1)
    • ►  June (1)
    • ►  May (29)
  • ►  2021 (72)
    • ►  December (14)
    • ►  May (11)
    • ►  February (22)
    • ►  January (25)
  • ►  2020 (564)
    • ►  December (33)
    • ►  November (123)
    • ►  October (16)
    • ►  August (1)
    • ►  June (87)
    • ►  May (83)
    • ►  April (221)
  • ►  2019 (115)
    • ►  December (2)
    • ►  September (1)
    • ►  August (1)
    • ►  May (9)
    • ►  April (46)
    • ►  March (42)
    • ►  February (14)
  • ►  2018 (1)
    • ►  December (1)
  • ►  2017 (7)
    • ►  August (1)
    • ►  July (3)
    • ►  January (3)
  • ►  2016 (22)
    • ►  November (5)
    • ►  September (1)
    • ►  August (1)
    • ►  July (3)
    • ►  June (10)
    • ►  May (2)
  • ►  2015 (3)
    • ►  December (2)
    • ►  September (1)
  • ►  2014 (3)
    • ►  September (1)
    • ►  March (1)
    • ►  January (1)
  • ►  2013 (44)
    • ►  October (4)
    • ►  September (9)
    • ►  July (2)
    • ►  June (1)
    • ►  May (7)
    • ►  April (7)
    • ►  March (12)
    • ►  February (1)
    • ►  January (1)

About Me

bijan krishna paul
View my complete profile
Watermark theme. Powered by Blogger.