<jupyter_start><jupyter_text>Praproses 2- Diskritisasi/binning- Scalling- Principal Component Analysis-- Feature Selection-- Feature Extraction  1. Diskritisasi/binningMengubah bilangan kontinyu menjadi diskrit atau kategorikal juga. misal Usia 1-5 diubah menjadi kategori balita, 6-15 menjadi anak-anak dan usia 16-22 menjadi remaja.<jupyter_code>usia = [12, 15, 7, 8, 40, 60]

usia_diskrit = ["anak_muda","anak_muda","bocil","bocil","dewasa","dewasa"]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("california_housing_test.csv")
data<jupyter_output><empty_output><jupyter_text>1. longitude: A measure of how far west a house is; a higher value is farther west2. latitude: A measure of how far north a house is; a higher value is farther north3. housingMedianAge: Median age of a house within a block; a lower number is a newer building4. totalRooms: Total number of rooms within a block5. totalBedrooms: Total number of bedrooms within a block6. population: Total number of people residing within a block7. households: Total number of households, a group of people residing within a home unit, for a block8. medianIncome: Median income for households within a block of houses (measured in tens of thousands of US Dollars)9. medianHouseValue: Median house value for households within a block (measured in US Dollars)<jupyter_code>data.describe()<jupyter_output><empty_output><jupyter_text>pandas.qcuthttps://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.qcut.htmlpandas.cuthttps://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html<jupyter_code>pd.qcut(data['population'], q=4).value_counts()
data['population']
pd.qcut(data['population'], q=5).value_counts().plot(kind='bar')
label = ['sedikit',
         'banyak',
         'agak banyak',
         'biasa',
         'tidak sedikit']

label_populasi = pd.qcut(data['population'],
                        q = [0, 0.2, 0.4, 0.6, 0.8, 1],
                        labels = label)

label_populasi
label_populasi.value_counts().plot(kind='barh')
data_umur_rumah = pd.cut(data['housing_median_age'], bins=5).value_counts()
data_umur_rumah
data_umur_rumah.plot(kind='bar')
label = ["lumayan tua",
        "biasa",
        "agak modern",
        "tua",
        "modern"]

label_umur_rumah = pd.cut(data['housing_median_age'],
                         bins = 5,
                         labels = label)

label_umur_rumah
label_umur_rumah.value_counts().plot(kind='barh')<jupyter_output><empty_output><jupyter_text>2. ScallingMengubah suatu data yang memiliki variasi besar sehingga memiliki skala yang tidak terlalu besar.<jupyter_code>data = pd.read_csv("baru.csv", index_col="Item_Identifier")
data
data.describe()
data.boxplot(['Item_MRP', 'Item_Weight'])
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(data[['Item_MRP']])
data['Item_MRP_Scalled'] = scaler.transform(data[['Item_MRP']])
data['Item_MRP_Scalled'].describe()
data.boxplot(['Item_MRP_Scalled'])
scaler.fit(data[['Item_Weight']])
data['Item_Weight_Scalled'] = scaler.transform(data[['Item_Weight']])
data['Item_Weight_Scalled'].describe()
data.boxplot(['Item_MRP_Scalled', 'Item_Weight_Scalled'])
# nilai min == 0
# nilai max == 200

awal = np.array([[25, 50, 150, 200, 75]])
# nilai min baru == 0
# nilai max baru == 1

(awal-0)*(1-0)/(200-0)+0<jupyter_output><empty_output><jupyter_text>3.1 Feature SelectionTujuannya adalah untuk memilih fitur yang "berpengaruh" pada dataset<jupyter_code>from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
bmsx = np.array([data["Item_Weight"],
                data["Item_Fat_Content"],
                data["Item_Visibility"],
                data['Item_MRP'],
                data["Outlet_Size"]])
bmsx.shape
bmsx
bmsxt=bmsx.transpose()
bmsxt.shape
bms_std=StandardScaler().fit_transform(bmsxt)
bms_std
pcafs=PCA(n_components=0.70,whiten=True)
bmsfs_pca=pcafs.fit_transform(bms_std)
print('Original number of features:', bms_std.shape[1])
print('Reduced numer of features:', bmsfs_pca.shape[1])
datafs_pca=pd.DataFrame(bmsfs_pca, columns=["PC1","PC2","PC3","PC4"])
datafs_pca<jupyter_output><empty_output><jupyter_text>3.2 Feature ExtractionTujuannya adalah menggabungkan/mengelompokkan feature menjadi beberapa feature saja.<jupyter_code>from sklearn import decomposition
pcafe=decomposition.PCA(n_components=3)
bmsfe_pca=pcafe.fit_transform(bms_std)
print('Original number of features:', bms_std.shape[1])
print('Reduced numer of features:', bmsfe_pca.shape[1])
datafe_pca=pd.DataFrame(bmsfe_pca, columns=["PC1","PC2", "PC3"])
datafe_pca<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup