Saturday, December 28, 2019

Dimensionality Reduction as Exercises 2


digits= ExampleData[{"MachineLearning","MNIST"},
"Data"]; sample=RandomSample[digits,3000];
Delete[digits]; sample[[1;;7]]
features=DimensionReduce[sample[[All,1]],
2,Method->"AutoEncoder"];
ListPlot[MapThread[Style[Labeled[#1,#2],
Hue[.1Values[#2]]]&,{features[[1;;320]],
sample[[1;;320]]}],ImageSize->600]


Regression as Exercises


train=ExampleData[{"MachineLearning",
"BostonHomes"},"TrainingData"];
test=ExampleData[{"MachineLearning",
"BostonHomes"}, "TestData"];
MLP=NetChain[{LinearLayer[13*4],
BatchNormalizationLayer[],ElementwiseLayer[Ramp],
LinearLayer[512],BatchNormalizationLayer[],
ElementwiseLayer[Ramp],LinearLayer[1]},
"Input"->13,"Output"->"Scalar" ]
results=NetTrain[MLP,train,All,
ValidationSet->test,MaxTrainingRounds->500]
{TrainedMLP,ValLosses}=results[{"TrainedNet",
"ValidationLossList"}]; Min[ValLosses]


Predictions= Predict[train,
Method->"GradientBoostedTrees",PerformanceGoal->"Quality"]
testpreds=Predictions[test[[All,1]]];
ListLinePlot[{test[[All,2]],testpreds},ImageSize->600,
PlotLegends->{"Real Data","Predictions"},
PlotLabel->{"Mean Square"->PredictorMeasurements[Predictions,
test,"MeanSquare"]}]

Dimensionality Reduction as Exercises


digits= ExampleData[{"MachineLearning","MNIST"},
"Data"];sample=RandomSample[digits,2000];
Delete[digits];sample[[1;;7]]
features=DimensionReduce[sample[[All,1]],2,
Method->"TSNE",PerformanceGoal->"Quality"];
bydigits=GroupBy[Thread[features->sample[[All,2]]],
Last->First];
ListPlot[Values[bydigits],PlotLegends->Keys[bydigits],
ImageSize->600,AspectRatio->1,Frame->True]


Graphics3D Exercises


TK[k_]:={{k,-k,k},{0,0,0},{k,k,k},
{-k,k,k},{-k,-k,k},{k,-k,k},
{k,k,k},{k,k,-k},{-k,k,-k},
{-k,-k,-k},{k,-k,-k},{k,k,-k},
{0,0,0},{k,-k,-k},{k,-k,k}};
Graphics3D[Table[{FaceForm[{ColorData["BrightBands"][.07i],
Opacity[.6]}],Tube[TK[1][[i;;i+1]],.1],
Opacity[1],Sphere[TK[1][[i]],.15]},{i,14}],
Boxed->False,Background->Black,
ViewPoint->{3,-1,0},ImageSize->500]


Examples of Shadow Effects


PPK[a_,b_,c_]:=Module[{ppt,pps},
ppt=Table[{c*Sin[a*t]^2,b*Cos[b*t]^3,
a*Sin[c*t]*Log[t+1]},{t,0,6Pi,.05}];
pps=GeometricTransformation[Tube[ppt,.1],
RotationTransform[Pi,{0, 0, 1}]];
Show[Graphics3D[{FaceForm[{Darker[Gray],Opacity[.3]}],pps}],
Graphics3D[{FaceForm[{Blue,Opacity[.9]}],Tube[ppt,.1]}],
Boxed->False,Background->RGBColor["silver"],
AspectRatio->1,ImageSize->500]];
PPK[4,3,2]


PPTS:=Function[{a,b,c},
img=Graphics3D[{RGBColor["#3636ff"],
Tube[Table[{c*Sin[a*t]^2,b*Cos[b*t]^3,
a*Sin[c*t]*Log[t+1]},{t,0,6Pi,.03}],.1]},
Boxed->False,AspectRatio->1,PlotRange->All,
ImageSize->{600,500},Background->RGBColor["silver"]];
ImageCompose[Lighter@Blur[img,10],SetAlphaChannel[img,
ColorNegate[img]],Scaled[{.38,.52}]]]
PPTS[4,3,2]


RotationMatrix Applying


RGS[n_,m_]:=Module[{rt,rm},
rt=RandomReal[{-1,1},{n,2}];rm=RotationMatrix[Pi/m];
Graphics[{Opacity[.4],Style[Polygon[rt.MatrixPower[rm,#]],
ColorData["BrightBands"][1-#/(2m)]]&/@Range[2m]},
Background->RGBColor["silver"],ImageSize->600]];
  RGS[128,6]


Parameter Influence


FP1[x_,k_]:=(.12k*x)^2-12x^2+((.12k)^2-3^1.5*.12k+6)*x;
FP2[k_]:=(.12k)^2-3^0.5*(.12k)-6;
Show[Table[Plot[FP1[x,k]+FP2[k],{x,-2,2},PlotStyle->Hue[k/32],
PlotLegends->Hue[k/32]->.12k],{k,Range[0,31,1]}],
PlotRange->{{-2,2},{-8,2}},Frame->True,GridLines->Automatic,
ImageSize->500,AspectRatio->1,Background->Lighter[Gray,.8]]


NestList Examples


NL=NestList[{Sin[2#1[[2]]]-Cos[3#1[[1]]],
Sin[2#1[[1]]]+Cos[#1[[2]]]}&,{-.5,.5},10^5];
ListPlot[NL,Axes->False,PlotStyle->Opacity[.1],
ColorFunction->Function[{x,y},Hue[x*y]],
ImageSize->500,AspectRatio->1,
Background->Lighter[Gray,.7]]


ReliefPlot Examples


With[{z=x+I y},ReliefPlot[Table[Cos[64#],
{x,##2},{y,##2}]&[If[Abs[z]>1.1 ,
0,#]&@Arg[Sum[(z(z^9+.01))^k!,
{k,4}]],-1.11,1.11,.005],
ImageSize->500,ColorFunction->"CoffeeTones"]]


Applying Accumulation


CL[k_,n_,alpha_]:=Accumulate[z=1;
Table[z=k*E^(#[[i]]I)z,
{i,n}]]&/@Tuples[{-alpha,alpha},n];
GCL=Function[{k,n,alpha},
Graphics[Table[{ColorData["CherryTones"][i/4],
Rotate[BSplineCurve@Transpose@{Re@#,
Im@#}&/@CL[k,n,alpha],i*Pi/2]},{i,4}],
ImageSize->400,Background->Lighter[Gray,.7]]];
{GCL[.7,10,Pi/4],GCL[.8,8,Pi/6]}


Examples of Dynamic Modules


DynamicModule[{cols,funs},
cols=RGBColor/@{"#3636ff","#ff3636"};
funs[y_,z_]:={{(1-z^4-Abs[y]^1.5)^2,y,z},
{-(1-z^4-Abs[y]^1.5)^2,y,z}};
Manipulate[Show[Table[ParametricPlot3D[funs[y,z],
{y,-(1 -z^4)^2,(1-z^4)^2},PlotRange->1,
Boxed->False,Axes->False,PlotPoints->100,
SphericalRegion->False,Background->RGBColor["#360036"],
PlotStyle->Blend[cols,(z+1)/2],ImageSize ->500,
ViewPoint->65 {1,0,1},ViewAngle ->Pi/256],
{z,-1+t,1-.001,.1}]],{t,.001,.1}]]