Python-用数据列表绘制正态分布线

时间:2018-09-19 18:30:01

标签: python-3.x math machine-learning

我正在从事电气工程项目,该项目需要绘制数据列表的正态分布。

我们随机测量了30个电阻的电阻并将其记下。

  X = [14.95,14.94,14.92,14.98,16.53,14.96,16.20,14.32,15.32,14.25,15.36,14.95,15.13,14.26,14.94,15.6,        15.20,14.94,15.02,15,14.62,14.94,14.94,14.98,15.12,15.06,14.95,14.96,15.13,15.20]

我想要这样的图形:

enter image description here

但是我得到这样的图:

enter image description here

我必须在基准点接近均值的图形中获得更多值。

这是我当前正在使用的代码:

import numpy as np
from matplotlib import pyplot as plt
import math

X = [14.95, 14.94, 14.92, 14.98, 16.53, 14.96, 16.20, 14.32, 15.32, 14.25, 15.36, 14.95, 15.13, 14.26, 14.94, 15.6,
     15.20, 14.94, 15.02, 15, 14.62, 14.94, 14.94, 14.98, 15.12, 15.06, 14.95, 14.96, 15.13, 15.20]

X = np.sort(X)
mean = np.mean(X)

sigma = 0

for i in X:
    sigma += np.square(mean - i)

sigma = np.sqrt(sigma / (len(X) - 1))


def func(x):
    return np.exp(np.square(x - mean) / (2 * np.square(sigma))) / np.sqrt(2 * math.pi * sigma)


Y = []

for i in X:
    Y.append(func(i))

plt.plot(X, Y, marker='o', color='b')
plt.show()

1 个答案:

答案 0 :(得分:0)

假设我正确理解了您的问题,认为您只是想添加更多数据点以生成正态分布曲线。

package com.jap.falcon;

import java.util.Scanner;

public class Problem2 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int seconds1;
        int seconds2;
        int minutes;
        int hours;

        System.out.println("Enter departure city: ");
        String departure = sc.nextLine();

        System.out.println("Enter arrival city ");
        String arrival = sc.nextLine();

        System.out.println("Enter time (in seconds) it takes to travel between: ");
        seconds1 = sc.nextInt();

        hours = (seconds1 % 86400) / 3600;
        minutes = ((seconds1 % 86400) % 3600) / 60;
        seconds2 = ((seconds1 % 86400) % 3600) % 60;

        System.out.println("The time to travel  between "+ departure +" and "+arrival+" is "); // I am trying to get this to say "The time between
                                                                    // + departure + and + arrival + is"
        System.out.println(hours + " : " + minutes + " : " + seconds2);
    }
}

但是,如果您还试图形成正态分布曲线,则不能仅将每个值与其概率密度函数作图。

尝试

mu = np.mean(X)
sigma = np.std(X) #You manually calculated it but you can also use this built-in function
data = np.random.normal(mu, sigma, SIZE_OF_DATA_YOU_NEED)

也可能希望将X与新的数据点连接起来。

希望以某种方式获得帮助,并在numpy.random.normal()上附加一个链接,以防以某种方式提供帮助(https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.random.normal.html)。