0 ratings0% found this document useful (0 votes) 23 views3 pagesArtificial Neural Network Code
A code for artificial intelligence work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
2ar1072024, 16:29 Honour_Text_lassfcation_for_Sentiment
ip inst nite setkst-learn pandss
FE Aequirenent a1ready satisfied: alte in /usr/local/tsb/eythons.20/dist-packages (3.8.1)
Requirenent ainesdy satisfied: scikst-leaen in /usr/loea)/Lie/pyenend1@/cist-paccages (1.5.2)
Requirenent already Satisfied: pandas in /ust/Local/1ib/python3-10/aist-paceages (2.2.2),
Recvirenont already satisfied: Elick in /usr/Local/1ib/pythons.18/¢i5t-packages (éron nish) (81.7)
cs (Fram ith) (2.4.2)
Requirement aineady satisfied: regox>=2023.8.9 in /usr/local/16/python®.o/dese-pockages (fron nitk) (2026.9.11)
Requirement already satisfied: tqdn In /ust/loeal/lib/oython).28/dist-packages (Fron Alli) (6-665)
nt already satiafied: munpyo-1-29.5 in /asr/Toeal/Iib/ python) 28/alst=packages (from acikit-leare) (2.26.4)
Recuirenent already Satisfied: Scipy>=1.6.8 in /ase/locai/ib/pythons.i0/dist-paceages (fron scikit-learn) (2-13-1)
‘rrenepoolcel>-3. 1.0 in fusr/loeal/1sb/pythons.s0/dise-packagee (Aram seSktt-loaea) (2-5-0)
2.8.2 tn /usr/1oCal/11b/pytnon3.10/eist-pactages (#ron pandas) (2-8-2)
8-1 in /vsr/locai/Mib/pytnon3.18/eist-paceages (From paras) (2624.2)
Requirement already satisfied: €24ata>=2022.7 9 fuse/local/1sb/aython3-40/dst-packages (Fron pandas) (2024-2)
Recuirenent already satisfied: shx3-1.3 sn /usr/Toesl/Isb/python}.28/akst-packages (fron python-dateutsl>=2-2.2-2p9r838) (1.16.9)
Requirement already satisfied: Joblsb in /sr/loeal/1i0/pyenon3. 10/4138 pack
Requirenent ainendy satisfied
Requirement already satisfied
Requirement already satisfied
tevti
anport re
fnport pandas a5 pe
Smport nite
‘tran skearnfeature.extraction. text ingort T#idfVectortzer
rom skLearn model selection sngort train test split
rom sklearncreighbors import tNeightorsClassi tier
‘rom sklearnonetries Inport classification-report, accuracy. score
rom sklearn-preprocessing import LabelEncoder
# bamiond nite esta
rithedounlondt'stoprords')
sith download ‘purke")
Sample Tustter Data (replace with actual dataset or use Taitter API)
asta = (
Sweet's [
“r love this product! e's anazing. #hagoy",
vinis is the worst experience £ have ever had! #angry",
"I'm not sure how I feel sbout this. I's okay, T guess. treutral”,
‘his 44 fantastic! Great Job. #hIPEY"y
Feeling good about this. Wat a great day. Ynapey”,
"This product is terrible. Tt broke in two aye. angry",
1's okay, tot the best but not the worst ether. mneutral”,
L
ercinent
positive’, "Negstive’, "Nevtral’, ‘Negative’,
positive’, "Positive’, “Negstive’, “Neutral
2
af = pa.batarrane(aata)
‘SE [nith dnt} oowoading package stopords to /rot/nith date
{aicaata) " Untpping corgore/stopmords. 20.
{nuthcdsta] Bounoasing package purkt to /rot/nite
(rithcdate) — Urzipping Eokentzers/purkt. 2.
1 step 1: Prepracess the Text date
det prepracess_text(text)
I convert to lowercase
‘ext = text Lowerd)
ext = re sub(rnttp\se baa\seltetps\se", "4 text, flagsere.MULTILINE)
4 fnove hashtags and mentions
text = ressub(e" iar [\#","", text)
ext = re-Sub(r Wy text)
text = recsub(re de", °°) text)
text = ressub(e'\se, * *5 text). steip()
+ sonly preprocessing
-ntps:ifolab research google. comvdrveingkMUGO2NgDBW8ASoKh2PCGwanhsYUXfprniMede=true
18.2ar1072024, 16:29 Honour_Text_t
AF ("Cleaned twnet*] = of[ 1466" ].apply(prepracess sex)
4 seep 2: Convert Text bata t9 TF-IDE vectors
{lar yectorszer = Teidflectorizer(nax_featuress500, stop sordsestopuondsworda(‘enghsh'))
Xie Clue vectortzer.._transforn(ae[ Cheaned Tweet” })-tourray()
1 encode sentinant Labels into numerical format
abel encoder = Labelencoder()
y's label encoder. (it_traneforn(4f('Sentinent’]) Onlegative, Aotevteal, 2ePositive
4 seep 3: Split she oataset into Training and Testing Oats
K.train, Xtest, yotrain, y.test = train test split, yp test_size-e.2, random staton42)
ken_elassifser = KeighborsChassifier(n_nesghtorse3)
en_elassiier.#2U0\ train, y_traie)
+ Kctgnborsctassiier
rveighborsclarssfter(n_neighborse3)
1 seep 5: fake Poestetions on Test oats
Yupred = kan_clazeifien.proekce (seat)
print(*\nclaselfiestion Report:\n")
1 cet unique Labels on both y_test andy. pred
unique labels = Sorced(List(set(y_test) | Set(y_oree)))
fF ralter carget nanes based on these unique 2abeis
tonget_naree = (Jabel_encoderclaeses_[1) for £ 39 unique_1nbels)
F Generate repore with filtered target names and labels
print(classifieation.roport(y test, y.pred, target nanesetargst_nanes, Isbels-unique_120e15))
accuracy + scearacy_seore(y.test, y_ared)
prine(eaccursey: {accuracy * 300:-24))
asst fication Report:
negative 3.08 2.08.0 2
Posstive teehee Tbe i
macro og 3.08 2.82.88 2
weighted ve 881.88 1.08 2
Accuracy: 160.005
+ branple to predict sentinert of new tucets
new_ewoets = ["I hate aaiting for #0 long! This Ss frustrating.*, “Wos, this Se fantasticl Love $¢.°]
rnew_elosned tweots = [praprocess_text(twest) for tweet in neu. tweets]
new_tfiet = tflde vectorizer-transtorn(nen cleaned tweets) -tosrray()
rewcpredictions ~ kan elassifler predict (new tf14F)
rescsentinente ~ label encoder. inverse. transforn(nex_predtctSons)
brint(*\nsentinant Pradictions for New Tweets:*)
for tweet, sertinent in zip(rew tweets, ew sentiments):
prine(e"Tweet: {eweet) \nPredicted Sentinent: (sentanent)\0")
a
Santinant Predictions for New Tweets
Predicted Sentiment: Neutral
-ntps:ifolab research google. comvdrveingkMUGO2NgDBW8ASoKh2PCGwanhsYUXfprniMede=true
nalysis_using_KNN_Note
2682ar1072024, 16:29 Honour_Text_classfcation for_Sentime
mnalysis_using_KNN._Note_Use_twiter_datajpynb -Colab
hips oolab research google. comidrverInqkMUGO2NgD8t#ASoKN2PCGwanthsYUXipiniMode=tre
38.