A little rookie front-end code farmer

To prepare data

export const data = [
  {
    data: 2018009,
    value: [[5.10.17.23.26.32], [7]]
  },
  {
    data: 2018010,
    value: [[1.8.17.20.21.22], [3]]
  },
  {
    data: 2018011,
    value: [[3.10.21.23.27.33], [11]]
  },
  {
    data: 2018012,
    value: [[11.12.13.19.26.28], [12]]}];Copy the code

The core code

import { Component, OnInit } from "@angular/core";
import { data } from ".. /.. /data/shuangseqiu";
import * as tf from "@tensorflow/tfjs";

@Component({
  selector: "shuangseqiu",
  templateUrl: "./shuangseqiu.component.html",
  styleUrls: ["./shuangseqiu.component.scss"]})export class ShuangseqiuComponent implements OnInit {
  items: any[] = data;
  redResults: number[] = [0.0.0.0.0.0];
  blowResults: number[] = [0];

  constructor() {}

  ngOnInit() {
    this.handler();
  }

  handler() {
    // Create the model
    let model = tf.sequential();
    model.add(tf.layers.dense({ units: 7, inputShape: [7]})); model.compile({ loss:"meanSquaredError", optimizer: "SGD" });
    // Format the data
    let r = this.formatData();
    // Enter data
    let x = tf.tensor(r.input);
    // Output data
    let y = tf.tensor(r.output);
    model.compile({ loss: "meanSquaredError", optimizer: "sgd" });
    // The training model
    model.fit(x, y);
    let u = tf.tensor(r.use);
    / / forecast
    (<tf.Tensor>model.predict(u)).data().then((res: any) = > {
      res.map((r, index) = > {
        if (index < 6) {
          let re = Math.abs(parseInt(r));
          // Red ball data secondary detection
          if (re === 0) {
            re = 1;
          }
          if (re > 35) {
            re = 35;
          }
          this.redResults[index] = re;
        } else {
           // Blue ball data secondary detection
          let re = Math.abs(parseInt(r));
          if (re === 0) {
            re = 1;
          }
          if (re > 16) {
            re = 16;
          }
          this.blowResults[0] = re; }});// Sort the results
      this.redResults.sort((a, b): any= > {
        return a > b;
      });
    });
  }

  formatData() {
    let handedXs = [];
    let handedYs = [];
    let use = [];
    this.items.map((res, index) = > {
      if(index ! = =0) {
        let m = [...res.value[0], ...res.value[1]];
        handedYs.push(m);
      } else {
        let m = [...res.value[0], ...res.value[1]];
      }
      if(index ! = =this.items.length - 1) {
        let m = [...res.value[0], ...res.value[1]];
        handedXs.push(m);
      } else {
        let m = [...res.value[0], ...res.value[1]]. use.push(m); }});return{ input: handedXs, output: handedYs, use: use }; }}Copy the code

Build HTML to display data

<div class="card">
  <div class="card-header">Bicolor prediction<a style="float:right; color: #fff;" class="btn btn-primary btn-sm">add</a>
  </div>
  <div class="card-body">
    <table class="table table-hover">
      <thead>
        <tr>
          <th>issue</th>
          <th>1 red ball</th>
          <th>The red ball</th>
          <th>Three red balls</th>
          <th>Red ball 4</th>
          <th>5 red balls</th>
          <th>Red balls and 6</th>
          <th>basketball</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let item of items; index as i;">
          <td>{{item.data}}</td>
          <td *ngFor="let it of item.value[0]">
            <span class="red">{{it}}</span>
          </td>
          <td *ngFor="let it of item.value[1]">
            <span class="blow">{{it}}</span>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
<div class="card">
  <div class="card-header">Predict the results of the next period<a style="float:right; color: #fff;" class="btn btn-primary btn-sm" (click) ="handler()">recalculate</a>
  </div>
  <div class="card-body">
    <div class="flex-row">
      <span class="red" *ngFor="let item of redResults">{{item}}</span>
      <span class="blow" *ngFor="let item of blowResults">{{item}}</span>
    </div>
  </div>
</div>
Copy the code