Monday, December 16, 2019
Examples of Highmaps
<script src='https://code.highcharts.com/maps/highmaps.js'></script>
<script src='https://code.highcharts.com/modules/exporting.js'></script>
<script src='https://code.highcharts.com/modules/offline-exporting.js'></script>
<script src='https://code.highcharts.com/mapdata/custom/europe.js'></script>
These scripts were added in the page head.
Text Classification
20newsgroups.ipynb
import pylab; from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neural_network import MLPClassifier
import warnings; warnings.filterwarnings('ignore')
train=fetch_20newsgroups(
subset='train',shuffle=True,remove=('headers','footers','quotes'))
test=fetch_20newsgroups(
subset='test',shuffle=True,remove=('headers','footers','quotes'))
y_train,y_test=train.target,test.target
vectorizer=TfidfVectorizer(
sublinear_tf=True,max_df=.5,stop_words='english')
x_train=vectorizer.fit_transform(train.data)
x_test=vectorizer.transform(test.data)
del train,test
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
clf=MLPClassifier(
hidden_layer_sizes=(128,),max_iter=3,solver='adam',
verbose=2,random_state=1,learning_rate_init=.01)
clf.fit(x_train,y_train); print(clf.score(x_test,y_test))
y_test_predictions=clf.predict(x_test)
pylab.figure(figsize=(6,4))
pylab.scatter(range(100),y_test[:100],s=100)
pylab.scatter(range(100),y_test_predictions[:100],s=25)
pylab.tight_lauoyt(); pylab.show()
Fun Plotly
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script> was added in the page head.
Additional Python Modules 2
russian-notes-pythondataanalysis-2-40.ipynb
You can install additional packages
!python3 -m pip install --upgrade pip \
--user --quiet --no-warn-script-location
!python3 -m pip install pygal \
--user --quiet --no-warn-script-location
And apply them
import pygal,pandas as pd
from IPython.display import SVG, display
from pygal.style import BlueStyle
columns=['Fresh','Milk','Grocery','Frozen',
'Detergents_Paper','Delicatessen']
colors=['#1b2c45','#5a8bbd','#008b8b','#ff5a8b']
index= ['C1','C2','C3']
data=[[26373,36423,22019,5154,4337,16523],
[16165,4230,7595,201,4003,57],
[14276,803,3045,485,100,518]]
samples=pd.DataFrame(data,columns=columns,index=index)
line=pygal.Line(
fill=False,height=int(400),width=int(900),
style=BlueStyle(
opacity='.3',colors=colors,background='transparent'))
line.title='Samples of the Wholesale Customers Dataset'
line.x_labels=columns
line.add('C1',data[0]); line.add('C2',data[1])
line.add('C3',data[2]); line.add('MEAN',samples.mean())
line.render_to_file('samples.svg')
display(SVG(open('samples.svg','rb').read()))
Additional Python Modules
russian-notes-pythondataanalysis-2-40.ipynb
import pandas,geopandas,pylab,warnings,fiona
fiona._env.set_gdal_config('OGR_GEOMETRY_ACCEPT_UNCLOSED_RING','YES')
fiona._env.logging.disable(level='WARNING')
warnings.filterwarnings('ignore',category=FutureWarning)
pylab.style.use('fivethirtyeight')
fpath='https://raw.githubusercontent.com/OlgaBelitskaya/'+\
'OlgaBelitskaya.github.io/master/map.geojson'
df=geopandas.GeoDataFrame.from_file(fpath)
ax=df.boundary.plot(
figsize=(12,7),markersize=0,linewidth=.1,edgecolor='gray')
footways=df['geometry'][df['highway']=='footway'].loc[2176:]
geopandas.plotting.plot_linestring_collection(
ax,footways,linewidth=.8,
color='steelblue',label='footways')
df[df['highway']=='street_lamp'].plot(
ax=ax,edgecolor='g',markersize=10,label='lamps')
df[df['highway']=='traffic_signals'].plot(
ax=ax,edgecolor='r',markersize=20,label='traffic signals')
pylab.xlim(37.613,37.636); pylab.ylim(55.752,55.762)
pylab.legend(); pylab.tight_layout(); pylab.show()
Functions with Summation
<script src='https://code.highcharts.com/highcharts.js'></script>
was added in the page head.
Artificial Clusters
makeCluster[class_,\[Mu]_,\[Rho]_]:=
RandomVariate[MultinormalDistribution[\[Mu],{{1,4\[Rho]},
{4 \[Rho],4}}],500]->class;
clusters=makeCluster@@@{{1,{1.5,1.8},-.2},{2,{-1.5,1.1},.3},
clusters=makeCluster@@@{{1,{1.5,1.8},-.2},{2,{-1.5,1.1},.3},
{3,{-1.6,-2.3},-.4},{4,{1.8,-2.2},.1}};
lp1=ListPlot[Keys[clusters],PlotStyle ->"BrightBands"];
clf=Classify[Flatten[Thread/@clusters],Method->"NearestNeighbors"]
sample={{2.5,2.8},{-1.7,1.5},{-0.9,-2.1},{1.6,-1.8}};
lp2=ListPlot[List/@sample,
lp1=ListPlot[Keys[clusters],PlotStyle ->"BrightBands"];
clf=Classify[Flatten[Thread/@clusters],Method->"NearestNeighbors"]
sample={{2.5,2.8},{-1.7,1.5},{-0.9,-2.1},{1.6,-1.8}};
lp2=ListPlot[List/@sample,
PlotMarkers->clf[sample],PlotStyle->Black];
Show[lp1,lp2,ImageSize->Large,PlotRange->{{-5,5},{-5,5}}]
Show[lp1,lp2,ImageSize->Large,PlotRange->{{-5,5},{-5,5}}]
HTML Objects
EmbeddedHTML["<ul style='list-style-type:circle;'>
<li style='color:#3636ff;'>Coffee</li>
<li style='color:#36ff36;'>Tea</li>
<li style='color:#ff3636;'>Milk</li></ul>"]
EmbeddedHTML["<svg width='150' height='150'>
<circle style='fill:steelblue; stroke:silver;'
stroke-width='5' cx='75' cy='75' r='50'/></svg>"]
Logical Functions
bc=Tuples[{False,True},10]; Length[bc]
LF=Function[{x1,x2,x3,x4,x5,x6,x7,x8,x9,x10},
f1=((x1||(! x2))&&(x3||(!x4))); f2=((x3||(!x4))&&(x5||(!x6)));
f3=((x5||(!x6))&&(x7||(!x8))); f4=((x7||(! x8))&&(x9||(! x10)));
f1&&f2&&f3&&f4];
LFvalues=Table[Apply[LF,bc[[i]]],{i,Range[1,Length[bc],1]}];
Count[LFvalues,True]
1024
243
Integration & Plotting
G[x_]=Integrate[Cos[x^2]*Exp[x],x,GeneratedParameters -> C];
Plot[Evaluate[{G[x]/.Table[{C[1]->i},{i,Range[0,10,5]}]}],
{x,3,5},PlotTheme->"Detailed",ImageSize->Large]
Plot[Evaluate[{G[x]/.Table[{C[1]->i},{i,Range[0,10,5]}]}],
{x,3,5},PlotTheme->"Detailed",ImageSize->Large]
Limits & Plotting
F[x_,y_]=(1-Sqrt[1-x^2+x*y^3-y^4])/Sin[x^2+y^4];
FL[x_,y_]=Limit[F[x,y],{x,y}->{0,0}];
Plot3D[{F[x,y],FL[x,y]},{x,-1/4,1/4},{y,-1/4,1/4},PlotRange->All,
PlotStyle->{Opacity[0.9],Opacity[0.4]},PlotTheme->"Web",Mesh->False,
PlotLabel->"Limits for Functions with Two Variables",
Boxed->False,Axes->False,ImageSize->Large]
Interactive Plotting with Parameters
Manipulate[Show[Table[Plot[Sin[t-2*l]-t^2,{t,-1,1},
PlotStyle->Hue[Sin[l/L]]],{l,Range[-1.25,1.25,0.05]}],
PlotRange->All,Axes->False,ImageSize->Large],{L,Range[1,10,1]}]
Sets of Mesh Surfaces
k=7; f1[x_,y_,z_]=x^2/9+y^2/16-z^2/25;
f2[x_,y_,z_]=x^2/4+y^2/9-z^2/16;
cp1=ContourPlot3D[f1[x,y,z]==1,{x,-k,k},{y,-k,k},{z,-k,k},
Mesh->20,MeshStyle->Blue,ContourStyle->{Blue,Opacity[0.1]}];
cp2=ContourPlot3D[f2[x,y,z]==0,{x,-k,k},{y,-k,k},{z,-k,k},
Mesh->30,MeshStyle->Red,ContourStyle->{Red,Opacity[0.2]}];
Show[cp1,cp2,Boxed->False,Axes->False,ImageSize->Large]
Colorized Curves
Show[Table[Plot[Sin[x],{x,i*Pi/24,(i+1)*Pi/24},
PlotStyle->{Hue[i/48],Thickness[0.02]}],{i,Range[0,47,1]}],
ImageSize->Large,GridLines->Automatic,PlotRange->All]
Texts & Strings
corpus ={"Have you already set your goals for the New Year?",
"Do you want to lose ten kilos, run a marathon or speak fluent English?",
"Some experts believe that you need systems, not goals.",
"A system is something you do on a regular basis.",
"This means focusing on what you can control (your actions) rather than what you can not.",
"For example, do not focus on losing ten kilos.",
"Focus on shopping for healthy food and cooking something light every day.",
"Do not focus on the marathon.","Focus on the training schedule.",
"Invent a system to improve your English, one step at a time.","Good luck!"};
wcorpus=StringReplace[StringJoin[corpus], PunctuationCharacter :> " "];
wc=KeySort[WordCounts[ToLowerCase[wcorpus]]]; fc=[]
WordCloud[wcorpus,fc,WordOrientation->"Random",PlotTheme->"Web"]
BarChart[wc,ChartLabels->Placed[Normal[Keys[wc]],Axis,Rotate[#,Pi/2] &],
ChartStyle->"Pastel",ChartElementFunction->"GlassRectangle",ImageSize->600]
Subscribe to:
Posts (Atom)