AI LAB · LEARNING JOURNAL
Machine Learning की शुरुआत:
Python, CSV और NumPy

Zero se नहीं, practical se सीखते हैं! इस blog में हमने अब तक जो concepts सीखे हैं—CSV reading, Python Lists, NumPy Arrays, array creation, indexing, slicing और 2D arrays—उन्हें Hindi + English mix में examples के साथ रखा है।


Beginner-friendly notes · Practical examples · Updated: 16 November 2026




इस Blog में क्या-क्या है?

  1. CSV और Python से पढ़ना

  2. Python List और append()

  3. NumPy क्या है?

  4. Array बनाना: ones, zeros, full

  5. Shape और Reshape

  6. arange()

  7. Indexing और Slicing

  8. आगे क्या सीखना है?





1. CSV क्या है और Python इसे कैसे पढ़ता है?


CSV का full form Comma-Separated Values है। इसमें data rows और columns में रखा जाता है। हमारे practice dataset emails.csv में दो columns हैं: label और text.


label,text
spam,"Congratulations! You won a free phone. Claim now."
ham,"Hi, are we still meeting for lunch today?"
spam,"Your account has been selected for a cash prize."
ham,"Please send me the project notes."

याद रखो: label बताता है कि email spam है या ham (normal email), जबकि text में actual message होता है।

CSV को पढ़ने का Python code


import csv

with open("emails.csv", "r", encoding="utf-8") as file:
reader = csv.reader(file)

for row in reader:
print(row)

csv.reader(file) CSV की rows को एक-एक करके पढ़ने वाला reader देता है।


Header को skip करना: next(reader)


next(reader)

next() Python का built-in function है। यह iterator से अगला item निकालता है। हमारे case में पहली row header है, इसलिए next(reader) उसे पढ़कर आगे बढ़ जाता है। यह row को file से delete नहीं करता।


ध्यान दें: अगर CSV में header मौजूद है, तभी उसे skip करें। बिना header वाली CSV में next(reader) पहली data row को skip कर देगा।



2. Python List में emails store करना


List Python का built-in collection है। इसमें कई values रख सकते हैं। append() किसी item को list के आखिर में जोड़ता है।


import csv

emails = []

with open("emails.csv", "r", encoding="utf-8") as file:
reader = csv.reader(file)
next(reader) # Header skip

for row in reader:
raw_text = row[1]
emails.append(raw_text)

print("Total Emails:", len(emails))
print("First Email:", emails[0])


  • emails = [] — empty list बनाता है।

  • row[0] — label यानी spam/ham.

  • row[1] — email का actual text.

  • emails.append(raw_text) — message को list में जोड़ता है।

  • len(emails) — list में items की गिनती बताता है।


हमारे sample dataset में 10 data rows हैं, इसलिए expected count 10 है।





3. NumPy क्या है?


NumPy का नाम Numerical Python से आया है। यह Python library numerical data पर efficient calculations करने और multi-dimensional arrays के साथ काम करने में मदद करती है। Machine Learning में features और numerical datasets पर operations के लिए NumPy बहुत उपयोगी है।


Python List vs NumPy Array



Python ListGeneral-purpose collection. Mixed data types रख सकती है।
NumPy ArrayNumerical computing के लिए optimized. आम तौर पर एक ही dtype के elements रखता है।

import numpy as np

numbers = [10, 20, 30]
arr = np.array(numbers)

print(arr) # [10 20 30]
print(arr + 5) # [15 25 35]
print(arr * 2) # [20 40 60]

NumPy array पर arr + 5 हर element में 5 जोड़ता है। इसे vectorized operation कहते हैं; scalar value का array के साथ इस तरह काम करना broadcasting का एक simple example है।


List का फर्क: Python में [10, 20, 30] + [5] दोनों lists को जोड़ता है, जबकि [10, 20, 30] + 5 TypeError देता है। List में भी loop या comprehension से हर item पर calculation कर सकते हैं।



4. NumPy Arrays बनाना: zeros(), ones(), full()


इन functions का काम सिर्फ rows और columns बनाना नहीं है। ये दिए गए shape के अनुसार array बनाते हैं और उसमें शुरुआती values भरते हैं।


np.zeros()


import numpy as np

a = np.zeros(5)
print(a)

Output: [0. 0. 0. 0. 0.]


np.ones()


b = np.ones(5)
print(b)

Output: [1. 1. 1. 1. 1.]


np.full()


c = np.full(5, 7)
print(c)

Output: [7 7 7 7 7]



zerosZero values से array initialize करता है।
onesOne values से array initialize करता है।

Rows और Columns वाला example


matrix = np.ones((2, 3))
print(matrix)

Output में 2 rows और 3 columns होंगे:


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

(2, 3) में पहला number rows और दूसरा columns बताता है। Total elements = 2 × 3 = 6.


Array के elements बदल भी सकते हैं:


marks = np.zeros(3)
marks[0] = 85
marks[1] = 92
print(marks) # [85. 92. 0.]

ML note: zeros और ones initialization तथा placeholder arrays के लिए उपयोगी हैं। Neural networks में biases को zeros से initialize करना आम है; लेकिन सभी model weights को zeros या ones से initialize करना सही तरीका नहीं होता।



5. shape, ndim और reshape()


shape क्या बताता है?


arr = np.array([10, 20, 30, 40, 50])

print(arr.shape)
print(arr.ndim)

Expected output: (5,) और 1.


(5,) का मतलब one-dimensional array में 5 elements हैं। इसे 5.0 नहीं समझना है—shape एक tuple होता है।


reshape() क्या करता है?


reshape() elements बदले बिना array का arrangement बदलता है। नई shape में total elements की संख्या वही रहनी चाहिए।


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

new_arr = arr.reshape(2, 3)
print(new_arr)

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

यह 6 elements को 2 rows और 3 columns में arrange करता है। 5 elements को reshape(2, 3) नहीं कर सकते, क्योंकि 2 × 3 = 6 elements चाहिए।





6. np.arange()


np.arange() एक sequence of numbers बनाता है। Stop value शामिल नहीं होती।


arr = np.arange(1, 7)
print(arr)

Output: [1 2 3 4 5 6]


इसे reshape भी कर सकते हैं:


arr = np.arange(1, 7)
matrix = arr.reshape(2, 3)
print(matrix)

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




7. NumPy Indexing और Slicing


Indexing से एक element चुनते हैं। Slicing से elements का एक हिस्सा चुनते हैं। Python indexing zero से शुरू होती है।


1D Array


arr = np.array([10, 20, 30, 40, 50])

print(arr[0]) # 10
print(arr[2]) # 30
print(arr[-1]) # 50
print(arr[1:4]) # [20 30 40]
print(arr[::-1]) # [50 40 30 20 10]


Index और value का relation

10index 0
20index 1
30index 2
40index 3
50index 4


Slicing में start शामिल होता है, लेकिन stop शामिल नहीं होता। इसलिए arr[1:4] में index 1, 2 और 3 आते हैं।


2D Array Indexing


arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])

print(arr[1, 1]) # 5
print(arr[1, :]) # [4 5 6]
print(arr[:, 0]) # [1 4 7]
print(arr[2, :]) # [7 8 9]
print(arr[1, 0:2]) # [4 5]

2D array में format है arr[row_index, column_index]. पहले row index, फिर column index लिखते हैं।



2D Array: हर cell के नीचे [row, column] index है

1[0, 0]
2[0, 1]
3[0, 2]
4[1, 0]
5[1, 1]
6[1, 2]
7[2, 0]
8[2, 1]
9[2, 2]


Quick reference



Expressionक्या select करता है?

arr[1, 2]दूसरी row, तीसरा column
arr[1, :]पूरी दूसरी row
arr[:, 0]सभी rows का पहला column
arr[-1, -1]आखिरी row का आखिरी element





8. आगे क्या सीखना है?


अब तक हमने NumPy के शुरुआती concepts cover किए हैं। अगली lessons में इस क्रम से आगे बढ़ सकते हैं:



1Boolean Indexing & Masking
Condition के आधार पर data filter करना, जैसे arr[arr > 30].
2Array Manipulation
flatten(), transpose(), concatenate और split.
3Math और Statistics
sum, mean, median, standard deviation और axis operations.
4Broadcasting और Vectorization
Arrays पर efficient calculations और dimension compatibility.
5Pandas
Series, DataFrame, CSV, missing values, filtering और grouping.
6Scikit-learn और पहला ML Model
Train/test split, classification, evaluation और Spam Email Classifier.

हमारा final goal: अपने emails.csv dataset से Spam Email Classifier बनाना—data preparation से लेकर model evaluation तक।



एक नज़र में: अब तक क्या सीखा?



CSV Readernext(reader)Python Listsappend()NumPy Arrayzeros()ones()full()shapereshape()arange()1D IndexingSlicing2D Indexing

Learning tip: हर example को खुद type करके run करो, output observe करो और values बदलकर experiment करो। सिर्फ code याद करने के बजाय यह समझो कि हर line क्या कर रही है।







AI Lab · Machine Learning Learning Journal

Learn by building. Practice one concept at a time. 🚀