calu函数python calu函数c语言

求n! , n由用户从键盘输入,其中子函数使用迭代方式来实现 提示使用函数public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
Integer number = scanner.nextInt();
System.out.println("您输入的整数为:" + number + "正在为您计算阶乘 。。。");
Integer integer=caluater(number);
System.out.println("您输入的整数为:" + number + "阶乘为:"+integer);
}
private static Integer caluater(Integer number) {
int i = 1;
Integer sum = 0;
if(i==number){//等于1的时候跳出循环
return 1;
}else {
sum = number * caluater(number - 1);//递归调用
return sum;
}
}
}
C语言编程在主函数中输入一个字符串利用函数求得字符串中大写字母小写字母数字字符空格及其他字符的个数#include stdio.h
#include ctype.h
int main(void)
{
char ch[100];
void count(char * p);
printf("请输入字符串 : ");
gets(ch);
count(ch);
return 0;
}
void count(char * p)
{
int upp=0, low=0, digi=0, spa=0, oth=0;
for (int i = 0; p[i]; ++i)
{
if (isupper(p[i]))
upp++;
else if (islower(p[i]))
low++;
else if (isspace(p[i]))
spa++;
else if (isdigit(p[i]))
digi++;
else
oth++;
}
printf("大写 = %d\n小写 = %d\n空格 = %d\n数字 = %d\n其calu函数python他 = %d\n", upp, low, digi, spa, oth);
}
如何用Python实现支持向量机#SVM.py
from numpy import *
import time
import matplotlib.pyplot as plt
# calulate kernel value
def calcKernelValue(matrix_x, sample_x, kernelOption):
kernelType = kernelOption[0]
numSamples = matrix_x.shape[0]
kernelValue = https://www.04ip.com/post/mat(zeros((numSamples, 1)))
if kernelType == 'linear':
kernelValue = https://www.04ip.com/post/matrix_x * sample_x.T
elif kernelType == 'rbf':
sigma = kernelOption[1]
if sigma == 0:
sigma = 1.0
for i in xrange(numSamples):
diff = matrix_x[i, :] - sample_x
kernelValue[i] = exp(diff * diff.T / (-2.0 * sigma**2))
else:
raise NameError('Not support kernel type! You can use linear or rbf!')
return kernelValue
# calculate kernel matrix given train set and kernel type
def calcKernelMatrix(train_x, kernelOption):
numSamples = train_x.shape[0]
kernelMatrix = mat(zeros((numSamples, numSamples)))
for i in xrange(numSamples):
kernelMatrix[:, i] = calcKernelValue(train_x, train_x[i, :], kernelOption)
return kernelMatrix
# define a struct just for storing variables and data
class SVMStruct:
def __init__(self, dataSet, labels, C, toler, kernelOption):
self.train_x = dataSet # each row stands for a sample
self.train_y = labels# corresponding label
self.C = C# slack variable
self.toler = toler# termination condition for iteration
self.numSamples = dataSet.shape[0] # number of samples
self.alphas = mat(zeros((self.numSamples, 1))) # Lagrange factors for all samples
self.b = 0
self.errorCache = mat(zeros((self.numSamples, 2)))
self.kernelOpt = kernelOption
self.kernelMat = calcKernelMatrix(self.train_x, self.kernelOpt)
# calculate the error for alpha k
def calcError(svm, alpha_k):
output_k = float(multiply(svm.alphas, svm.train_y).T * svm.kernelMat[:, alpha_k] + svm.b)
error_k = output_k - float(svm.train_y[alpha_k])
return error_k
# update the error cache for alpha k after optimize alpha k
def updateError(svm, alpha_k):
error = calcError(svm, alpha_k)
svm.errorCache[alpha_k] = [1, error]
# select alpha j which has the biggest step

推荐阅读