Implementing KMeans Clustering in Python

Created By: Debasis Das(23-Feb-2021)

In this post we will see how KMeans clustering works by using sklearn KMeans Cluster and by manually writing the KMeans Clustering logic and comparing the results.

The manual approach code is a sample to demonstrate the iterative process of starting with random centroids and updating the cluster labels for each point based on its proximity to the centroids and then updating the centroids as the mean of all the points in the cluster for the given centroid and repeat the process (update cluster label, update centroids) for a given number of iterations

KMeans is a very simple algorithm to implement, has a downside of being very sensitive to noise. KMeans will try to assign a cluster to noise points as well. There are known challenges such as identifying the initial cluster centroids and initial cluster centroid selection has an effect on how the clusters will be formed.

http://www.knowstack.com/notebooks/KMeans_ManualApproach.html

Posted in Data Mining, Data Science, Python Tagged with: , ,

Pandas DataFrame – Pivot, Groupby, Filter, Query

Created By: Debasis Das (18-Feb-2021)

http://www.knowstack.com/notebooks/DataFrame_Pivot_GroupBy_Filter.html

In this post we will explore the following DataFrame functions

  • pivot_table
  • groupby
  • filter
  • query
import pandas as pd
import numpy as np

df = pd.read_csv("SalesData.csv",low_memory=False)
df

The working DataFrame is as below and we will use different DataFrame functions to slice and dice the data.

Pivoted view of the above Data 

df1 = pd.pivot_table(df,index=["Region","Country","Product"],aggfunc=np.sum)
df1

Pivot using only 2 columns, the data corresponding to the Products will be rolled up.

df2 = pd.pivot_table(df,index=["Region","Country"],aggfunc=np.sum)
df2

 

Multi-Level Column Pivot 

DataFrame pivot with multi level in columns (Week followed by product)

df3 = pd.pivot_table(df,index=["Region","Country"],columns=["Product"],aggfunc=np.sum)
df3

df4 = pd.pivot_table(df,index=["Region","Country"],columns=["Product"],values = ["WK_1","WK_2"],aggfunc=np.sum)
df4

 

Adding a new column to the pivot table 

df5 = pd.pivot_table(df,index=["Region","Country"], aggfunc=np.sum)
df5['Total'] = df5['WK_1'] + df5['WK_2']+ df5['WK_3'] + df5['WK_4']
df5

 

DataFrame Filter

fdf1 = df[df["Region"] == "America"]
fdf1

Using the DataFrame Filter function

fdf3 = df.filter(["Region", "Country", "WK_1"]) 
fdf3

DataFrame groupby function

gdf1 = df.groupby(['Region']).sum()
gdf1

gdf3 = df.groupby(['Region',"Country","Product"],sort=True).sum()
gdf3

groupby and get_group

gdf4 = df.groupby(["Country","Product"]).get_group(('Canada','Laptop'))
gdf4

gdf5 = df.groupby(["Region","Product"]).get_group(('America','Phone'))
gdf5

Iterating through a grouped data frame

gdf6 = df.groupby('Country')
for name,group in gdf6:
    print(name)
    print(group)

Using Numpy functions for groupby aggregates

gdf7 = df.groupby(['Region','Product']).aggregate(np.sum)
gdf7

Using Multiple Aggregate functions in DataFrame groupby

gdf8 = df.groupby(['Region','Country']).aggregate([np.sum,np.mean])
gdf8

Using a different aggregate function for columns and renaming the column to reflect the function used

gdf10 = df.groupby(['Region']).agg({'WK_1': np.sum, 'WK_2':np.mean}).rename(columns = {'WK_2':'WK_2 (Mean)'})
gdf10

DataFrame Query

Query to see if one column value is greater than another column

qdf1 = df[df.WK_2 > df.WK_1]
qdf1

In the below sample week 2 values are greater than week 1 values

qdf2 = df[df.WK_1 > 1500]
qdf2

Multi Column Query

qdf3 = df[(df.WK_1 > 1500) & (df.WK_2 > 1900)]
qdf3

Using the DataFrame.query function

qdf4 = df.query('WK_1 > 1500 & WK_2 > 1900')
qdf4

Posted in Data Mining, Python Tagged with: , , ,

Pandas DataFrame – Different ways to Create and Edit

By Debasis Das (17-Feb-2021)

In this post we will see Different ways of creating a pandas DataFrame and editing it

Lets first import the Python Pandas and numpy module

import pandas as pd
import numpy as np
import random
pd.set_option('display.width', 1000)

Creating a dataframe by reading from a CSV

salesDataDf = pd.read_csv("SalesData.csv",low_memory=False)
print(salesDataDf)

Output:

    Region  Country Product  WK_1  WK_2  WK_3  Wk_4
0  America      USA  Laptop  1241  1160  1929  1174
1  America      USA   Phone  1098  1092  1089  1819
2  America   Canada  Laptop  1441  1099  1950  1394
3  America   Canada   Phone  1990  1057  1656  1060
4   Europe  Belgium  Laptop  1084  1116  1002  1566
5   Europe  Belgium   Phone  1574  1958  1793  1213
6   Europe  Finland  Laptop  1325  1374  1300  1579
7   Europe  Finland   Phone  1347  1736  1782  1921

Create a DataFrame from a List

seasons = ['Winter','Spring','Summer','Fall']
df = pd.DataFrame(seasons)
print(df)

Output:

        0
0  Winter
1  Spring
2  Summer
3    Fall

Creating a DataFrame from an Array of Array:

data = [[100,200,300],[400,500,600],['John','Jane','Mary','Jin']]
df = pd.DataFrame(data)
print(df)

Output:

  0 1 2 3
0 100 200 300 None
1 400 500 600 None
2 John Jane Mary Jin

Creating a DataFrame by passing an arrays of values and an array of column header names:

df = pd.DataFrame([['Debasis Das',100],['John Doe',98],['Jane Doe',93]], columns = ["name","score"],dtype = float)
print(df)

Output:

  name score
0 Debasis Das 100.0
1 John Doe 98.0
2 Jane Doe 93.0

Create a DataFrame from a Dictionary:

dictionary = {"names":["John Doe","Jane Doe","Mary Jane"], "score":[90,91,93]}
df = pd.DataFrame(dictionary)
print(df)

Output:

  names score
0 John Doe 90
1 Jane Doe 91
2 Mary Jane 93

Create a DataFrame from a List of Dictionaries:

listOfDict = [{"names":"John Doe", "age":30},{"names":"Jane Doe", "age":10, "score":98.0}]
df = pd.DataFrame(listOfDict)
print(df)

Output:

  names age score
0 John Doe 30 NaN
1 Jane Doe 10 98.0

Add a column to the DataFrame based on a condition:

df = pd.DataFrame({'Score':[100,20,30,80,90]})
print(df)
df.loc[df.Score <= 80, "Grade"] = "B"
df.loc[df.Score > 80, "Grade"] = "A"
df.loc[df.Score < 35, "Grade"] = "F"
print(df)

Output:

  Score
0 100
1 20
2 30
3 80
4 90

After adding a new column

 Score Grade
0 100 A
1 20 F
2 30 F
3 80 B
4 90 A

Creating a DataFrame from a numpy array:

df = pd.DataFrame(np.random.randint(low=80, high=100, size=(3, 5)), columns=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],index=['Temp Morning', 'Temp Afternoon', 'Temp Evening'])
print(df)

Output:

             Monday Tuesday Wednesday Thursday Friday
Temp Morning   98     88      91         87     91
Temp Afternoon 92     83      80         83     81
Temp Evening   84     94      92         96     84   

Creating a DataFrame with Random Values in few columns

salesData = {"Region":["Americas","Americas","Americas","Americas","Europe","Europe","Europe","Europe"],
 "Country":["USA","USA","Mexico","Mexico","Belgium","Belgium","Finland","Finland"],
 "Product":["Phone","TV","Phone","TV","Phone","TV","Phone","TV"],
 "wk1":np.random.uniform(1000,2000,8),
 "wk2":np.random.uniform(1000,2000,8),
 "wk3":np.random.uniform(1000,2000,8),
 "wk4":np.random.uniform(1000,2000,8)
 }
salesDF = pd.DataFrame(salesData)
print(salesDF)

Output:

Adding New Columns to an Existing DataFrame:

salesDF["Total"] = salesDF['wk1'] + salesDF['wk2'] + salesDF['wk3'] + salesDF['wk4']
salesDF["Total (K)"] = salesDF["Total"] /1000
print(salesDF)

Output: In this sample we added 2 new columns (Total and Total In K Format)

Creating a DataFrame using Assign Function

# Assign returns a copy of the data frame as a new object with the new columns added to the original data frame.
salesDF1 = salesDF[["Region","Country","Product","wk1"]]
salesDF2 = salesDF1.assign(week1_k=lambda x: salesDF1['wk1']/1000)
salesDF2

Output:

Transpose a DataFrame:

data = {'Col1': [1, 2, 3], 'Col2': [4, 5, 6], 'Col3': [7, 8, 9]}
df = pd.DataFrame(data, index=['Row1', 'Row2', 'Row3'])
df_transpose = df.T

print("\nBefore Transpose")
print(df)

print("\nAfter Transpose")
print(df_transpose)

Selecting a Subset of a DataFrame using loc

data = {'Col1': [1, 2, 3], 'Col2': [4, 5, 6], 'Col3': [7, 8, 9]}
df = pd.DataFrame(data, index=['Row1', 'Row2', 'Row3'])
print(df)
df1 = df.loc['Row1':'Row2','Col1':'Col2']
print("\nSubset of the Dataframe")
print(df1)

Merge Dataframes

data1 = {"Country":["USA","Mexico","Brazil"],"Jan_Sales":[1000,2000,3000]}
data2 = {"Country":["Canada","Mexico","Brazil","Belgium"],"Feb_Sales":[4000,5000,6000,7000]}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
#Pandas merge function can automatically detect which columns are common between the dataframes 
#Pandas will use the common columns to merge the two dataframes
df1
Country	Jan_Sales
0	USA	1000
1	Mexico	2000
2	Brazil	3000

df2
Country	Feb_Sales
0	Canada	4000
1	Mexico	5000
2	Brazil	6000
3	Belgium	7000
df3 = df1.merge(df2)
df3
Country	Jan_Sales	Feb_Sales
0	Mexico	2000	5000
1	Brazil	3000	6000

Inner Merge

df4 = df1.merge(df2,how='inner')
df4
Country	Jan_Sales	Feb_Sales
0	Mexico	2000	5000
1	Brazil	3000	6000

Outer Merge

df5 = df1.merge(df2,how='outer')
df5
  Country	Jan_Sales	Feb_Sales
0	USA	1000.0	NaN
1	Mexico	2000.0	5000.0
2	Brazil	3000.0	6000.0
3	Canada	NaN	4000.0
4	Belgium	NaN	7000.0

Left Merge

df6 = df1.merge(df2,how='left')
df6
  Country	Jan_Sales	Feb_Sales
0	USA	1000	NaN
1	Mexico	2000	5000.0
2	Brazil	3000	6000.0

Right Merge

df7 = df1.merge(df2,how='right')
df7
Country	Jan_Sales	Feb_Sales
0	Canada	NaN	4000
1	Mexico	2000.0	5000
2	Brazil	3000.0	6000
3	Belgium	NaN	7000

 

Posted in Data Mining, Data Science, Python Tagged with: , ,

KMeans Clustering

By: Debasis Das (17-Feb-2021)

KMeans Clustering using SKLearn
Plotting the cluster centroid with the cluster points

import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt


A = [
[10,10],
[11,10],
[12,10],
[10,11],
[15,17],
[13.5,15],
[15,16],
[17,18],
[14.5,19],
[20,20],
[13.5,14.5]
]

df = pd.DataFrame(A)
number_clusters = 3
pca = PCA(n_components=2) 
pca.fit(df)
x_pca = pca.fit_transform(df)
km = KMeans(
    n_clusters=number_clusters, init='random',
    n_init=10, max_iter=300, 
    tol=1e-04, random_state=0
).fit(x_pca)


kmeans_labels = km.labels_
unique_labels = np.unique(kmeans_labels)
for i in unique_labels:
    plt.scatter(
        x_pca[kmeans_labels == i, 0], x_pca[kmeans_labels == i, 1]
    )
cluster_centroids = km.cluster_centers_
print("cluster_centroids = ",cluster_centroids)
print("kmeans_labels = ",kmeans_labels)
plt.scatter(cluster_centroids[:,0], cluster_centroids[:,1],linewidths = 3, s=150,marker="x", color='r')

Output:

cluster_centroids =  [[ 6.38423083 -1.0142913 ]
 [ 1.67176795  0.63576739]
 [-5.28182536 -0.28756359]]
kmeans_labels =  [2 2 2 2 1 1 1 0 1 0 1]

 

If you are interested to see how the KMeans clustering can be implemented in Python you can check the below link http://www.knowstack.com/notebooks/KMeans_ManualApproach.html

Posted in Data Mining, Data Science Tagged with: ,

SSE Calculation for a Clustering

By Debasis Das: (17-Feb-2021)

How to manually calculate the SSE for a Clustering.

Clustering such as KMeans has a inertia_ function that gives the total SSE for the clustering, however clustering such as DBScan lacks an inertia_ function and in this sample code we are going to see how we can derive the SSE number for a clustering

import pandas as pd
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

A = [[10,10],
[12,12],
[10,11],
[15,17],
[13.5,15],
[14.5,19],
[13.5,14.5]]

df = pd.DataFrame(A)
number_clusters = 2
pca = PCA(n_components=2) 
pca.fit(df)
x_pca = pca.fit_transform(df)
km = KMeans(
    n_clusters=number_clusters, init='random',
    n_init=10, max_iter=300, 
    tol=1e-04, random_state=0
).fit(x_pca)


kmeans_labels = km.labels_
print("The KMeans Labels are = ",kmeans_labels)
print("The Kmeans SSE using inertia_ function =", km.inertia_)

manual_SSE = 0
for i in range(number_clusters):
    cluster = x_pca[km.labels_ == i]
    if len(cluster) > 0:
            clusterMean = cluster.mean(axis = 0)
            manual_SSE += ((cluster - clusterMean) ** 2).sum()

print("The KMeans SSE using manual calculation = ",manual_SSE)  


# Clustering such as DBScan doesnot have a inertia function 
# and in case one needs to calculate the SSE for a DBScan clustering, 
# we can use the manual method of SSE Calculation

Output: for the given dataset

The KMeans Labels are =  [0 0 0 1 1 1 1]
The Kmeans SSE using inertia_ function = 19.04166666666666
The KMeans SSE using manual calculation =  19.041666666666664

As you can see the SSE calculated matches the SSE given by the inertia_ function.

You can use the same manual approach of using the DBScan Cluster and DBScan Labels to come up with the SSE for DBScan Clustering

Posted in Data Mining, Data Science Tagged with: , , ,

DBScan Clustering Sample

Written By: Debasis Das 

In this post we will generate a DBScan cluster using SKLearn DBSCAN module and will generate the following

  1. List of noise/outlier points (not readily available in DBSCAN model output)
  2. Index of noise/outlier points
  3. View the clusters by cluster label and identify all the core points in the clusters
  4. All the core points, count of core point, indexes of the core points
  5. Estimated number of clusters
  6. Silhouette Score of the Clustering
  7. Silhouette Score of the Clustering by ignoring the noise points

Note: the noise points are those with label -1

import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.pyplot as plt

epsilon = 3
min_samples = 2

X = [
[1,2],
[2,2],
[2,3],
[8,7],
[8,8],
[25,80]]

# plot
A = np.array(X)
plt.scatter(
   A[:, 0], A[:, 1],
   c='white', marker='o',
   edgecolor='red', s=50
)
print("The points prior to running DBScan Clustering")
plt.show()



db = DBSCAN(eps=epsilon, min_samples=min_samples).fit(X)

core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_
no_clusters = len(np.unique(labels) )
no_noise = np.sum(np.array(labels) == -1, axis=0)

noise_index = []
for i in range(len(labels)):
    if labels[i] == -1:
        noise_index.append(i)
print("Noise/Outliers Index are at the following indexes ",noise_index)
for obj in noise_index:
    print("Noise/outlier point = ",X[obj])

print("-"*20) 
unique_labels = set(labels)
print("unique labels are ",unique_labels)
print("-"*20)
for label in unique_labels:
    print("cluster points for ", label)
    cluster_indexes = []
    for i in range(len(labels)):
        if labels[i] == label:
            cluster_indexes.append(i)
    print("cluster_indexes = ",cluster_indexes)        
    for obj in cluster_indexes:
        print("exact point = ",X[obj])
    print("-"*10)    

silhouette_avg = silhouette_score(X, labels)

print("-"*20)    
print("Labels ",labels)
print("Indices of Core Samples = ",db.core_sample_indices_)
print("Copy of each core sample found by training = ",db.components_)
print("Count of Core Samples ",len(db.core_sample_indices_))
print('Estimated no. of clusters: %d' % no_clusters)
print('Estimated no. of noise points: %d' % no_noise)
print("-"*20)    
print("silhouette_avg = ",silhouette_avg)
print("Silhoutter Avg ignoring noise points ",(silhouette_avg * len(labels))/(len(labels)-no_noise) )

The points prior to running DBScan Clustering

Noise/Outliers Index are at the following indexes  [5]
Noise/outlier point =  [25, 80]
--------------------
unique labels are  {0, 1, -1}
--------------------
cluster points for  0
cluster_indexes =  [0, 1, 2]
exact point =  [1, 2]
exact point =  [2, 2]
exact point =  [2, 3]
----------
cluster points for  1
cluster_indexes =  [3, 4]
exact point =  [8, 7]
exact point =  [8, 8]
----------
cluster points for  -1
cluster_indexes =  [5]
exact point =  [25, 80]
----------
--------------------
Labels  [ 0  0  0  1  1 -1]
Indices of Core Samples =  [0 1 2 3 4]
Copy of each core sample found by training =  [[1 2]
 [2 2]
 [2 3]
 [8 7]
 [8 8]]
Count of Core Samples  5
Estimated no. of clusters: 3
Estimated no. of noise points: 1
--------------------
silhouette_avg =  0.7227526416896678
Silhoutter Avg ignoring noise points  0.8673031700276013

Further Read/ References

https://scikit-learn.org/stable/modules/clustering.html#dbscan
https://scikit-learn.org/stable/auto_examples/cluster/plot_dbscan.html#sphx-glr-auto-examples-cluster-plot-dbscan-py

Posted in Data Mining, Data Science Tagged with: , , ,

eCharts (v3) Gauge chart

eCharts (v3) has captured my attention and am impressed. Lets create a gauge chart and also understand the code.

Note: CDN for english version of echarts version 3 can be found here :

https://cdnjs.com/libraries/echarts

HTML:

<!doctype html>
<html>
<head>
<title>Charts Gauge Example</title>
</head>
<body>
<div id="chart" style="width: 100vw; height: 100vh;">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/3.7.2/echarts-en.min.js"></script>
<script src="app.js"></script>
</body>
</html>

The js code (app.js):

var mychart = document.getElementById('chart');
 gChart = echarts.init(mychart);
 var option = {
   tooltip : {
     formatter: "{a} <br/>{b} : {c}%"
   },
   toolbox: {
     show : true,
     feature : {
       restore : {show: true},
       saveAsImage : {show: true}
    }
   },
 series : [
 { 
     title:{
         show:true,
         offsetCenter:[0,-230],
         color:'#888',
         fontWeight:'bold',
         fontSize:24 
         },
     clockwise:true,
     startAngle:180,
     endAngle:0,
     pointer:{show:true},
     axisTick:{show:true},
     splitLine:{show:false},
     name:'Business Indicators',
     type:'gauge',
     detail : {
         offsetCenter:[5,-40],
         formatter:'{value}%'
         },
     data:[{value: 75, name: 'Rate'}]
 }]
};

//Add this to have the data changed every two seconds
setInterval(function () {
 option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0;
 gChart.setOption(option, true);
},2000);

  • Here first we are setting variable “mychart” to DOM element with ID = “chart”
  • At line 2 of the JS we initialize an echart at the DOM ID (“chart”)
  • Now we set the options which will be passed to the initialized echart to render on screen

Decoding the “options”:

  • In the example , options has the following attributes
    • series
    • tooltip
    • toolbox

Series:

Here we define the

  • name of chart – ‘Business Indicators’ in the example
  • type of chart – ‘Gauge’
  • Title (“Rate” in this example)  takes its value from data.name and the styling is driven by series.title properties 
  • startAngle is 180 and endAngle is 0 (see pic) when property clockwise is set to true (default)
  • series.data.value is displayed with style and positioning defined by series.details
  • pointer and axis ticks have visbility set to true.

 

Tooltip:

  • Here we define the content to be seen when hovering over the pointer.
  • In line 33 of JS code ,
    • {a} represents name of the chart, defined in property series.name (“Businesss Indicators”)
    • {b} represents data.name
    • {c} represents data.value

Note : There are more options to customize ( described in API documentation of echarts)

Tool Box:

  • Here two features are defined to be visible -> Restore and Download as Image.
  • There are other options that can be configured

Heres the output:

Just by changing the startAngle and/or endAngle you can change the shape of the dial.

Ex, set startAngle to 225 and endAngle to -45 and here’s what you get:

 

Posted in Charts n Graphs, Generic, JS Frameworks, Web Technology

eCharts (v3) Simple Bar Chart

I have been playing around with javascript based charting libraries. I like HighCharts. Its pretty good for the charts you will need with very good customization features. It, however is not free (for commercial uses).

D3 library is good for creating your own charting solutions and there are a pretty good charting libraries out there built on top of D3. Personally am a big fan of the D3 library and Mike Bostock.

However recently I came across eCharts (version 3) (owned by Baidu) and was blown away by the examples in its gallery. Its based on ZRender which is a lightweight canvas library.

Its definitely something I will use over highcharts in my next project.

Here’s a list of things that made me try it :

  • A very rich gallery where you can play around as well -> Demo Gallery
  • Canvas based*
  • Very well documented -> API Docs
  • It also has a offline chart builder.
  • Free. Yes you can simply get the minified js and start using it.

Note : A lot of people indicated that the documentation is difficult as it is mostly in chinese. As of today I did not find that to be a hindrance and the documentation I read was in plain English.

Here’s the CDN for the english version of echarts version 3

https://cdnjs.com/libraries/echarts

Do give it a try.

Here’s a sample application :

The js code (app.js):

var chartdata=[
 {    
     name: 'Oranges',    
     type: 'bar',    
     data: [20, 20, 36, 12, 15, 20]    
 },          
 {    
     name: 'Apples',    
     type: 'bar',    
     data: [8, 5, 25, 10, 10, 13]    
 },
 {    
     name: 'Strawberries',    
     type: 'bar',    
     data: [8, 5, 20, 10, 7, 15]    
 },          
 {    
     name: 'Mangoes',    
     type: 'bar',    
     data: [8, 5, 20, 10, 7, 15]    
 }
];

var legendData = chartdata.map(function(d){return d.name;});

var chart = document.getElementById('chart');    

var myChart = echarts.init(chart);  
  
var option = {    
               title: { 
                        text: 'eCharts:Bar',                 
                        textStyle:{                          
                                    color:'#fff'                  
                                   }
                       },
               toolbox: {
                         show : true,
                         feature : {            
                                     dataView : {show: true, title:'Data View', readOnly: false},            
                                     magicType : {show: false, title:['line','bar'],type: ['line', 'bar']},            
                                     restore : {show: true, title:'Restore'},            
                                     saveAsImage : {show: true, title:"Save As Image"}        
                                    }    
                        },    
               tooltip: {                   
                         backgroundColor:'rgba(250,250,250,0.7)',                  
                         textStyle:{                              
                                     color:'#000'                              
                                   }                                     
                         },
               legend: { 
                         orient:'vertical',                   
                         textStyle:{                              
                                     color:'#fff'                              
                                   },                   
                         left:'30',
                         top:'100',           
                         data: [ 'Oranges','Apples','Strawberries','Mangoes' ]
                        },
               xAxis: { 
                        data: [ "2006", "2008", "2010", "2012", "2014", "2016" ]
                       },    
               yAxis: { },    
               series: chartdata,          
               backgroundColor:'rgba(0, 0, 0, 1)',          
               textStyle:{
                           color:'#fff'          
                },          
               color:[ '#FF9800','#ef5350','#F48FB1','#81C784','#c4ccd3','#c23531','#2f4554','#61a0a8','#d48265','#91c7ae']
};
myChart.setOption(option);

The HTML Code(index.html):

<!doctype html>
<html>
<head>
<title>Charts BarChart Example</title>
</head>
<body>
<div id="chart" style="width: 100vw; height: 100vh;">
</div>
<script src="http://echarts.baidu.com/dist/echarts.min.js"></script>
<script src="app.js"></script>
</body>
</html>

And here’s the output (Its interactive and you can toggle the series via the legends) :

 

 

Github repo : https://github.com/nizantz/echartsDemo.git

Codepen link : https://codepen.io/nizantz/full/QqgVmZ/

Pretty interesting. I will be playing around more with echarts and vue.

Posted in Charts n Graphs, Generic, JS Frameworks, Web Technology

Switch to Yarn (from npm)

I switched to yarn from npm cause it does everything that npm does (almost everything and more) , only faster.

And by faster I mean, really really fast, like ultrafast.

What is yarn, anyways?
Its a package manager for your code (just like npm) from Facebook.


How to install yarn on your OSX?
brew install yarn
This will also install node.

Use this if you want to skip installing node.
brew install yarn --without-node

You could also use npm itself to install yarn ( and then switch to yarn completely )
npm i -g yarn

 

If you already have yarn and would like to upgrade to the latest version :

brew upgrade yarn

If you are already familiar with npm heres a list of commands that can help you switch to yarn faster:

here will be a table

Example of “yarn why <package>”

Pretty useful command to get package details and info

nsingh$ yarn why babel-core
yarn why v0.27.5
[1/4] Why do we have the module "babel-core"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
info Has been hoisted to "babel-core"
info Reasons this module exists
- "babel-core#babel-register" depends on it
- Specified in "devDependencies"
info Disk size without dependencies: "228kB"
info Disk size with unique dependencies: "8.25MB"
info Disk size with transitive dependencies: "17.06MB"
info Number of shared dependencies: 28
Done in 0.91s

Screenshot of “yarn upgrade-interactive v0.27.5 “

 

 

 

 

 

 

Speed Test : npm vs yarn

npm install took 26 secs

yarn install tool 9.95 secs

(Note : I will be updating this page as and when needed)

 

 

Posted in Generic, Web Technology

Scaffolding Vue Projects – Webpack Template

In this post we will initialize a project using the webpack template (provided out of the box by vuejs.org)

This post assumes that you already have the vue-cli installed.
[ See the previous post if not – > http://www.knowstack.com/scaffolding-vue-projects-simple-template/]

Now we initialize a project using the webpack template:

vue init web pack vue_webpack_demo

Next step is to install the dependencies using npm or yarn (Yarn was much faster):

npm install

OR
yarn install

nsingh$ yarn install
yarn install v0.27.5
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 7.15s.

Once done , start the dev server:

npm start

OR
yarn start

This will launch the app in your browser:

You can open the project in your favorite IDE ( am using atom)  and start adding/modifying content and see the updates immediately in your browser window:

So we have a vue project with webpack template ready to go and work on.

 

Posted in Generic, HTML5, JS Frameworks, VueJS, Web Technology Tagged with: , ,

Hit Counter provided by technology news