小编non*_*one的帖子

C中的更多链接列表

在我开始我想讲清楚,我不想回答我的家庭作业的问题,我只是想,如果有人能真正解释正是我的教练在这种分配(最好是简单化版)要求,也许一个有用的推动正确的方向.我在这个话题上遇到了很多麻烦,每当我问导师时,我发现他比我更困惑我.

所以,这是作业:

1.添加一个新函数insertN(struct list*x,int num,int pos,int n),它将在pos位置插入n个整数num的副本,如果可能的话(如果pos太大,采取适当的行动) .我在这里感到困惑的主要是他所说的位置pos.

这是我正在使用的代码 - 由我的老师编写,我必须修改它.

#include<stdio.h>
#include<stdlib.h>

struct list {
    int data;
    struct list * next;
        };

struct list *slist;

/*adds a node at the end of the linked list*/
void insert(struct list *x,int num){
  /*if the list is empty*/
  if(x==NULL){
    /*create first node*/
    slist=malloc(sizeof(struct list));
    slist->data=num; 
    slist->next=NULL;
    }
  else{
    /*go to the last node*/
    while(x->next!=NULL) x=x->next;
    /*add node at the end*/
      x->next=malloc(sizeof(struct list));
      x->next->data=num;
      x->next->next=NULL;

  }
}


void display(struct list *x){
  /*traverse the …
Run Code Online (Sandbox Code Playgroud)

c pointers structure linked-list

12
推荐指数
1
解决办法
586
查看次数

C语言中的简单乘法和算术

我有一个计算C中二次方程的根的任务,应该非常简单,我知道我需要对程序做什么,但我仍然遇到了问题.当根是虚构的并且平方根内的项为零时,它工作正常.

但是当我输入系数a,b和c时会产生真正的根,它给了我错误的答案,我无法弄清楚什么是错的.(我用a = 2,b = -5和c = 1测试它)

这是我的代码,它编译并运行,但给出了错误的答案.

#include<stdio.h>
#include<math.h>

int main()
{
    float a, b, c, D, x, x1, x2, y, xi;

    printf("Please enter a:\n");
    scanf("%f", &a);
    printf("Please enter b:\n");
    scanf("%f",&b);
    printf("Please enter c:\n");
    scanf("%f", &c);
    printf("The numbers you entered are: a = %f, b = %f, c = %f\n", a, b, c);


    D = b*b-4.0*a*c;
    printf("D = %f\n", D);
    if(D > 0){
        x1 =  (-b + sqrt(D))/2*a;
        x2 = ((-b) - sqrt(D))/2*a;
        printf("The two real roots are …
Run Code Online (Sandbox Code Playgroud)

c math quadratic

3
推荐指数
1
解决办法
259
查看次数

逐行读取文本文件中的信息

我想学习如何逐行读取.txt文件C,以便每行中的信息可以用于循环等.我没有这方面的功课,但我需要弄清楚一个项目写一个天文包的脚本.

所以为了学习如何做到这一点,我创建了一个名为list.txt1,2,3,4,5,6,7,8,9 的文件,每个文件在不同的行上,我希望程序对每行的值求和.我还创建了一个调用的输出文件sum.txt,该程序应该对行进行求和,并将总和打印到输出文件中.

我运行程序时遇到错误,我不确定问题是什么,因为我不完全理解这一点.我在一本关于C的书中得到了这个程序,并尝试修改它以在我的PC上运行它.

我需要能够做到这一点的原因是因为我需要从列表中的每一行获取信息(每行包含一个字符串).但我想学习先用数字来做.

这是代码:

#include<stdio.h>

int main(void)
{
    int a, sum = 0;
    FILE *ifp, *ofp;
    ifp = fopen("list.txt", "r");
    ofp = ("sum.txt", "w");
    while (fscanf(ifp, "%d", &a) == 1)
    {
        sum += a;
        fprintf(ofp, "The sum is %d. \n", sum);
        fclose(ifp);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c file-io

3
推荐指数
1
解决办法
694
查看次数

如何确定sklearn logistic回归的预测概率是否准确?

我是机器学习的新手,我正在尝试使用scikit-learn来制作一个简单的逻辑回归模型,其中包含1个输入变量(X)和一个二元结果(Y).我的数据包括325个样本,39个成功和286个失败.数据被分成训练和测试(30%)集.

我的目标实际上是根据我的数据获得任何给定X的预测成功概率,而不是分类预测本身.也就是说,我将把预测的概率用于我正在构建的单独模型中,并且根本不会使用逻辑回归作为分类器.因此,预测的概率实际上适合数据是很重要的.

但是,我无法理解我的模型是否适合数据,或者计算的概率是否真实准确.

我得到以下指标:

  • 分类准确度:metrics.accuracy_score(Y_test,预测) = 0.92.我对这个指标的理解是模型很有可能做出正确的预测,所以我觉得这个模型非常合适.

  • 记录丢失:cross_val_score(LogisticRegression(),X,Y,scoring ='neg_log_loss',cv = 10) = -0.26这对我来说可能是最令人困惑的指标,显然最重要的是因为它是预测概率的准确性.我知道得分越接近于零越好 - 但距离足够接近?

  • AUC:metrics.roc_auc_score(Y_test,probs [:,1]) = 0.9.再次,这看起来不错,因为ROC得分越接近1越好.

  • 混淆矩阵:metrics.confusion_matrix(Y_test,预测) =

            [  88,  0]
               [8,  2]
    
    Run Code Online (Sandbox Code Playgroud)

    我在这里的理解是,对角线给出了训练集中正确预测的数量,所以这看起来不错.

  • 报告:metrics.classification_report(Y_test,预测) =

                precision    recall  f1-score   support
    
    0.0       0.92      1.00      0.96        88
    1.0       1.00      0.20      0.33        10
    
    avg / total       0.93      0.92      0.89        98
    
    Run Code Online (Sandbox Code Playgroud)

    根据该分类报告,该模型具有良好的精度,因此非常适合.我不确定如何解释召回或者如果这个报告对我的模型来说是个坏消息 - sklearn文档指出召回是一种模型能力,可以找到所有正样本 - 因此预测为1时得分为0.2意味着它只有20%的时间才能找到积极因素?这听起来像是非常不合适的数据.

如果有人能够澄清我正在以正确的方式处理这些指标,我真的很感激 - 也许可以说明我的模型是好还是虚假.此外,如果我还有其他测试可以确定计算的概率是否准确,请告诉我.

如果这些不是很好的度量标准分数,我真的很欣赏在改进方面的下一步方向.

谢谢!!

python machine-learning scikit-learn logistic-regression

1
推荐指数
1
解决办法
1778
查看次数

如何使用结构来表示复数

我需要编写一个程序,它使用结构来定义复数,即z1 = x + yi.然后添加2个复数.在继续使用我的代码之前,我需要弄清楚如何正确地使用它们.到目前为止,我已经尝试了一些东西,这是我提出的最好的东西,它仍然没有编译.

这是我的代码的副本,我只需要修复这部分,然后我就可以自己做其余的事了.

#include<stdio.h>

typedef struct complex1{
    float *real1;
    float *imaginary1;
} complex1;


typedef struct complex2{
    float *real2;
    float *imaginary2;
} complex2;


int main(){
  struct complex1 real;
  struct complex1 *realptr;
  struct complex1 imaginary;
  struct complex1 *imaginaryptr;
  struct complex2 real;
  struct complex2 *realptr;
  struct complex2 imaginary;
  struct complex2 *imaginaryptr;

  printf("Please enter variable x1.");
  scanf("%d", &real.real1);
  printf("Please enter variable y1.");
  scanf("%d", &imaginary.imaginary1);
  printf("Please enter variable x2.");
  scanf("%d", &real.real2);
  printf("Please enter variable y2.");
  scanf("%d", &imaginary.imaginary2);
  printf("You have entered: %d,%d,%d,%d\n", 
  real.real1, …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
6336
查看次数

对于c中的for循环内的循环

出于某种原因,我的外部for循环似乎没有做任何事情,我检查了所有的paranthesis,一切看起来还不错,但它仍然没有循环.有了这个程序,我想总计50个随机数(数字可以是1或-1 ......这是计算物理问题)并打印程序所做的大小.但我想进一步做10次,并计算平均幅度.我知道我需要做什么我只是遇到了这个循环的问题.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h> //Neeed this to seed the random generator with time, or else it      will always generate the same numbers.

//This is a program to calculate the magnitude of the displacement of a particle after    random collisions.
#define RAND_MAX 1
int main()
{   
    //using ints because the particle can only move one unit on x-axis at a time.
    int i, x, displacement, sum = 0, avg;
    int total_disp=0, mag_disp;
    srand(time(NULL));


    //Each collision causes the particle to …
Run Code Online (Sandbox Code Playgroud)

c for-loop

-1
推荐指数
2
解决办法
4099
查看次数