<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Identify North Atlantic Winter Weather Regimes by K-means Clustering
The four weather regimes typically found over the North Atlantic in winter are identified as
* NAO+ (positive NAO) 
* NAO- (negative NAO)
* Blocking 
* Atlantic Ridge.
Each weather regime is associated with different climatic conditions over Europe and North America (Cassou, 2008). In particular, the negative NAO and the blocking regimes are generally associated with cold extreme temperatures over Europe and the eastern United States (US) (Yiou and Nogaj 2004).
The North Atlantic winter weather(DJF) regimes are computed using a k-mean clustering algorithm applied to the monthly anomalies of the 500 hPa geopotential height (Z500) on the NCEP/NCAR reanalysis. The monthly anomalies are with respect to the 1979–2010 climatology  and are computed over the [90W/60E; 20/80N] domain.
k-means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. k-means clustering aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. This results in a partitioning of the data space into Voronoi cells (https
Step1:  2. Load data
Step2:  3. Perform KMeans clustering to idenfity weather regimes
It is worth noting that sklearn.cluster.KMeans only support dimensions<=2. Have to convert 3D (time|lat|lon) data into 2D (time|lat*lon) using numpy.reshape. When visualizing the final identified cluster_centers(i.e., weaterh regions), have to convert them back from 1D to 2d spatial format (lat|lon).
Step3:  Get the fraction of a given cluster denoted by label.
Defaultly the labeles is 0 to n_clusters-1. In this case, should be 0, 1, 2, 3.
Step4:  4. Visualize weather regimes
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs
from sklearn.cluster import KMeans
Explanation: Identify North Atlantic Winter Weather Regimes by K-means Clustering
The four weather regimes typically found over the North Atlantic in winter are identified as
* NAO+ (positive NAO) 
* NAO- (negative NAO)
* Blocking 
* Atlantic Ridge.
Each weather regime is associated with different climatic conditions over Europe and North America (Cassou, 2008). In particular, the negative NAO and the blocking regimes are generally associated with cold extreme temperatures over Europe and the eastern United States (US) (Yiou and Nogaj 2004).
The North Atlantic winter weather(DJF) regimes are computed using a k-mean clustering algorithm applied to the monthly anomalies of the 500 hPa geopotential height (Z500) on the NCEP/NCAR reanalysis. The monthly anomalies are with respect to the 1979–2010 climatology  and are computed over the [90W/60E; 20/80N] domain.
k-means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. k-means clustering aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. This results in a partitioning of the data space into Voronoi cells (https://en.wikipedia.org/wiki/K-means_clustering). 
sklearn.cluster.KMeans is used in this notebook.
1. Load all needed libraries
End of explanation
z500 = xr.open_dataset('data\z500.DJF.anom.1979.2010.nc', decode_times=False)
print(z500)
da = z500.sel(P=500).phi.load()
print(da.name, da.dims)
print(da.coords)
Explanation: 2. Load data
End of explanation
data = da.values
nt,ny,nx = data.shape
data = np.reshape(data, [nt, ny*nx], order='F')
mk = KMeans(n_clusters=4, random_state=0, n_jobs=-1).fit(data)
Explanation: 3. Perform KMeans clustering to idenfity weather regimes
It is worth noting that sklearn.cluster.KMeans only support dimensions<=2. Have to convert 3D (time|lat|lon) data into 2D (time|lat*lon) using numpy.reshape. When visualizing the final identified cluster_centers(i.e., weaterh regions), have to convert them back from 1D to 2d spatial format (lat|lon).
End of explanation
def get_cluster_fraction(m, label):        
        return (m.labels_==label).sum()/(m.labels_.size*1.0)
Explanation: Get the fraction of a given cluster denoted by label.
Defaultly the labeles is 0 to n_clusters-1. In this case, should be 0, 1, 2, 3.
End of explanation
x,y = np.meshgrid(da.X, da.Y)
proj = ccrs.Orthographic(0,45)
fig, axes = plt.subplots(2,2, figsize=(8,8), subplot_kw=dict(projection=proj))
regimes = ['NAO$^-$', 'NAO$^+$', 'Blocking', 'Atlantic Ridge']
tags = list('abcd')
for i in range(mk.n_clusters):
    onecen = mk.cluster_centers_[i,:].reshape(ny,nx, order='F')
    cs = axes.flat[i].contourf(x, y, onecen,
                               levels=np.arange(-150, 151, 30), 
                               transform=ccrs.PlateCarree(),
                               cmap='RdBu_r')
    
    cb=fig.colorbar(cs, ax=axes.flat[i], shrink=0.8, aspect=20) 
    cb.set_label('[unit: m]',labelpad=-7)    
    axes.flat[i].coastlines()
    axes.flat[i].set_global()
  
    title = '{}, {:4.1f}%'.format(regimes[i], get_cluster_fraction(mk, i)*100)
    axes.flat[i].set_title(title)
    plt.text(0, 1, tags[i], 
             transform=axes.flat[i].transAxes, 
             va='bottom', 
             fontsize=plt.rcParams['font.size']*2, 
             fontweight='bold')   
Explanation: 4. Visualize weather regimes
End of explanation
<END_TASK>