0% found this document useful (0 votes)
141 views6 pages

Praktikum IV

The document discusses analyzing customer data from a Starbucks store using Pandas and Seaborn in Python. It loads customer data from a CSV file and explores the data, looking at variables like gender, purchases, time spent, and number of visits. It generates histograms and calculates outliers to identify relationships between variables like gender and purchases. Heatmaps and calculations of covariance and correlation are used to quantify relationships between variables like time spent, satisfaction ratings, and number of visits.

Uploaded by

Pujiyanti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views6 pages

Praktikum IV

The document discusses analyzing customer data from a Starbucks store using Pandas and Seaborn in Python. It loads customer data from a CSV file and explores the data, looking at variables like gender, purchases, time spent, and number of visits. It generates histograms and calculates outliers to identify relationships between variables like gender and purchases. Heatmaps and calculations of covariance and correlation are used to quantify relationships between variables like time spent, satisfaction ratings, and number of visits.

Uploaded by

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

10/19/21, 8:56 AM Untitled3.

ipynb - Colaboratory

import pandas as pd

import numpy as np

import seaborn as sns

import matplotlib.pyplot as plt

from pandas.plotting import autocorrelation_plot

from scipy import stats
plt.style.use("ggplot")

import warnings

warnings.filterwarnings("ignore")
from scipy import stats

url_data = "https://raw.githubusercontent.com/supasonicx/ATA-praktikum-01/main/Starbucks%2
data = pd.read_csv(url_data)

data.head()

chaseCoffee itempurchaseCold itemPurchasePastries itemPurchaseJuices itemPurchaseS

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

data.shape

(113, 33)

data.columns

Index(['Id', 'gender', 'age', 'status', 'income', 'visitNo', 'method',

'timeSpend', 'location', 'membershipCard', 'itemPurchaseCoffee',

'itempurchaseCold', 'itemPurchasePastries', 'itemPurchaseJuices',

'itemPurchaseSandwiches', 'itemPurchaseOthers', 'spendPurchase',

'productRate', 'priceRate', 'promoRate', 'ambianceRate', 'wifiRate',

'serviceRate', 'chooseRate', 'promoMethodApp', 'promoMethodSoc',

'promoMethodEmail', 'promoMethodDeal', 'promoMethodFriend',

'promoMethodDisplay', 'promoMethodBillboard', 'promoMethodOthers',

'loyal'],

dtype='object')

data['gender'].unique()

array([1, 0])

plt.figure(figsize=(20,10))

https://colab.research.google.com/drive/1qVPMTOdz4eXyXMKL6SFtxrUYWmhJZAVK#scrollTo=-2nyy2bdQ1Yn&uniqifier=1 1/6
10/19/21, 8:56 AM Untitled3.ipynb - Colaboratory

<Figure size 1440x720 with 0 Axes>


<Figure size 1440x720 with 0 Axes>

plt.figure(figsize=(20,10))

plt.title('Histogram of timeSpend')

sns.histplot(data,x='...', kde=True)

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

<ipython-input-17-576c17bd502c> in <module>()

1 plt.figure(figsize=(20,10))

2 plt.title('Histogram of timeSpend')

----> 3 sns.histplot(data,x='...', kde=True)

4 frames
/usr/local/lib/python3.7/dist-packages/seaborn/_core.py in _assign_variables_longfor
901

902 err = f"Could not interpret value `{val}` for parameter `{ke
--> 903 raise ValueError(err)

904

905 else:

ValueError: Could not interpret value `...` for parameter `x`

SEARCH STACK OVERFLOW

## ploting untuk data produk pastry 

m = plt.hist(data[data["gender"] == 1].itemPurchasePastries,bins=30,fc = (1,0,0,0.5),label
##ploting untuk data tumor dengan label jinak

f = plt.hist(data[data["gender"] == 0].itemPurchasePastries,bins=30,fc = (0,1,0,0.5),label
plt.legend()

https://colab.research.google.com/drive/1qVPMTOdz4eXyXMKL6SFtxrUYWmhJZAVK#scrollTo=-2nyy2bdQ1Yn&uniqifier=1 2/6
10/19/21, 8:56 AM Untitled3.ipynb - Colaboratory

plt.xlabel("Nilai itemPurchasePastries")

plt.ylabel("Frequency")
plt.title("Histogram Pembelian produk pastry bedasarkan demografi gender")

plt.show()

 ## Menghitung nilai outliers dari variabel timeSpend pada data customer dengan gender lak
male = data[data["gender"] == 1]

female = data[data["gender"] == 0]

desc = male.timeSpend.describe()

Q1 = desc[4]

Q3 = desc[6]

IQR = Q3-Q1

lower_bound = Q1 - 1.5*IQR

upper_bound = Q3 + 1.5*IQR

print("Apa pun di luar kisaran ini adalah outlier timeSpend: (", lower_bound ,",", upper_b
male[male.timeSpend < lower_bound].timeSpend

print("Outliers: ",male[(male.timeSpend < lower_bound) | (male.timeSpend > upper_bound)].t

Apa pun di luar kisaran ini adalah outlier timeSpend: ( -1.5 , 2.5 )

Outliers: [4]

## Menghitung nilai outliers dari variabel visitNo (jumlah kedatangan) pada data customer 
male = data[data["gender"] == 1]

female = data[data["gender"] == 0]

desc = data.<<??>>.describe()

Q1 = desc[4]

Q3 = desc[6]

IQR = Q3-Q1

lower_bound = Q1 - 1.5*IQR

upper_bound = Q3 + 1.5*IQR

print("Apa pun di luar kisaran ini adalah outlier visitNo: (", lower_bound ,",", upper_bou
female[female.timeSpend < lower_bound].timeSpend

print("Outliers: ",female[(male.timeSpend < lower_bound) | (female.timeSpend > upper_bound

https://colab.research.google.com/drive/1qVPMTOdz4eXyXMKL6SFtxrUYWmhJZAVK#scrollTo=-2nyy2bdQ1Yn&uniqifier=1 3/6
10/19/21, 8:56 AM Untitled3.ipynb - Colaboratory

File "<ipython-input-31-126ddc38fe97>", line 4

desc = data.<<??>>.describe()
^

## variabel male menyimpan data yang ada pada seluruh kolom dengan nilai gender = 1

SyntaxError: invalid syntax

male = data[data["gender"] == 1]

## variabel female menyimpan data yang ada pada seluruh kolom dengan nilai gender = 0

SEARCH STACK OVERFLOW


female = data[data["gender"] == 0]

print("mean: ",<<2.407407>>.<<2.407407>>.mean())

print("variance: ",<<0.6610761705101328>>.<<0.6610761705101328>>.var())

print("standart deviation (std): ",<< 0.8130659078513456>>.<< 0.8130659078513456>>.std())
print("describe method: ",<<54.000000>>.<<54.000000>>.describe())

File "<ipython-input-32-8f34908708e4>", line 5

print("mean: ",<<2.407407>>.<<2.407407>>.mean())

SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

mean_diff = male.visitNo.mean() - female.visitNo.mean()

var_male = male.visitNo.var()

var_female = female.visitNo.var()
var_pooled = (len(male)*var_male +len(female)*var_female )  /float(len(male)+ len(female))
effect_size = mean_diff/np.sqrt(var_pooled)

print("Effect size: ",effect_size)

Effect size: 0.40638112114512326

## Relationship Between Variables - Korelasi

* Kita dapat mengatakan bahwa dua variabel terkait satu sama lain, jika salah satunya memb
* Misalnya, harga dan jarak. Jika Anda pergi jarak jauh dengan taksi Anda akan membayar le
* Scatter Plot, Cara termudah untuk memeriksa hubungan antara dua variabel

* Matriks korelasi besar yang mencakup banyak angka

* Kisaran angka ini adalah -1 hingga 1. 

* Arti dari 1 adalah dua variabel yang saling berkorelasi positif seperti mean radius dan 
* Arti dari nol adalah tidak ada korelasi antara variabel seperti productRate dengan incom
* Arti dari -1 adalah dua variabel berkorelasi negatif satu sama lain seperti income dan m

File "<ipython-input-34-e8ec6617507c>", line 2

* Kita dapat mengatakan bahwa dua variabel terkait satu sama lain, jika salah sa
yang lain\n",

SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

f,ax=plt.subplots(figsize = (11, 9))

dfs = data.loc[:,['productRate','priceRate','serviceRate','visitNo','timeSpend','wifiRate'
sns.heatmap(dfs.corr(),annot= True,linewidths=0.5,fmt = ".1f",ax=ax)

bottom, top = ax.get_ylim()

ax.set_ylim(bottom + 0.5, top - 0.5)

plt.xticks(rotation=90)

plt.yticks(rotation=0)

https://colab.research.google.com/drive/1qVPMTOdz4eXyXMKL6SFtxrUYWmhJZAVK#scrollTo=-2nyy2bdQ1Yn&uniqifier=1 4/6
10/19/21, 8:56 AM Untitled3.ipynb - Colaboratory

plt.title('Correlation Map')

plt.savefig('graph.png')

plt.show()

print("Covariance diantara timeSpend dan income: ",data.timeSpend.cov(data.<<??>>))

print("Covariance diantara timeSpend dan visitNo: ",data.timeSpend.cov(data.<<??>>))"

File "<ipython-input-36-6fb6de043c1e>", line 1

print("Covariance diantara timeSpend dan income: ",data.timeSpend.cov(data.<<??>


^

SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

p1 = data.loc[:,["gender","serviceRate"]].corr(method= "pearson")

p2 = data.serviceRate.cov(data.timeSpend)(data.serviceRate.std()*data.timeSpend.std())

print('Pearson correlation: ')

print(p1)

print('Pearson correlation: ',p2)

https://colab.research.google.com/drive/1qVPMTOdz4eXyXMKL6SFtxrUYWmhJZAVK#scrollTo=-2nyy2bdQ1Yn&uniqifier=1 5/6
10/19/21, 8:56 AM Untitled3.ipynb - Colaboratory

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-37-e5795f5e9419> in <module>()

1 p1 = data.loc[:,["gender","serviceRate"]].corr(method= "pearson")

----> 2 p2 = data.serviceRate.cov(data.timeSpend)
(data.serviceRate.std()*data.timeSpend.std())

3 print('Pearson correlation: ')

4 print(p1)

5 print('Pearson correlation: ',p2)

TypeError: 'numpy.float64' object is not callable

SEARCH STACK OVERFLOW

ranked_data = data.rank()
spearman_corr = ranked_data.loc[:,["timeSpend","serviceRate"]].corr(method= "pearson")
print("Spearman's correlation: ")
print(spearman_corr)

Spearman's correlation:

timeSpend serviceRate

timeSpend 1.000000 -0.092361

serviceRate -0.092361 1.000000

statistic, p_value = stats.ttest_rel(data.gender,data.productRate)
print('p-value adalah: ',p_value)

p-value adalah: 9.83197741893934e-60

check 0 d selesai pada 08.56

https://colab.research.google.com/drive/1qVPMTOdz4eXyXMKL6SFtxrUYWmhJZAVK#scrollTo=-2nyy2bdQ1Yn&uniqifier=1 6/6

You might also like