您的位置:首页 > 编程语言

双色球特征分析代码实现

2012-11-05 18:17 274 查看
个人闲来无事写了一段代码来分析时下流行的双色球。

1 中彩网下载双色球所以历史数据写入MYSQL形成0 1码表

2 分析分开中的号码特征码,再提取下次开奖的33个红球特征码加入分析引擎进行分析。主要代码入下希望有此兴趣的大侠一起研究》

/// <summary>
/// 提取表中所有特征码
/// </summary>
/// <param name="dtTable"></param>
/// <returns></returns>
public List<Features> GetAllFeatures(DataTable dtTable,int featuresSize)
{
//特征集合
List<Features> listFeatures = new List<Features>();
//检查需要提取的特征码
for (int i = featuresSize+1; i < dtTable.Rows.Count; i++)
{
for (int c = 0; c < dtTable.Columns.Count; c++)
{
//只有为1时才提取
if (dtTable.Rows[i][c].ToString() == "1")
{
//提取特征
listFeatures.Add(GetFeatures(i,c,featuresSize,dtTable));
}
}
}
return listFeatures;
}

/// <summary>
/// 从指定DataTable中提取特征码
/// </summary>
/// <param name="row">定位的行</param>
/// <param name="col">定位的列</param>
/// <param name="size">长宽</param>
/// <param name="dtTable">按时间升序排列(只保留当前号码列数)</param>
/// <returns></returns>
public Features GetFeatures(int row, int col, int size, DataTable dtTable)
{
//定义特征类型
Features features = new Features(size, size);
//按行列定位到指定单元格再取以上行的size长方体
for (int s = size; s > 0; s--)
{
//数组的X定位
int arrX = s - 1;
int rowIndex = row - s;
if (rowIndex <= 0) continue;
//先定位行
DataRow dr = dtTable.Rows[rowIndex];
//取列 分别为定位点的两侧的col/2个
int colSize = col / 2;
for (int c = -colSize; c <= colSize; c++)
{
//数组的X定位
int arrY = colSize + c;
//定位列
int colIndex = col + c;
//左边溢出
if (colIndex < 0)
{
//如为-2则定位到33-2=31位置
colIndex = dtTable.Columns.Count + colIndex;
}
//右边溢出
else if (colIndex > dtTable.Columns.Count - 1)
{
//如为34则定位到33-32=1位置
colIndex = colIndex - dtTable.Columns.Count;
}
//写入特征数组
features.features[arrX, arrY] = int.Parse(dr[colIndex].ToString());
}
}
//返回特征
return features;
}

/// <summary>
/// 比较两个特征相似性(只取值为1的,为0暂不做对比)
/// </summary>
/// <param name="sourceFeatures">源特征,作为对比的参数</param>
/// <param name="targetFeatures">目标特征,参与对比的特征</param>
/// <returns>返回双精度</returns>
public double FeaturesCompare(Features sourceFeatures, Features targetFeatures)
{
//源特征开总中次数
int totalCount = 0;
//目标特征跟源特征开中的相似个数
int comparaCount = 0;
for (int i = 0; i < sourceFeatures.arrX; i++)
{
for (int c = 0; c < sourceFeatures.arrY; c++)
{
//源特征为1
if (sourceFeatures.features[i, c] == 1)
{
//目标特征为1
if (targetFeatures.features[i, c] == 1)
{
//相似个数加1
comparaCount++;
}
//总数加1
totalCount++;
}
}
}
//返回特征相似性比率
return Convert.ToDouble(comparaCount) / Convert.ToDouble(totalCount);
}
}
/// <summary>
/// 特征实体类
/// </summary>
public class Features
{
//定义二维数据长高度
int x = 0;
int y = 0;
int[,] baseFeatures;
//构造特征二维数组
public Features(int ax, int ay)
{
x = ax;
y = ay;
baseFeatures = new int[x, y];
}
//Get方法
public int[,] features
{
get { return baseFeatures; }
}
//返回数据长度
public int arrX
{
get { return x; }
}
//返回数据高度
public int arrY
{
get { return y; }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: