----------------------------------Coding
Library---------------------------------------------
##Import popular library for EDA/Visualization
import numpy as np
import pandas as pd
import [Link] as plt
%matplotlib inline
import seaborn as sns
#Be careful using warnigns
import warnings
[Link]('ignore')
##Read file/get the data (csv/excel)
-----------------------------------------------------------------------------------
-----------
Method 1: Read csv file
df = pd.read_csv('titanic_train.csv')
##Actual file path
df = pd.read_csv('D:\\For Dan\\Learning\\Udemy\\Python\\[Link]')
##Method 2: Change Woring Directory
import os
print([Link]())
-->C:\Users\wooju\Desktop\Python Programing
[Link]('D:\\For Dan\\Learning\\Udemy\\Python')
df = pd.read_csv('[Link]')
[Link]
##Column rename
[Link] = ['CountryName', 'CountryCode', 'BirthRate',
'InternetUsers','IncomeGroup']
## [column name] to get the unique items within the column
[Link]()
[Link]()
[Link]()
##[Link]().transpose()
##Passing the filter with more than 1 conditions ( and & or |)
df[([Link] >= 40) & ([Link] < 2)]
df[[Link] == 'Malta']
[Link] = [Link]('category')
----------------------------------Visualization
---------------------------------------------
import seaborn as sns
sns.set_style('darkgrid')
sns.set_style('whitegrid')
[Link]['[Link]'] = 8,4
[Link](figsize=(8,4))
##Histogram/Distribution
[Link]()
vis1 = [Link](stats['InternetUsers'], hist_kws={"edgecolor":"Black"},
bins=20)
[Link]()
[Link]([Link], bins = 15)
#With filter
h1 = [Link](movies[[Link] == 'Drama'].BudgetMillion)
##Stacked column chart
listgen = list() or []
listlabel = list() or[]
for gen in [Link]:
[Link](movies[[Link] == gen].BudgetMillion)
[Link](gen)
sns.set_style('darkgrid')
fig, ax = [Link]()
fig.set_size_inches(11.7,8.27)
h2 = [Link](list1, bins = 20, stacked = True, rwidth = 1, label = listlabel)
#
[Link]('Movie Budget Distribution', fontsize=30)
[Link]('Number of Movies',fontsize=15)
[Link]('Budget',fontsize=15)
[Link](fontsize=15)
[Link](fontsize=15)
[Link](frameon = True, fancybox = True, shadow = True, fontsize=15)
[Link]()
##Subplot
f, axes = [Link](1,2,figsize = (12,6), sharex = True, sharey=True)
k3 = [Link]([Link], [Link],cmap = 'Greens',
ax = axes[0])
k4 = [Link]([Link], [Link],
ax = axes[1])
[Link](xlim = (-20,160)) #custom x-axis range
[Link]()
##violin plot
w = [Link](data=movies, x = 'Genre', y = 'CriticRatings')
##Boxplot
[Link]()
vis2 = [Link](data = stats, x = 'IncomeGroup', y = 'BirthRate')
##Linear Model
vis3 = [Link](data = stats, x = 'InternetUsers', y = 'BirthRate',
fit_reg = False, hue = 'IncomeGroup', size = 10, aspect=1)
##Jointplot
j = [Link](data = movies, x = 'CriticRatings', y = 'AudienceRatings')
j = [Link](data = movies, x = 'CriticRatings', y = 'AudienceRatings',
kind = 'kde')
##FacetGrid
# Controlling Axes and Adding Diagonals
g = [Link](movies, row='Genre', col='YearRelease', hue='Genre')
kws = dict(s=50, edgecolor='black', linewidth=0.5)
g = [Link]([Link], 'CriticRatings', 'AudienceRatings')
[Link](xlim=(0,100), ylim=(20,100))
for ax in [Link]:
[Link]((0,100),(20,100), c='grey', ls='--')
g.add_legend()
[Link]()
-----------------------------------------------------------------------------------
-----------
#sns.set_style('darkgrid') #white, whitegrid, dard, darkgrid
sns.set_style('dark', {'[Link]':'Black'})
f, axes = [Link](2,2, figsize = (15,15))
k1 = [Link]([Link], [Link],
shade = True, Shade_lowest = True, cmap='inferno',
ax = axes[0,0])
k1b = [Link]([Link], [Link], cmap = 'PuBu',
ax = axes[0,0])
k2 = [Link]([Link], [Link],
shade = True, Shade_lowest = True, cmap='inferno',
ax = axes[0,1])
k2b = [Link]([Link], [Link], cmap = 'cool',
ax = axes[0,1])
v = [Link](data=movies, x = 'YearRelease', y = 'BudgetMillion',
palette='YlOrRd',
ax = axes[1,0])
k4 = [Link]([Link], [Link],
shade = True, shade_lowest = False, cmap = 'Blues_r',
ax = axes[1,1])
k4b = [Link]([Link], [Link], cmap =
'gist_gray_r',
ax = axes[1,1])
[Link](xlim = (-20,200))
[Link](xlim = (-20,200))
[Link]()
def myplot(data, playerlist = Players):
Col = {"KobeBryant":'Black',"JoeJohnson":'green',"LeBronJames":'red',
"CarmeloAnthony":'y',"DwightHoward":'k',"ChrisBosh":'m',
"ChrisPaul":'b',"KevinDurant":'k',"DerrickRose":'c',"DwayneWade":'m'}
Mkers = {"KobeBryant":"o","JoeJohnson":"D","LeBronJames":"^",
"CarmeloAnthony":"*","DwightHoward":"v","ChrisBosh":'',
"ChrisPaul":"p","KevinDurant":"D","DerrickRose":"H","DwayneWade":"^"}
for name in playerlist:
[Link](data[Pdict[name]], c=Col[name], ls = '--',
Marker = Mkers[name], ms = 8, label = name)
[Link](loc = 'upper left', bbox_to_anchor = (1,1))
[Link](list(range(0,10)), Seasons, rotation = 'horizontal')
[Link]()
------------------------------------------Machine
Learning-------------------------------------
----------------LinearRegression
----------------LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
lm = LinearRegression(
logmodel = LogisticRegression()
[Link](X_train,y_train)
[Link](X_train,y_train)
predictions = [Link](X_test)
predictions = [Link](X_test)
from [Link] import confusion_matrix
accuracy = confusion_matrix(y_test,predictions)
from [Link] import accuracy_score
acscore = accuracy_score(y_test,predictions)
#F1-Score??
from [Link] import classification_report
print(classification_report(y_test,predictions))
---------------------KNN
from [Link] import StandardScaler
##Standardize
scaler = StandardScaler()
[Link]([Link]('TARGET CLASS', axis=1))
scaled_features = [Link]([Link]('TARGET CLASS', axis=1))
df_feat = [Link](scaled_features, columns=[Link][0:-1])
from sklearn.model_selection import train_test_split
X = df_feat
y = df['TARGET CLASS']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,
random_state=101)
from [Link] import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
[Link](X_train, y_train)
predictions = [Link](X_test)
from [Link] import classification_report, confusion_matrix
print(confusion_matrix(y_test, predictions))
print(classification_report(y_test, predictions))
##Find the minimum K-value
error_rate = []
for i in range(1,40):
knn = KNeighborsClassifier(n_neighbors=i)
[Link](X_train, y_train)
predictions = [Link](X_test)
error_rate.append([Link](predictions != y_test))
[Link](figsize=(10,6))
[Link](range(1,40), error_rate, color='blue', linestyle='-', marker='o',
markerfacecolor='red', markersize=10)
[Link]('Error Rate vs K-value')
[Link]('K')
[Link]('K-value')
knn = KNeighborsClassifier(n_neighbors=17)
[Link](X_train, y_train)
predictions = [Link](X_test)
print(confusion_matrix(y_test, predictions))
print('\n')
print(classification_report(y_test, predictions))
--------------------------------------Decision
Tree-----------------------------------------
X = final_data.drop('[Link]', axis=1)
y = final_data['[Link]']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=101)
from [Link] import DecisionTreeClassifier
dtree = DecisionTreeClassifier()
[Link](X_train, y_train)
predictions = [Link](X_test)
from [Link] import classification_report, confusion_matrix
print(confusion_matrix(y_test, predictions))
print('\n')
print(classification_report(y_test, predictions))
from [Link] import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=200)
[Link](X_train, y_train)
rfc_pred = [Link](X_test)
print(confusion_matrix(y_test, rfc_pred))
print('\n')
print(classification_report(y_test, rfc_pred))
------------------------------Standardisation vs Max-Min
Normalization----------------------------------------------------
Unit/magnitude
Standardisation
#Import library
from [Link] import StandardScaler
sc_X = StandardScaler()
sc_X = sc_X.fit_transform(df)
#Convert to table format - StandardScaler
sc_X = [Link](data=sc_X, columns=["Age",
"Salary","Purchased","Country_France","Country_Germany", "Country_spain"])
sc_X
Max-Min Normalization
from [Link] import MinMaxScaler
scaler = MinMaxScaler()
[Link](df)
scaled_features = [Link](df)
#Convert to table format - MinMaxScaler
df_MinMax = [Link](data=scaled_features, columns=["Age",
"Salary","Purchased","Country_France","Country_Germany", "Country_spain"])