2014년 8월 16일 토요일

k-mean clustering

require(graphics) beta1<-c(-0.130811881,-0.124113184,-0.111084035,-0.346505179,0.146765992,-0.203682819) beta2<-c(0.19744896,0.158675025,0.098311959,0.644843941,-0.197478252,0.278922585) beta1<-c(-0.130811881, -0.124113184, -0.111084035, -0.346505179, 0.146765992, -0.201818799, 0.109229194, -0.203682819, -0.124113184, -0.065281704, -0.089614531) beta2<-c(0.19744896, 0.158675025, 0.098311959, 0.644843941, -0.197478252, 0.100779252, -0.204082744, 0.278922585, 0.158675025, 0.099221018, 0.136914875) x <- cbind(beta1, beta2) colnames(x) <- c("x", "y") (cl <- kmeans(x, 2)) plot(x, col = cl$cluster) points(cl$centers, col = 1:2, pch = 8, cex = 2) # sum of squares ss <- function(x) sum(scale(x, scale = FALSE)^2) ## cluster centers "fitted" to each obs.: fitted.x <- fitted(cl); head(fitted.x) resid.x <- x - fitted(cl) ## Equalities : ---------------------------------- cbind(cl[c("betweenss", "tot.withinss", "totss")], # the same two columns c(ss(fitted.x), ss(resid.x), ss(x))) stopifnot(all.equal(cl$ totss, ss(x)), all.equal(cl$ tot.withinss, ss(resid.x)), ## these three are the same: all.equal(cl$ betweenss, ss(fitted.x)), all.equal(cl$ betweenss, cl$totss - cl$tot.withinss), ## and hence also all.equal(ss(x), ss(fitted.x) + ss(resid.x)) ) kmeans(x,1)$withinss # trivial one-cluster, (its W.SS == ss(x)) ## random starts do help here with too many clusters ## (and are often recommended anyway!): (cl <- kmeans(x, 5, nstart = 25)) plot(x, col = cl$cluster) points(cl$centers, col = 1:5, pch = 8)

2014년 7월 28일 월요일

2014년 7월 27일 일요일

텍스트 파일 병합 펄 스크립트

$name = "name.txt"; $formular = "formular.txt"; $A = "A.txt"; $B = "B.txt"; $C = "C.txt"; open $fh_name, '<', $name or die "Cannot open $name: $!"; open $fh_formular, '<', $formular or die "Cannot open $formular: $!"; open $fh_A, '<', $A or die "Cannot open $A: $!"; open $fh_B, '<', $B or die "Cannot open $B: $!"; open $fh_C, '<', $C or die "Cannot open $C: $!"; open $table, '>table.txt'; my $n = @{[<$fh_name>]}; seek $fh_name,0,0; for ($i=1; $i<= $n; $i++){ $l_name = <$fh_name>; chomp($l_name); $l_formular = <$fh_formular>; chomp($l_formular); $l_A = <$fh_A>; chomp($l_A); $l_B = <$fh_B>; chomp($l_B); $l_C = <$fh_C>; chomp($l_C); print $table "$l_name\t$l_formular\t$l_A\t$l_B\t$l_C\n"; }

2014년 7월 23일 수요일

이클립스 프로젝트 SWT 예제

1.[File]-[New]-[Java Project]


[New]-[Class]

소스코드 입력

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * @author Steven Holzner
 * 
 */
public class SWTButtonExampleDemo {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setText("Button Example");
    shell.setLayout(new RowLayout());

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Click Me");

    final Text text = new Text(shell, SWT.SHADOW_IN);

    button.addSelectionListener(new SelectionListener() {

      public void widgetSelected(SelectionEvent event) {
        text.setText("No worries!");
      }

      public void widgetDefaultSelected(SelectionEvent event) {
        text.setText("No worries!");
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}



2. [Project]-[Java Build Path]-[Libraries]-[Add External JARs]

2014년 7월 8일 화요일

[c#] 엑셀 파일 읽기

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

namespace ReadExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\\C.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

    }
}

클릭하면 div를 보여주는 jquery 소스

<button>Toggle</button>
<div id="select" style="display: none">
<p>Such interesting text, eh?</p>
</div>
<div>this is second para</div>
</body>

<script>
$( "button" ).click(function() {
$('#select').toggle( "slow" );
});
</script>

2014년 6월 28일 토요일

[c#]1분 간격으로 이벤트 실행

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;

namespace sendgmail
{
    public partial class Form1 : Form
    {
        static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
       
        public Form1()
        {
            InitializeComponent();

            timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
            timer.Interval = 60 * 1000;
            timer.Enabled = true;                       // Enable the timer
            timer.Start();    

  
        }

        // 쓰레드풀의 작업쓰레드가 지정된 시간 간격으로
        // 아래 이벤트 핸들러 실행
        private void timer_Tick(object sender, EventArgs e)
        {
            // MessageBox.Show("timer success");

        }

    }
}