比如说,我正在使用朴素贝叶斯在 R 中开发机器学习模型。所以我将使用 naiveBayes 包构建一个模型,如下所示
model <- naiveBayes(Class ~ ., data = HouseVotes84)
Run Code Online (Sandbox Code Playgroud)
我还可以通过打印模型来打印模型的权重。
我按如下方式进行预测,这给了我一个作为预测的类
predict(model, HouseVotes84[1:10,], type = "raw")
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是,有没有办法查看哪些列对这个预测影响最大?因此,我可以了解导致学生不及格的最重要因素是什么,例如,如果这是响应变量,而各种可能的因素是其他预测变量列。
我的问题是对于 R 中的任何包,上面的 naiveBayes 只是一个例子。
这是我的计数排序的两个实现
在这个非常简单的实现中,我所做的就是计算元素出现的次数,并在输出数组中插入与出现次数相同的次数。实施1
public class Simple
{
static int[] a = {5,6,6,4,4,4,8,8,8,9,4,4,3,3,4};
public static void main(String[] args)
{
fun(a);
print(a);
}
static void fun(int[] a)
{
int max = findMax(a);
int[] temp = new int[max+1];
for(int i = 0;i<a.length;i++)
{
temp[a[i]]++;
}
print(temp);
//print(temp);
int k = 0;
for(int i = 0;i<temp.length;i++)
{
for(int j = 0;j<temp[i];j++)
a[k++] = i;
}
print(a);
}
static int findMax(int[] a)
{
int max = a[0];
for(int i= 1;i<a.length;i++)
{
if(a[i] > max)
max = …
Run Code Online (Sandbox Code Playgroud) 我已尝试过以下内容,但是,当字符串包含任何其他字符(例如空格)时,它会出错.正如您在下面看到的,有一个名为"subway 10"的字符串,它包含数字字符,但由于空间的原因,它被报告为false.
我的字符串可能包含任何其他字符,但如果它包含至少一个数字,我想从数组中获取这些字符串的索引.
> mywords<- c("harry","met","sally","subway 10","1800Movies","12345")
> numbers <- grepl("^[[:digit:]]+$", mywords)
> letters <- grepl("^[[:alpha:]]+$", mywords)
> both <- grepl("^[[:digit:][:alpha:]]+$", mywords)
>
> mywords[xor((letters | numbers), both)] # letters & numbers mixed
[1] "1800Movies"
Run Code Online (Sandbox Code Playgroud) 我读了一些文章说,在堆中,只能删除根元素.但是,为什么我们不能使用以下方法删除元素?
所以,代码看起来像
static void delete(int[] a, int key)
{
int i = 0;
for(i = 0;i<a.length;i++)
{
if(a[i] == key)
break;
}
swap(a, i, a.length-1);
max_heapify_forheapsort(a, i, a.length-2);
}
static void max_heapify_forheapsort(int[] a, int i, int limit)
{
int left = (2*i)+1;
int right = (2*i) + 2;
int next = i;
if(left <= limit && a[left] > a[i])
next = left;
if(right <= limit && a[right] > a[next] )
next = right;
if(next!=i)
{
swap(a, …
Run Code Online (Sandbox Code Playgroud) 我想通过gdb看到我的数组作为参数传递给函数时的争议.
说,我有一些看起来像的代码
#include <stdio.h>
int fun(int b[], int len)
{
int i = 0;
/* how do I see the contents of array b[] in gdb */
for(i = 0; i < len; ++i)
printf("%d ", b[i]);
}
int main()
{
int a[] = {1,2,3,4,5};
fun(a, sizeof(a) / sizeof(*a));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在gdb中,[]如下所示
(gdb) disp a
1: a = {1, 2, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试打印b [],因为它是一个指针(因为数组作为指针传递),内容看起来像这样
fun (b=0x7fffffffdf90, len=5) at main.c:14
(gdb) disp b
2: b = (int *) 0x7fffffffdf90 …
Run Code Online (Sandbox Code Playgroud) 说,我试图找到数组中最大的元素,并按如下方式编写一些代码.
public class LargestElement
{
public static void main(String[] args)
{
int[] a = {1,2,6,4,5,4,3,1};
int max = a[0];
for(int i = 1;i<a.length;i++)
{
if(a[i] > max)
max = a[i];
}
System.out.println(max);
}
}
Run Code Online (Sandbox Code Playgroud)
这叫做线性搜索吗?
假设我有一个字符串的ArrayList,其中包含"hello"和"world"字样.我想在每个末尾添加单词"java".
我试图在循环时这样做,但没有成功.请建议我一个相同的方法.另一方面,我发现是创建一个不同的ArrayList和复制内容,但我不认为在空间方面是有效的.
import java.util.ArrayList;
public class EditArrayListInLoop {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("hello");
arrayList.add("world");
/* This does not work */
/*for(int i = 0;i<arrayList.size();i++)
{
arrayList.get(i) += "java";
}*/
ArrayList<String> arrayList2 = new ArrayList<String>();
for(int i = 0;i<arrayList.size();i++)
{
String test = arrayList.get(i);
test += "java";
arrayList2.add(test);
}
System.out.println(arrayList2);
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将一个字符串数组传递给一个函数,在这个函数中对它进行一些更改,并将其传递回main()
并打印它以查看更改.它没有按预期工作.请告诉我我哪里出错了.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//don't forget to declare this function
char** fun(char [][20]);
int main(void)
{
char strar[10][20] = { {"abc"}, {"def"}, {"ghi"}, {""},{""} }; //make sure 10 is added
char** ret; //no need to allocate anything for ret, ret is just a placeholder, allocation everything done in fun
int i = 0;
ret = fun(strar);
for(i=0;i<4;i++)
printf("[%s] ",ret[i]);
printf("\n");
return 0;
}
//don't forget function has to return char** and not int. (Remember char**, …
Run Code Online (Sandbox Code Playgroud) 我想定期检查 C# 中的 60 秒。我可以通过定期检查日期来做到这一点,如下所示
但是,当我使用秒表时,秒表会重置回开头,而不是从上次停止时继续。
using System;
using System.Diagnostics;
using System.Threading;
namespace StopwatchExample
{
class Program
{
static void Main()
{
////Works
//DateTime start = DateTime.Now;
//while (true)
//{
// DateTime current = DateTime.Now;
// var diffInSeconds = (current - start).TotalSeconds;
// if (diffInSeconds > 5)
// {
// Console.WriteLine("5 s done!");
// break;
// }
//}
// Does not work
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
stopwatch.Stop();
if (stopwatch.Elapsed.Seconds > 5)
{
Console.WriteLine("5 s done!"); …
Run Code Online (Sandbox Code Playgroud) 当我尝试下面的代码时,它给了我一个错误
int a =1,b=2;
if(a < b)
int c = 10;
Run Code Online (Sandbox Code Playgroud)
但如果我做同样添加花括号,它工作正常
int a =1,b=2;
if(a < b)
{
int c = 10;
}
Run Code Online (Sandbox Code Playgroud)
但我只是好奇为什么Java不允许在if条件中创建变量.