Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views5 pages

Google Collab

Belajar Pyrhon with google collab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Google Collab

Belajar Pyrhon with google collab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

### 1.

Membuka Google Colab

- Buka Google Colab di browser Anda.

- Klik tombol **New Notebook** untuk membuat notebook baru.

### 2. Instalasi Paket yang Diperlukan

Di sel pertama, instal paket yang diperlukan dengan menjalankan perintah berikut:

```python

!pip install scrapy

!pip install pandas

!pip install scikit-learn

!pip install nltk

```

### 3. Scraping Data dari Shopee

Di sel kedua, tambahkan kode untuk scraping data dari Shopee:

```python

import scrapy

from scrapy.crawler import CrawlerProcess

import pandas as pd

class ShopeeLaptopSpider(scrapy.Spider):

name = "shopee_laptops"

start_urls = ['https://shopee.co.id/search?keyword=laptop']
def parse(self, response):

for product in response.css('div.shopee-search-item-result__item'):

yield {

'name': product.css('div._1NoI8_::text').get(),

'price': product.css('span._341bF0::text').get(),

'rating': product.css('div._3Oj5_n span::text').get(),

'reviews': product.css('div._3Oj5_n::text').get(),

process = CrawlerProcess(settings={

'FEED_FORMAT': 'csv',

'FEED_URI': 'shopee_laptops.csv',

})

process.crawl(ShopeeLaptopSpider)

process.start()

```

Jalankan sel ini untuk memulai proses scraping dan menyimpan data ke file `shopee_laptops.csv`.

### 4. Membaca Data dan Melakukan Preprocessing

Di sel ketiga, tambahkan kode untuk membaca dan melakukan preprocessing data:

```python

import re

from nltk.corpus import stopwords


from nltk.stem import PorterStemmer

from sklearn.preprocessing import MinMaxScaler

from scipy import stats

# Membaca data

df = pd.read_csv('shopee_laptops.csv')

df.dropna(inplace=True)

# Preprocessing harga

df['price'] = df['price'].str.replace('Rp', '').str.replace('.', '').astype(int)

# Preprocessing rating

df['rating'] = df['rating'].astype(float)

# Preprocessing ulasan

df['reviews'] = df['reviews'].str.replace(' ulasan', '').astype(int)

# Mengisi nilai kosong

df['name'].fillna('Unknown', inplace=True)

# Normalisasi harga

scaler = MinMaxScaler()

df['price'] = scaler.fit_transform(df[['price']])

# Menghapus outlier
df = df[(np.abs(stats.zscore(df['price'])) < 3)]

# Preprocessing teks

stop = stopwords.words('english')

stemmer = PorterStemmer()

df['name'] = df['name'].apply(lambda x: re.sub('[^a-zA-Z]', ' ', x))

df['name'] = df['name'].str.lower()

df['name'] = df['name'].apply(lambda x: ' '.join([word for word in x.split() if word not in stop]))

df['name'] = df['name'].apply(lambda x: ' '.join([stemmer.stem(word) for word in x.split()]))

# Feature engineering

df['price_per_review'] = df['price'] / (df['reviews'] + 1)

```

Jalankan sel ini untuk melakukan preprocessing data.

### 5. Membuat Model Naive Bayes

Di sel keempat, tambahkan kode untuk membuat dan melatih model Naive Bayes:

```python

from sklearn.model_selection import train_test_split

from sklearn.feature_extraction.text import CountVectorizer

from sklearn.naive_bayes import MultinomialNB

from sklearn.metrics import accuracy_score

X = df['name']
y = df['rating']

vectorizer = CountVectorizer()

X = vectorizer.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = MultinomialNB()

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

print(f'Accuracy: {accuracy_score(y_test, y_pred)}')

```

Jalankan sel ini untuk membuat model, melatihnya, dan mengevaluasi akurasinya.

Dengan mengikuti langkah-langkah di atas, Anda dapat mengimplementasikan kode untuk scraping data
dari Shopee dan membuat model rekomendasi laptop menggunakan metode Naive Bayes di Google
Colab. Jika ada pertanyaan lebih lanjut atau butuh bantuan tambahan, jangan ragu untuk bertanya! 😊

You might also like